1 | <?php |
||
23 | final class CodeGenMethod |
||
24 | { |
||
25 | /** |
||
26 | * @var \PhpParser\Parser |
||
27 | */ |
||
28 | private $parser; |
||
29 | |||
30 | /** |
||
31 | * @var \PhpParser\BuilderFactory |
||
32 | */ |
||
33 | private $factory; |
||
34 | |||
35 | /** |
||
36 | * @var \PhpParser\PrettyPrinter\Standard |
||
37 | */ |
||
38 | private $printer; |
||
39 | |||
40 | private $reader; |
||
41 | |||
42 | /** |
||
43 | * @var AbstractAssisted |
||
44 | */ |
||
45 | private $assisted; |
||
46 | |||
47 | /** |
||
48 | * @param \PhpParser\Parser $parser |
||
49 | * @param \PhpParser\BuilderFactory $factory |
||
50 | * @param \PhpParser\PrettyPrinter\Standard $printer |
||
51 | */ |
||
52 | 32 | public function __construct( |
|
53 | Parser $parser, |
||
54 | BuilderFactory $factory, |
||
55 | Standard $printer |
||
56 | ) { |
||
57 | 32 | $this->parser = $parser; |
|
58 | 32 | $this->factory = $factory; |
|
59 | 32 | $this->printer = $printer; |
|
60 | 32 | $this->reader = new AnnotationReader; |
|
61 | 32 | } |
|
62 | |||
63 | /** |
||
64 | * @param \ReflectionClass $class |
||
65 | * @param BindInterface $bind |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | 16 | public function getMethods(\ReflectionClass $class, BindInterface $bind) : array |
|
70 | { |
||
71 | 16 | $bindingMethods = array_keys($bind->getBindings()); |
|
72 | 16 | $stmts = []; |
|
73 | 16 | $methods = $class->getMethods(); |
|
74 | 16 | foreach ($methods as $method) { |
|
75 | 16 | $this->assisted = $this->reader->getMethodAnnotation($method, AbstractAssisted::class); |
|
76 | 16 | $isBindingMethod = in_array($method->name, $bindingMethods, true); |
|
77 | /* @var $method \ReflectionMethod */ |
||
78 | 16 | if ($isBindingMethod && $method->isPublic()) { |
|
79 | 16 | $stmts[] = $this->getMethod($method); |
|
80 | } |
||
81 | } |
||
82 | |||
83 | 16 | return $stmts; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Return method statement |
||
88 | * |
||
89 | * @param \ReflectionMethod $method |
||
90 | * |
||
91 | * @return \PhpParser\Node\Stmt\ClassMethod |
||
92 | */ |
||
93 | 15 | private function getMethod(\ReflectionMethod $method) |
|
94 | { |
||
95 | 15 | $methodStmt = $this->factory->method($method->name); |
|
96 | 15 | $params = $method->getParameters(); |
|
97 | 15 | foreach ($params as $param) { |
|
98 | 13 | $methodStmt = $this->getMethodStatement($param, $methodStmt); |
|
99 | } |
||
100 | 15 | $returnType = $method->getReturnType(); |
|
101 | 15 | if ($returnType instanceof \ReflectionType) { |
|
|
|||
102 | 5 | $this->setReturnType($returnType, $methodStmt); |
|
103 | } |
||
104 | 15 | $methodInsideStatements = $this->getMethodInsideStatement($method); |
|
105 | 15 | $methodStmt->addStmts($methodInsideStatements); |
|
106 | |||
107 | 15 | return $this->addMethodDocComment($methodStmt, $method); |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Return parameter reflection |
||
112 | */ |
||
113 | 13 | private function getMethodStatement(\ReflectionParameter $param, Method $methodStmt) : Method |
|
114 | { |
||
115 | /* @var $paramStmt Param */ |
||
116 | 13 | $paramStmt = $this->factory->param($param->name); |
|
117 | /* @var $param \ReflectionParameter */ |
||
118 | 13 | $this->setParameterType($param, $paramStmt); |
|
119 | 13 | $this->setDefault($param, $paramStmt); |
|
120 | 13 | $methodStmt->addParam($paramStmt); |
|
121 | |||
122 | 13 | return $methodStmt; |
|
123 | } |
||
124 | |||
125 | 15 | private function addMethodDocComment(Method $methodStmt, \ReflectionMethod $method) : ClassMethod |
|
135 | |||
136 | /** |
||
137 | * @return \PhpParser\Node[] |
||
138 | */ |
||
139 | 15 | private function getMethodInsideStatement(\ReflectionMethod $method) : array |
|
153 | |||
154 | /** |
||
155 | * @codeCoverageIgnore |
||
156 | */ |
||
157 | private function setTypeHint(\ReflectionParameter $param, Param $paramStmt, \ReflectionClass $typeHint = null) |
||
169 | |||
170 | 13 | private function setDefault(\ReflectionParameter $param, Param $paramStmt) |
|
181 | |||
182 | 13 | private function setParameterType(\ReflectionParameter $param, Param $paramStmt) |
|
196 | |||
197 | 5 | private function setReturnType(\ReflectionType $returnType, Method $methodStmt) |
|
202 | } |
||
203 |
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.