Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Php54Features 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 Php54Features, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Php54Features extends LanguageFeatureAnalyser implements AnalyserAwareInterface |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * Closure nesting level |
||
| 42 | * |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | private $inClosureLevel = 0; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @inheritdoc |
||
| 49 | */ |
||
| 50 | 46 | public function enterNode(Node $node) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Leave node |
||
| 71 | * |
||
| 72 | * Currently only used to decrease closure level. |
||
| 73 | * |
||
| 74 | * @param \PhpParser\Node $node |
||
| 75 | * @return void |
||
| 76 | * @see Php54LanguageFeatureNodeWalker::$inClosureLevel Closure Level |
||
| 77 | */ |
||
| 78 | 44 | public function leaveNode(Node $node) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Wrapper for message addition in closure-this context |
||
| 87 | * |
||
| 88 | * @param \PhpParser\Node $node |
||
| 89 | */ |
||
| 90 | 22 | private function addThisInClosureRequirement(Node $node) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param \PhpParser\Node $node |
||
| 97 | */ |
||
| 98 | 44 | private function handleTraitFeatures(Node $node) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @param \PhpParser\Node $node |
||
| 120 | */ |
||
| 121 | 44 | private function handleFunctionDereferencing(Node $node) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param \PhpParser\Node $node |
||
| 136 | */ |
||
| 137 | 44 | private function handleCallableType(Node $node) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @param \PhpParser\Node $node |
||
| 158 | */ |
||
| 159 | 44 | private function handleInstantClassMemberAccess(Node $node) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @param \PhpParser\Node $node |
||
| 171 | */ |
||
| 172 | 44 | private function handleThisInClosure(Node $node) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param \PhpParser\Node $node |
||
| 191 | */ |
||
| 192 | 44 | private function handleBinaryNumberDeclaration(Node $node) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @param \PhpParser\Node $node |
||
| 208 | */ |
||
| 209 | 42 | private function handleShortArrayDeclaration(Node $node) |
|
| 215 | |||
| 216 | 42 | private function handleStaticCallByExpressionSyntax(Node $node) |
|
| 222 | |||
| 223 | 42 | private function detectShortEchoSyntax(Node $node) |
|
| 229 | } |
||
| 230 |