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 DotKey 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 DotKey, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class DotKey |
||
9 | { |
||
10 | /** |
||
11 | * @var object|array |
||
12 | */ |
||
13 | protected $item; |
||
14 | |||
15 | /** |
||
16 | * Create new structure as array (when using put) |
||
17 | * @var boolean |
||
18 | */ |
||
19 | public $assoc; |
||
20 | |||
21 | /** |
||
22 | * Class constructor |
||
23 | * |
||
24 | * @param object|array $item |
||
25 | * @param boolean $assoc Create new structure as array (when using put) |
||
26 | */ |
||
27 | public function __construct($item, $assoc = null) |
||
32 | |||
33 | |||
34 | /** |
||
35 | * Check if property exists |
||
36 | * |
||
37 | * @param string $key The index to fetch in dot notation |
||
38 | * @return boolean |
||
39 | */ |
||
40 | public function exists($key) |
||
47 | |||
48 | /** |
||
49 | * Get a value |
||
50 | * |
||
51 | * @param string $key The index to fetch in dot notation |
||
52 | * @return mixed |
||
53 | */ |
||
54 | View Code Duplication | public function get($key) |
|
66 | |||
67 | /** |
||
68 | * Navigate through the item and get the value |
||
69 | * |
||
70 | * @param array $item |
||
71 | * @param array $index The index sequence we are navigating to |
||
72 | * @param array $ignore Don't raise an error if not exists |
||
73 | * @param object $err Error object [OUTPUT] |
||
74 | * @return mixed |
||
75 | */ |
||
76 | protected static function getValue($item, $index, $ignore = false, &$err = null) |
||
98 | |||
99 | |||
100 | /** |
||
101 | * Set a value |
||
102 | * |
||
103 | * @param string $key The index to fetch in dot notation |
||
104 | * @param mixed $value |
||
105 | * @return object|array |
||
106 | */ |
||
107 | public function set($key, $value) |
||
120 | |||
121 | /** |
||
122 | * Set a value, creating the structure if required |
||
123 | * |
||
124 | * @param string $key The index to fetch in dot notation |
||
125 | * @param mixed $value |
||
126 | * @return object|array |
||
127 | */ |
||
128 | public function put($key, $value) |
||
142 | |||
143 | /** |
||
144 | * Navigate through the item and set the value |
||
145 | * |
||
146 | * @param array $item |
||
147 | * @param array $index The index sequence we are navigating to |
||
148 | * @param mixed $value |
||
149 | * @param string|false $create Create structure if required: 'object', 'array' or false |
||
150 | * @param object $err Error object [OUTPUT] |
||
151 | */ |
||
152 | protected static function setValue(&$item, $index, $value, $create = false, &$err = null) |
||
191 | |||
192 | |||
193 | /** |
||
194 | * Get a particular value back from the config array |
||
195 | * |
||
196 | * @param string $key The index to fetch in dot notation |
||
197 | * @return object|array |
||
198 | */ |
||
199 | View Code Duplication | public function remove($key) |
|
211 | |||
212 | /** |
||
213 | * Navigate through the item and remove the value |
||
214 | * |
||
215 | * @param array $item |
||
216 | * @param array $index The index sequence we are navigating to |
||
217 | * @param object $err Error object [OUTPUT] |
||
218 | * @return mixed |
||
219 | */ |
||
220 | protected static function removeValue(&$item, $index, &$err = null) |
||
240 | |||
241 | |||
242 | /** |
||
243 | * Factory method |
||
244 | * |
||
245 | * @param object|array $item |
||
246 | * @param boolean $assoc Create new structure as array (when using put) |
||
247 | */ |
||
248 | public static function on($item, $assoc = null) |
||
252 | } |
||
253 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.