1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\PHPStan\Rules\TypeHints; |
5
|
|
|
|
6
|
|
|
use PHPStan\Analyser\Scope; |
7
|
|
|
use PHPStan\Broker\Broker; |
8
|
|
|
use PHPStan\Reflection\ParametersAcceptorSelector; |
9
|
|
|
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; |
10
|
|
|
use PHPStan\Reflection\Php\PhpParameterReflection; |
11
|
|
|
use Roave\BetterReflection\Reflection\ReflectionFunction; |
12
|
|
|
use Roave\BetterReflection\Reflection\ReflectionFunctionAbstract; |
13
|
|
|
use Roave\BetterReflection\Reflection\ReflectionMethod; |
14
|
|
|
use Roave\BetterReflection\Reflection\ReflectionParameter; |
15
|
|
|
use PhpParser\Node; |
16
|
|
|
|
17
|
|
|
class MissingTypeHintInFunctionRule extends AbstractMissingTypeHintRule |
18
|
|
|
{ |
19
|
|
|
public function getNodeType(): string |
20
|
|
|
{ |
21
|
|
|
return Node\Stmt\Function_::class; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function isReturnIgnored(Node $node): bool |
25
|
|
|
{ |
26
|
|
|
return false; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function getReflection(Node\FunctionLike $function, Scope $scope, Broker $broker) : ParametersAcceptorWithPhpDocs |
30
|
|
|
{ |
31
|
|
|
$functionName = $function->name->name; |
|
|
|
|
32
|
|
|
if (isset($function->namespacedName)) { |
|
|
|
|
33
|
|
|
$functionName = (string) $function->namespacedName; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
$functionNameName = new Node\Name($functionName); |
36
|
|
|
if (!$broker->hasCustomFunction($functionNameName, null)) { |
37
|
|
|
throw new \RuntimeException("Cannot find function '$functionName'"); |
38
|
|
|
} |
39
|
|
|
$functionReflection = $broker->getCustomFunction($functionNameName, null); |
40
|
|
|
/** @var \PHPStan\Reflection\ParametersAcceptorWithPhpDocs $parametersAcceptor */ |
41
|
|
|
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function shouldSkip(Node\FunctionLike $function, Scope $scope): bool |
45
|
|
|
{ |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: