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 Php53Features 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 Php53Features, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class Php53Features extends LanguageFeatureAnalyser |
||
39 | { |
||
40 | /** |
||
41 | * Stores the current in class state of this NodeVisitor. |
||
42 | * This is required to detect the usage of doc-syntax usaged within class constant |
||
43 | * definitions. |
||
44 | * |
||
45 | * @var bool |
||
46 | */ |
||
47 | private $inClass = false; |
||
48 | |||
49 | /** |
||
50 | * @inheritdoc |
||
51 | */ |
||
52 | 56 | public function enterNode(Node $node) |
|
53 | { |
||
54 | 56 | if ($this->isClassDeclarationStatement($node)) { |
|
55 | 32 | $this->inClass = true; |
|
56 | 16 | } |
|
57 | 56 | if ($this->mode & self::MODE_ADDITION) { |
|
58 | 52 | $this->detectGotoKeywordAndJumpLabel($node); |
|
59 | 52 | $this->detectNamespaces($node); |
|
60 | 52 | $this->detectNowDoc($node); |
|
61 | 52 | $this->detectNewMagicDefinitions($node); |
|
62 | 52 | $this->detectDocFormatConstantInitializationAndConstOutsideClass($node); |
|
63 | 52 | $this->detectShortHandTernary($node); |
|
64 | 52 | $this->detectClosures($node); |
|
65 | 52 | $this->detectDynamicAccessToStatic($node); |
|
66 | 52 | $this->detectLateStateBinding($node); |
|
67 | 26 | } |
|
68 | 56 | if ($this->mode & self::MODE_DEPRECATION) { |
|
69 | 24 | $this->detectNewByReference($node); |
|
70 | 12 | } |
|
71 | 56 | } |
|
72 | |||
73 | /** |
||
74 | * @inheritdoc |
||
75 | */ |
||
76 | 56 | public function leaveNode(Node $node) |
|
82 | |||
83 | /** |
||
84 | * @param \PhpParser\Node $node |
||
85 | * @return bool |
||
86 | */ |
||
87 | 56 | private function isClassDeclarationStatement(Node $node) |
|
91 | |||
92 | /** |
||
93 | * @param \PhpParser\Node $node |
||
94 | */ |
||
95 | 52 | private function detectGotoKeywordAndJumpLabel(Node $node) |
|
113 | |||
114 | /** |
||
115 | * @param \PhpParser\Node $node |
||
116 | */ |
||
117 | 52 | private function detectNamespaces(Node $node) |
|
137 | |||
138 | /** |
||
139 | * @param \PhpParser\Node $node |
||
140 | */ |
||
141 | 52 | private function detectNowDoc(Node $node) |
|
147 | |||
148 | /** |
||
149 | * @param \PhpParser\Node $node |
||
150 | */ |
||
151 | 52 | private function detectNewMagicDefinitions(Node $node) |
|
166 | |||
167 | /** |
||
168 | * @param \PhpParser\Node $node |
||
169 | */ |
||
170 | 52 | private function detectDocFormatConstantInitializationAndConstOutsideClass(Node $node) |
|
190 | |||
191 | /** |
||
192 | * @param \PhpParser\Node $node |
||
193 | */ |
||
194 | 52 | private function detectShortHandTernary(Node $node) |
|
200 | |||
201 | /** |
||
202 | * @param \PhpParser\Node $node |
||
203 | */ |
||
204 | 52 | private function detectClosures(Node $node) |
|
210 | |||
211 | /** |
||
212 | * @param \PhpParser\Node $node |
||
213 | */ |
||
214 | 52 | private function detectDynamicAccessToStatic(Node $node) |
|
222 | |||
223 | /** |
||
224 | * @param \PhpParser\Node $node |
||
225 | */ |
||
226 | 52 | private function detectLateStateBinding(Node $node) |
|
234 | |||
235 | /** |
||
236 | * @param \PhpParser\Node $node |
||
237 | */ |
||
238 | 24 | private function detectNewByReference(Node $node) |
|
244 | } |
||
245 |