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 Data 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 Data, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Data implements IDataProvider |
||
26 | { |
||
27 | /** |
||
28 | * Data array. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $data = array(); |
||
33 | |||
34 | /** |
||
35 | * Returns the data array. |
||
36 | * |
||
37 | * @return array |
||
38 | */ |
||
39 | public function getData() |
||
43 | |||
44 | /** |
||
45 | * Clears a the entire data or only the given key. |
||
46 | * |
||
47 | * @param array|string $name clears only one value if you give a name, multiple values if |
||
48 | * you give an array of names, or the entire data if left null |
||
49 | */ |
||
50 | public function clear($name = null) |
||
62 | |||
63 | /** |
||
64 | * Overwrites the entire data with the given array. |
||
65 | * |
||
66 | * @param array $data the new data array to use |
||
67 | */ |
||
68 | public function setData(array $data) |
||
72 | |||
73 | /** |
||
74 | * merges the given array(s) with the current data with array_merge. |
||
75 | * |
||
76 | * @param array $data the array to merge |
||
77 | */ |
||
78 | public function mergeData(array $data) |
||
87 | |||
88 | /** |
||
89 | * Assigns a value or an array of values to the data object. |
||
90 | * |
||
91 | * @param array|string $name an associative array of multiple (index=>value) or a string |
||
92 | * that is the index to use, i.e. a value assigned to "foo" will be |
||
93 | * accessible in the template through {$foo} |
||
94 | * @param mixed $val the value to assign, or null if $name was an array |
||
95 | */ |
||
96 | public function assign($name, $val = null) |
||
107 | |||
108 | /** |
||
109 | * Allows to assign variables using the object syntax. |
||
110 | * |
||
111 | * @param string $name the variable name |
||
112 | * @param string $value the value to assign to it |
||
113 | */ |
||
114 | public function __set($name, $value) |
||
118 | |||
119 | /** |
||
120 | * Assigns a value by reference to the data object. |
||
121 | * |
||
122 | * @param string $name the index to use, i.e. a value assigned to "foo" will be |
||
123 | * accessible in the template through {$foo} |
||
124 | * @param mixed $val the value to assign by reference |
||
125 | */ |
||
126 | public function assignByRef($name, &$val) |
||
130 | |||
131 | /** |
||
132 | * Appends values or an array of values to the data object. |
||
133 | * |
||
134 | * @param array|string $name an associative array of multiple (index=>value) or a string |
||
135 | * that is the index to use, i.e. a value assigned to "foo" will be |
||
136 | * accessible in the template through {$foo} |
||
137 | * @param mixed $val the value to assign, or null if $name was an array |
||
138 | * @param bool $merge true to merge data or false to append, defaults to false |
||
139 | */ |
||
140 | public function append($name, $val = null, $merge = false) |
||
168 | |||
169 | /** |
||
170 | * Appends a value by reference to the data object. |
||
171 | * |
||
172 | * @param string $name the index to use, i.e. a value assigned to "foo" will be |
||
173 | * accessible in the template through {$foo} |
||
174 | * @param mixed $val the value to append by reference |
||
175 | * @param bool $merge true to merge data or false to append, defaults to false |
||
176 | */ |
||
177 | public function appendByRef($name, &$val, $merge = false) |
||
191 | |||
192 | /** |
||
193 | * Returns true if the variable has been assigned already, false otherwise. |
||
194 | * |
||
195 | * @param string $name the variable name |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function isAssigned($name) |
||
203 | |||
204 | /** |
||
205 | * Supports calls to isset($dwoo->var). |
||
206 | * |
||
207 | * @param string $name the variable name |
||
208 | * |
||
209 | * @return bool |
||
210 | */ |
||
211 | public function __isset($name) |
||
215 | |||
216 | /** |
||
217 | * Unassigns/removes a variable. |
||
218 | * |
||
219 | * @param string $name the variable name |
||
220 | */ |
||
221 | public function unassign($name) |
||
225 | |||
226 | /** |
||
227 | * Supports unsetting variables using the object syntax. |
||
228 | * |
||
229 | * @param string $name the variable name |
||
230 | */ |
||
231 | public function __unset($name) |
||
235 | |||
236 | /** |
||
237 | * Returns a variable if it was assigned. |
||
238 | * |
||
239 | * @param string $name the variable name |
||
240 | * |
||
241 | * @return mixed |
||
242 | */ |
||
243 | public function get($name) |
||
247 | |||
248 | /** |
||
249 | * Allows to read variables using the object syntax. |
||
250 | * |
||
251 | * @param string $name the variable name |
||
252 | * |
||
253 | * @return mixed |
||
254 | * @throws Exception |
||
255 | */ |
||
256 | public function __get($name) |
||
264 | } |
||
265 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.