Complex classes like ReflectionEngine 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 ReflectionEngine, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class ReflectionEngine |
||
33 | { |
||
34 | /** |
||
35 | * @var null|LocatorInterface |
||
36 | */ |
||
37 | protected static $locator; |
||
38 | |||
39 | /** |
||
40 | * @var array|Node[] |
||
41 | */ |
||
42 | protected static $parsedFiles = []; |
||
43 | |||
44 | /** |
||
45 | * @var null|int |
||
46 | */ |
||
47 | protected static $maximumCachedFiles; |
||
48 | |||
49 | /** |
||
50 | * @var null|Parser |
||
51 | */ |
||
52 | protected static $parser; |
||
53 | |||
54 | /** |
||
55 | * @var null|NodeTraverser |
||
56 | */ |
||
57 | protected static $traverser; |
||
58 | |||
59 | /** |
||
60 | * @var null|Lexer |
||
61 | */ |
||
62 | protected static $lexer; |
||
63 | |||
64 | 1 | private function __construct() {} |
|
65 | |||
66 | 1 | public static function init(LocatorInterface $locator): void |
|
86 | |||
87 | /** |
||
88 | * Limits number of files, that can be cached at any given moment |
||
89 | */ |
||
90 | public static function setMaximumCachedFiles(int $newLimit): void |
||
97 | |||
98 | /** |
||
99 | * Locates a file name for class |
||
100 | */ |
||
101 | public static function locateClassFile(string $fullClassName): string |
||
119 | 22 | ||
120 | /** |
||
121 | * Tries to parse a class by name using LocatorInterface |
||
122 | */ |
||
123 | 22 | public static function parseClass(string $fullClassName): ClassLike |
|
143 | |||
144 | 22 | /** |
|
145 | 22 | * Loop through an array and find a ClassLike statement by the given class name. |
|
146 | 22 | * |
|
147 | * If an if statement like `if (false) {` is found, the class will also be search inside that if statement. |
||
148 | 22 | * This relies on the guide of greg0ire on how to deprecate a type. |
|
149 | * |
||
150 | * @see https://dev.to/greg0ire/how-to-deprecate-a-type-in-php-48cf |
||
151 | */ |
||
152 | protected static function findClassLikeNodeByClassName(array $nodes, string $className): ?ClassLike |
||
173 | |||
174 | /** |
||
175 | * Parses class method |
||
176 | */ |
||
177 | public static function parseClassMethod(string $fullClassName, string $methodName): ClassMethod |
||
190 | 1 | ||
191 | 1 | /** |
|
192 | 1 | * Parses class property |
|
193 | 1 | * |
|
194 | 1 | * @return array Pair of [Property and PropertyProperty] nodes |
|
195 | */ |
||
196 | public static function parseClassProperty(string $fullClassName, string $propertyName): array |
||
213 | |||
214 | /** |
||
215 | * Parses class constants |
||
216 | * |
||
217 | * @param string $fullClassName |
||
218 | * @param string $constantName |
||
219 | * @return array Pair of [ClassConst and Const_] nodes |
||
220 | */ |
||
221 | public static function parseClassConstant(string $fullClassName, string $constantName): array |
||
238 | 3051 | ||
239 | 3051 | /** |
|
240 | 3049 | * Parses a file and returns an AST for it |
|
241 | * |
||
242 | * @param string|null $fileContent Optional content of the file |
||
243 | 9 | * |
|
244 | * @return Node[] |
||
245 | */ |
||
246 | public static function parseFile(string $fileName, ?string $fileContent = null) |
||
267 | 37 | ||
268 | /** |
||
269 | 37 | * Parses a file namespace and returns an AST for it |
|
270 | * |
||
271 | 37 | * @throws ReflectionException |
|
272 | 37 | */ |
|
273 | 2 | public static function parseFileNamespace(string $fileName, string $namespaceName): Namespace_ |
|
289 | |||
290 | /** |
||
291 | * @return Lexer |
||
292 | */ |
||
293 | public static function getLexer(): ?Lexer |
||
297 | } |
||
298 |