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 |
||
21 | class ODataParams |
||
22 | { |
||
23 | /** |
||
24 | * The ODataFilter or false if not set |
||
25 | * @var false|Data\Filter |
||
26 | */ |
||
27 | public $filter = false; |
||
28 | /** |
||
29 | * An array of properties to expand or false if not set |
||
30 | * @var false|array |
||
31 | */ |
||
32 | public $expand = false; |
||
33 | /** |
||
34 | * An array of properties to display or false if not set |
||
35 | * @var false|array |
||
36 | */ |
||
37 | public $select = false; |
||
38 | /** |
||
39 | * An array of properties to sort by or false if not set |
||
40 | * @var false|array |
||
41 | */ |
||
42 | public $orderby = false; |
||
43 | /** |
||
44 | * The number of results to display or false if not set |
||
45 | * @var false|integer |
||
46 | */ |
||
47 | public $top = false; |
||
48 | /** |
||
49 | * The number of results to skip or false if not set |
||
50 | * @var false|integer |
||
51 | */ |
||
52 | public $skip = false; |
||
53 | /** |
||
54 | * Display the count of results |
||
55 | * @var boolean |
||
56 | */ |
||
57 | public $count = false; |
||
58 | /** |
||
59 | * Not yet implemented |
||
60 | */ |
||
61 | public $search = false; |
||
62 | |||
63 | /** |
||
64 | * Parse the parameter array into an ODataParams instance |
||
65 | * |
||
66 | * @param string[] $params An key=>value array of strings representing the query string. |
||
67 | */ |
||
68 | public function __construct($params) |
||
79 | |||
80 | /** |
||
81 | * Take the parameter array and find the Filter parameter and convert that to a \Data\Filter if present |
||
82 | * |
||
83 | * @param string[] $params An key=>value array of strings representing the query string. |
||
84 | */ |
||
85 | protected function processFilter($params) |
||
96 | |||
97 | /** |
||
98 | * Take the parameter array and find the Expand parameter and convert it to a PHP array |
||
99 | * |
||
100 | * @param string[] $params An key=>value array of strings representing the query string. |
||
101 | */ |
||
102 | protected function processExpand($params) |
||
109 | |||
110 | /** |
||
111 | * Take the parameter array and find the Select parameter and convert it to a PHP array |
||
112 | * |
||
113 | * @param string[] $params An key=>value array of strings representing the query string. |
||
114 | */ |
||
115 | protected function processSelect($params) |
||
126 | |||
127 | /** |
||
128 | * Take the parameter array and find the OrderBy parameter and convert it to a PHP array |
||
129 | * |
||
130 | * @param string[] $params An key=>value array of strings representing the query string. |
||
131 | */ |
||
132 | protected function processOrderBy($params) |
||
164 | |||
165 | /** |
||
166 | * Take the parameter array and find the Top parameter and convert it to an int |
||
167 | * |
||
168 | * @param string[] $params An key=>value array of strings representing the query string. |
||
169 | */ |
||
170 | protected function processTop($params) |
||
177 | |||
178 | /** |
||
179 | * Take the parameter array and find the Skip parameter and convert it to an int |
||
180 | * |
||
181 | * @param string[] $params An key=>value array of strings representing the query string. |
||
182 | */ |
||
183 | protected function processSkip($params) |
||
190 | |||
191 | /** |
||
192 | * Take the parameter array and find the Count parameter and convert it to a boolean |
||
193 | * |
||
194 | * @param string[] $params An key=>value array of strings representing the query string. |
||
195 | */ |
||
196 | protected function processCount($params) |
||
203 | |||
204 | /** |
||
205 | * Take the parameter array and find the Search parameter and process it |
||
206 | * |
||
207 | * @param string[] $params An key=>value array of strings representing the query string. |
||
208 | */ |
||
209 | protected function processSearch($params) |
||
216 | |||
217 | /** |
||
218 | * Take an input array and filter the array based on the select parameter |
||
219 | * |
||
220 | * @param array $array The array to be filtered |
||
221 | * |
||
222 | * @return array The filtered array |
||
223 | */ |
||
224 | public function filterArrayPerSelect($array) |
||
243 | } |
||
244 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: