Complex classes like ContextFactory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContextFactory, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
26 | final class ContextFactory |
||
27 | { |
||
28 | /** The literal used at the end of a use statement. */ |
||
29 | const T_LITERAL_END_OF_USE = ';'; |
||
30 | |||
31 | /** The literal used between sets of use statements */ |
||
32 | const T_LITERAL_USE_SEPARATOR = ','; |
||
33 | |||
34 | /** |
||
35 | * Build a Context given a Class Reflection. |
||
36 | * |
||
37 | * @see Context for more information on Contexts. |
||
38 | 4 | */ |
|
39 | public function createFromReflector(\Reflector $reflector): Context |
||
63 | 4 | ||
64 | private function createFromReflectionParameter(\ReflectionParameter $parameter): Context |
||
68 | |||
69 | 2 | private function createFromReflectionMethod(\ReflectionMethod $method): Context |
|
73 | |||
74 | private function createFromReflectionProperty(\ReflectionProperty $property): Context |
||
78 | |||
79 | private function createFromReflectionClassConstant(\ReflectionClassConstant $constant): Context |
||
83 | |||
84 | 6 | private function createFromReflectionClass(\ReflectionClass $class): Context |
|
95 | |||
96 | /** |
||
97 | * Build a Context for a namespace in the provided file contents. |
||
98 | 6 | * |
|
99 | 6 | * @param string $namespace It does not matter if a `\` precedes the namespace name, this method first normalizes. |
|
100 | 6 | * @param string $fileContents the file's contents to retrieve the aliases from with the given namespace. |
|
101 | 6 | * |
|
102 | 6 | * @see Context for more information on Contexts. |
|
103 | 6 | * |
|
104 | 6 | * @return Context |
|
105 | 6 | */ |
|
106 | public function createForNamespace($namespace, $fileContents) |
||
154 | |||
155 | /** |
||
156 | 4 | * Deduce the name from tokens when we are at the T_NAMESPACE token. |
|
157 | * |
||
158 | 4 | * @return string |
|
159 | 4 | */ |
|
160 | private function parseNamespace(\ArrayIterator $tokens) |
||
174 | |||
175 | /** |
||
176 | * Deduce the names of all imports when we are at the T_USE token. |
||
177 | 6 | * |
|
178 | * @return string[] |
||
179 | 6 | */ |
|
180 | 6 | private function parseUseStatement(\ArrayIterator $tokens) |
|
197 | 4 | ||
198 | 4 | /** |
|
199 | * Fast-forwards the iterator as longs as we don't encounter a T_STRING or T_NS_SEPARATOR token. |
||
200 | */ |
||
201 | 4 | private function skipToNextStringOrNamespaceSeparator(\ArrayIterator $tokens) |
|
207 | |||
208 | 4 | /** |
|
209 | 4 | * Deduce the namespace name and alias of an import when we are at the T_USE token or have not reached the end of |
|
210 | * a USE statement yet. |
||
211 | 4 | * |
|
212 | 4 | * @return array |
|
213 | */ |
||
214 | 4 | private function extractUseStatement(\ArrayIterator $tokens) |
|
244 | } |
||
245 |
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.