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:
1 | <?php |
||
22 | class ContainerBuilder |
||
23 | { |
||
24 | protected $expressionPaths = array(); |
||
25 | /** |
||
26 | * Constructor. |
||
27 | * |
||
28 | * @param array $config |
||
29 | */ |
||
30 | 11 | public function __construct($config) |
|
37 | |||
38 | |||
39 | /** |
||
40 | * Adds a callable to decide if a path should be an expression node. |
||
41 | * |
||
42 | * @param callable $callable |
||
43 | * @return void |
||
44 | */ |
||
45 | 1 | public function addExpressionPath($callable) |
|
49 | |||
50 | |||
51 | /** |
||
52 | * Decides if a node should be an expression. |
||
53 | * |
||
54 | * @param array $path |
||
55 | * @param mixed $node |
||
56 | * @return bool |
||
57 | */ |
||
58 | 10 | public function isExpressionPath($path, $node) |
|
69 | |||
70 | |||
71 | /** |
||
72 | * Build the container node |
||
73 | * |
||
74 | * @return ContainerNode |
||
75 | */ |
||
76 | 11 | public function build() |
|
89 | |||
90 | |||
91 | /** |
||
92 | * Creates the traverser that gathers all nodes (i.e. Node instances) that are specified in the tree. |
||
93 | * |
||
94 | * @param array $result |
||
95 | * @param \Zicht\Tool\Script\Node\Branch $containerNode |
||
96 | * @return Traverser |
||
97 | */ |
||
98 | 11 | public function createNodeGathererTraverser($result, $containerNode) |
|
112 | |||
113 | |||
114 | /** |
||
115 | * Creates a task node for the specified path |
||
116 | * |
||
117 | * @param array $path |
||
118 | * @param array $node |
||
119 | * @return Task |
||
120 | */ |
||
121 | 7 | public function createTaskNode($path, $node) |
|
125 | |||
126 | |||
127 | /** |
||
128 | * Creates a definition node for the specified path |
||
129 | * |
||
130 | * @param array $path |
||
131 | * @param array $node |
||
132 | * @return Definition |
||
133 | */ |
||
134 | 1 | public function createDefinitionNode($path, $node) |
|
138 | |||
139 | |||
140 | /** |
||
141 | * Creates a declaration node for the specified path |
||
142 | * |
||
143 | * @param array $path |
||
144 | * @param array $node |
||
145 | * @return Declaration |
||
146 | */ |
||
147 | 1 | public function createDeclarationNode($path, $node) |
|
151 | |||
152 | |||
153 | /** |
||
154 | * Creates a node for the 'args' definition of the task. |
||
155 | * |
||
156 | * @param array $path |
||
157 | * @param string $node |
||
158 | * @return \Zicht\Tool\Script\Node\Task\ArgNode |
||
159 | */ |
||
160 | 2 | public function createArgNode($path, $node) |
|
171 | |||
172 | /** |
||
173 | * Creates a node for the 'opts' definition of the task. |
||
174 | * |
||
175 | * @param array $path |
||
176 | * @param string $node |
||
177 | * @return \Zicht\Tool\Script\Node\Task\OptNode |
||
178 | */ |
||
179 | public function createOptNode($path, $node) |
||
183 | |||
184 | |||
185 | /** |
||
186 | * Creates a node for the 'set' definition of the task. |
||
187 | * |
||
188 | * @param array $path |
||
189 | * @param string $node |
||
190 | * @return SetNode |
||
191 | */ |
||
192 | public function createSetNode($path, $node) |
||
196 | |||
197 | |||
198 | /** |
||
199 | * Creates an expression node at the specified path. |
||
200 | * |
||
201 | * @param array $path |
||
202 | * @param array $node |
||
203 | * @return \Zicht\Tool\Script\Node\Node |
||
204 | */ |
||
205 | 3 | public function createExpressionNode($path, $node) |
|
209 | |||
210 | |||
211 | /** |
||
212 | * Creates a script node at the specified path. |
||
213 | * |
||
214 | * @param array $path |
||
215 | * @param array $node |
||
216 | * @return \Zicht\Tool\Script\Node\Node |
||
217 | */ |
||
218 | 1 | public function createScriptNode($path, $node) |
|
222 | |||
223 | |||
224 | /** |
||
225 | * Creates the traverser that creates relevant nodes at all known paths. |
||
226 | * |
||
227 | * @param array $config |
||
228 | * @return Traverser |
||
229 | */ |
||
230 | 11 | public function createNodeCreatorTraverser($config) |
|
300 | } |
||
301 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: