Complex classes like GeneratorNavigator 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 GeneratorNavigator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class GeneratorNavigator { |
||
| 41 | |||
| 42 | private $constantSortFunc; |
||
| 43 | |||
| 44 | private $propertySortFunc; |
||
| 45 | |||
| 46 | private $methodSortFunc; |
||
| 47 | |||
| 48 | private $useStatementSortFunc; |
||
| 49 | |||
| 50 | private static $defaultMethodSortFunc; |
||
| 51 | |||
| 52 | private static $defaultPropertySortFunc; |
||
| 53 | |||
| 54 | private static $defaultUseStatementSortFunc; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Sets a custom constant sorting function. |
||
| 58 | * |
||
| 59 | * @param null|\Closure $func |
||
| 60 | */ |
||
| 61 | 2 | public function setConstantSortFunc(\Closure $func = null) { |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Sets a custom property sorting function. |
||
| 67 | * |
||
| 68 | * @param null|\Closure $func |
||
| 69 | */ |
||
| 70 | 2 | public function setPropertySortFunc(\Closure $func = null) { |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Sets a custom method sorting function. |
||
| 76 | * |
||
| 77 | * @param null|\Closure $func |
||
| 78 | */ |
||
| 79 | 2 | public function setMethodSortFunc(\Closure $func = null) { |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Sets a custom method sorting function. |
||
| 85 | * |
||
| 86 | * @param null|\Closure $func |
||
| 87 | */ |
||
| 88 | public function setUseStatementSortFunc(\Closure $func = null) { |
||
| 91 | |||
| 92 | 18 | public function accept(GeneratorVisitorInterface $visitor, GenerateableInterface $model) { |
|
| 93 | 18 | if ($model instanceof AbstractPhpStruct) { |
|
| 94 | 13 | $this->visitStruct($visitor, $model); |
|
| 95 | 5 | } else if ($model instanceof PhpFunction) { |
|
| 96 | 5 | $visitor->visitFunction($model); |
|
| 97 | } |
||
| 98 | 18 | } |
|
| 99 | |||
| 100 | 13 | private function visitStruct(GeneratorVisitorInterface $visitor, AbstractPhpStruct $struct) { |
|
| 101 | // start struct - sort use statements |
||
| 102 | 13 | $useStatements = $struct->getUseStatements(); |
|
| 103 | 13 | uasort($useStatements, $this->getUseStatementSortFunc()); |
|
| 104 | 13 | $struct->setUseStatements($useStatements); |
|
| 105 | |||
| 106 | 13 | if ($struct instanceof PhpInterface) { |
|
| 107 | 1 | $visitor->startVisitingInterface($struct); |
|
| 108 | 12 | } else if ($struct instanceof PhpTrait) { |
|
| 109 | 1 | $visitor->startVisitingTrait($struct); |
|
| 110 | 11 | } else if ($struct instanceof PhpClass) { |
|
| 111 | 11 | $visitor->startVisitingClass($struct); |
|
| 112 | } |
||
| 113 | |||
| 114 | // contents |
||
| 115 | 13 | if ($struct instanceof ConstantsInterface) { |
|
| 116 | 12 | $constants = $struct->getConstants(true); |
|
| 117 | 12 | if (!empty($constants)) { |
|
| 118 | 6 | uksort($constants, $this->getConstantSortFunc()); |
|
| 119 | |||
| 120 | 6 | $visitor->startVisitingStructConstants(); |
|
| 121 | 6 | foreach ($constants as $constant) { |
|
| 122 | 6 | $visitor->visitStructConstant($constant); |
|
| 123 | } |
||
| 124 | 6 | $visitor->endVisitingStructConstants(); |
|
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | 13 | if ($struct instanceof TraitsInterface) { |
|
| 129 | 12 | $properties = $struct->getProperties(); |
|
| 130 | 12 | if (!empty($properties)) { |
|
| 131 | 6 | usort($properties, $this->getPropertySortFunc()); |
|
| 132 | |||
| 133 | 6 | $visitor->startVisitingProperties(); |
|
| 134 | 6 | foreach ($properties as $property) { |
|
| 135 | 6 | $visitor->visitProperty($property); |
|
| 136 | } |
||
| 137 | 6 | $visitor->endVisitingProperties(); |
|
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | 13 | $methods = $struct->getMethods(); |
|
| 142 | 13 | if (!empty($methods)) { |
|
| 143 | 8 | usort($methods, $this->getMethodSortFunc()); |
|
| 144 | |||
| 145 | 8 | $visitor->startVisitingMethods(); |
|
| 146 | 8 | foreach ($methods as $method) { |
|
| 147 | 8 | $visitor->visitMethod($method); |
|
| 148 | } |
||
| 149 | 8 | $visitor->endVisitingMethods(); |
|
| 150 | } |
||
| 151 | |||
| 152 | // end struct |
||
| 153 | 13 | if ($struct instanceof PhpInterface) { |
|
| 154 | 1 | $visitor->endVisitingInterface($struct); |
|
| 155 | 12 | } else if ($struct instanceof PhpTrait) { |
|
| 156 | 1 | $visitor->endVisitingTrait($struct); |
|
| 157 | 11 | } else if ($struct instanceof PhpClass) { |
|
| 158 | 11 | $visitor->endVisitingClass($struct); |
|
| 159 | } |
||
| 160 | 13 | } |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Returns the constants sort function |
||
| 164 | * |
||
| 165 | * @return \Closure |
||
| 166 | */ |
||
| 167 | 6 | private function getConstantSortFunc() { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Returns the use statements sort function |
||
| 173 | * |
||
| 174 | * @return \Closure |
||
| 175 | */ |
||
| 176 | 13 | private function getUseStatementSortFunc() { |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Returns the methods sort function |
||
| 218 | * |
||
| 219 | * @return \Closure |
||
| 220 | */ |
||
| 221 | 8 | private function getMethodSortFunc() { |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Returns the properties sort func |
||
| 248 | * |
||
| 249 | * @return \Closure |
||
| 250 | */ |
||
| 251 | 6 | private function getPropertySortFunc() { |
|
| 271 | } |
||
| 272 |