Total Complexity | 42 |
Total Lines | 118 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 5 |
Complex classes like ClassMethodArgVisitor 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.
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 ClassMethodArgVisitor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class ClassMethodArgVisitor extends NodeVisitorAbstract |
||
17 | { |
||
18 | private $keys = []; |
||
19 | |||
20 | private $filePath; |
||
21 | |||
22 | private $className; |
||
23 | |||
24 | private $classArgposClassesMap = []; |
||
25 | |||
26 | private $config; |
||
27 | |||
28 | public function __construct(array &$keys, string $filePath, array $config) |
||
29 | { |
||
30 | $this->keys = &$keys; |
||
31 | $this->filePath = $filePath; |
||
32 | $this->className = (string)pathinfo($filePath, PATHINFO_FILENAME); |
||
33 | $this->config = $config; |
||
34 | } |
||
35 | |||
36 | public function enterNode(Node $node) |
||
61 | } |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | |||
67 | private function prepareUseClasses(Node $node) |
||
68 | { |
||
69 | if ($node instanceof Use_) { |
||
70 | foreach ($node->uses as $use) { |
||
71 | $useName = $use->name->name; |
||
72 | foreach ($this->config['ARGPOS_CLASSES'] ?? [] as $argIndex => $classes) { |
||
73 | if (in_array($useName, $classes)) { |
||
74 | $shortName = basename(str_replace('\\', '/', $useName)); |
||
75 | $this->classArgposClassesMap[$argIndex] = $shortName; |
||
76 | } |
||
77 | } |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | |||
82 | private function extractKeyFromArgument(MethodCall $node, int $argIndex, string $classNamePart): void |
||
123 | } |
||
124 | } |
||
125 | |||
126 | private function addKey(int $line, string $call, string $key, string $arg = null): void |
||
137 |