Complex classes like UsedSymbolCollector 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 UsedSymbolCollector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | final class UsedSymbolCollector extends NodeVisitorAbstract |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var mixed[] |
||
| 12 | */ |
||
| 13 | private $collectedSymbols = []; |
||
| 14 | |||
| 15 | public function __construct() |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @return string[] |
||
| 21 | */ |
||
| 22 | public function getCollectedSymbols() : array |
||
| 26 | |||
| 27 | /** |
||
| 28 | * {@inheritDoc} |
||
| 29 | */ |
||
| 30 | public function beforeTraverse(array $nodes) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritDoc} |
||
| 39 | */ |
||
| 40 | public function enterNode(Node $node) |
||
| 54 | |||
| 55 | private function recordExtendsUsage(Node $node) |
||
| 65 | |||
| 66 | private function recordImplementsUsage(Node $node) |
||
| 72 | |||
| 73 | private function recordClassExpressionUsage(Node $node) |
||
| 89 | |||
| 90 | private function recordCatchUsage(Node $node) |
||
| 96 | |||
| 97 | private function recordFunctionCallUsage(Node $node) |
||
| 107 | |||
| 108 | private function recordFunctionParameterTypesUsage(Node $node) |
||
| 123 | |||
| 124 | private function recordFunctionReturnTypeUsage(Node $node) |
||
| 137 | |||
| 138 | private function recordConstantFetchUsage(Node $node) |
||
| 144 | |||
| 145 | private function recordTraitUsage(Node $node) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param Node\Name $symbol |
||
| 164 | * |
||
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | private function recordUsageOf(Node\Name $symbol) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param Node\Name $symbol |
||
| 174 | * |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | private function recordUsageOfByString(string $symbol) |
||
| 181 | } |
||
| 182 |