Error Identifier: missingType.callable
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
class Foo
{
public function doFoo(callable $cb): void // error: Method Foo::doFoo() has parameter $cb
{ // with no signature specified for callable.
}
}
Why is it reported? #
Using callable without specifying its parameter and return types means PHPStan cannot verify that the correct callable is being passed. A bare callable type is equivalent to accepting any callable regardless of its signature, which can lead to runtime type errors when the callable is invoked with unexpected arguments or returns an unexpected type.
This error is reported when the checkMissingCallableSignature option is enabled.
How to fix it #
Specify the callable signature using PHPDoc.
class Foo
{
+ /**
+ * @param callable(string): bool $cb
+ */
public function doFoo(callable $cb): void
{
}
}
Alternatively, use a Closure type with a specified signature.
class Foo
{
- public function doFoo(callable $cb): void
+ /**
+ * @param \Closure(string): bool $cb
+ */
+ public function doFoo(\Closure $cb): void
{
}
}
How to ignore this error #
You can use the identifier missingType.callable to ignore this error using a comment:
// @phpstan-ignore missingType.callable
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: missingType.callable
Rules that report this error #
- PHPStan\Rules\Classes\LocalTypeAliasesRule [1]
- PHPStan\Rules\Classes\LocalTypeTraitAliasesRule [1]
- PHPStan\Rules\Classes\LocalTypeTraitUseAliasesRule [1]
- PHPStan\Rules\Classes\MethodTagRule [1]
- PHPStan\Rules\Classes\MethodTagTraitRule [1]
- PHPStan\Rules\Classes\MethodTagTraitUseRule [1]
- PHPStan\Rules\Classes\MixinRule [1]
- PHPStan\Rules\Classes\MixinTraitRule [1]
- PHPStan\Rules\Classes\MixinTraitUseRule [1]
- PHPStan\Rules\Classes\PropertyTagRule [1]
- PHPStan\Rules\Classes\PropertyTagTraitRule [1]
- PHPStan\Rules\Classes\PropertyTagTraitUseRule [1]
- PHPStan\Rules\Constants\MissingClassConstantTypehintRule [1]
- PHPStan\Rules\Functions\MissingFunctionParameterTypehintRule [1]
- PHPStan\Rules\Functions\MissingFunctionReturnTypehintRule [1]
- PHPStan\Rules\Methods\MissingMethodParameterTypehintRule [1]
- PHPStan\Rules\Methods\MissingMethodReturnTypehintRule [1]
- PHPStan\Rules\Methods\MissingMethodSelfOutTypeRule [1]
- PHPStan\Rules\PhpDoc\FunctionAssertRule [1]
- PHPStan\Rules\PhpDoc\MethodAssertRule [1]
- PHPStan\Rules\Properties\MissingPropertyTypehintRule [1]
- PHPStan\Rules\Properties\SetPropertyHookParameterRule [1]