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\ClassReflection; |
9
|
|
|
use PHPStan\Reflection\ParametersAcceptorSelector; |
10
|
|
|
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; |
11
|
|
|
use PHPStan\Reflection\Php\PhpMethodReflection; |
12
|
|
|
use Roave\BetterReflection\Reflection\ReflectionFunction; |
13
|
|
|
use Roave\BetterReflection\Reflection\ReflectionFunctionAbstract; |
14
|
|
|
use Roave\BetterReflection\Reflection\ReflectionMethod; |
15
|
|
|
use Roave\BetterReflection\Reflection\ReflectionParameter; |
16
|
|
|
use PhpParser\Node; |
17
|
|
|
use PHPStan\Reflection\MethodReflection; |
18
|
|
|
|
19
|
|
|
class MissingTypeHintInMethodRule extends AbstractMissingTypeHintRule |
20
|
|
|
{ |
21
|
|
|
private const RETURN_BLACKLIST = [ |
22
|
|
|
'__construct' => true, |
23
|
|
|
'__destruct' => true, |
24
|
|
|
'__call' => true, |
25
|
|
|
'__callStatic' => true, |
26
|
|
|
'__get' => true, |
27
|
|
|
'__set' => true, |
28
|
|
|
'__isset' => true, |
29
|
|
|
'__unset' => true, |
30
|
|
|
'__sleep' => true, |
31
|
|
|
'__wakeup' => true, |
32
|
|
|
'__toString' => true, |
33
|
|
|
'__invoke' => true, |
34
|
|
|
'__set_state' => true, |
35
|
|
|
'__clone' => true, |
36
|
|
|
'__debugInfo' => true |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
public function getNodeType(): string |
40
|
|
|
{ |
41
|
|
|
return Node\Stmt\ClassMethod::class; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Node\Stmt\ClassMethod $node |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function isReturnIgnored(Node $node): bool |
49
|
|
|
{ |
50
|
|
|
return isset(self::RETURN_BLACKLIST[$node->name->name]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function getReflection(Node\FunctionLike $function, Scope $scope, Broker $broker) : ParametersAcceptorWithPhpDocs |
54
|
|
|
{ |
55
|
|
|
if (!$scope->isInClass()) { |
56
|
|
|
throw new \PHPStan\ShouldNotHappenException(); |
57
|
|
|
} |
58
|
|
|
$nativeMethod = $scope->getClassReflection()->getNativeMethod($function->name->name); |
59
|
|
|
if (!$nativeMethod instanceof PhpMethodReflection) { |
|
|
|
|
60
|
|
|
throw new \PHPStan\ShouldNotHappenException(); |
61
|
|
|
} |
62
|
|
|
/** @var \PHPStan\Reflection\ParametersAcceptorWithPhpDocs $parametersAcceptor */ |
63
|
|
|
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($nativeMethod->getVariants()); |
64
|
|
|
return $parametersAcceptor; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function shouldSkip(Node\FunctionLike $function, Scope $scope): bool |
68
|
|
|
{ |
69
|
|
|
// We should skip if the method is inherited! |
70
|
|
|
if (!$scope->isInClass()) { |
71
|
|
|
throw new \PHPStan\ShouldNotHappenException(); |
72
|
|
|
} |
73
|
|
|
$nativeMethod = $scope->getClassReflection()->getNativeMethod($function->name->name); |
74
|
|
|
|
75
|
|
|
return $this->isInherited2($nativeMethod, $scope->getClassReflection()); |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function isInherited2(MethodReflection $method, ClassReflection $class = null): bool |
80
|
|
|
{ |
81
|
|
|
if ($class === null) { |
82
|
|
|
$class = $method->getDeclaringClass(); |
83
|
|
|
} |
84
|
|
|
$interfaces = $class->getInterfaces(); |
85
|
|
|
foreach ($interfaces as $interface) { |
86
|
|
|
if ($interface->hasMethod($method->getName())) { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$parentClass = $class->getParentClass(); |
92
|
|
|
if ($parentClass !== false) { |
93
|
|
|
if ($parentClass->hasMethod($method->getName())) { |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
return $this->isInherited2($method, $parentClass); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.