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 |
||
20 | class ODataParams |
||
21 | { |
||
22 | /** |
||
23 | * The ODataFilter or false if not set |
||
24 | * @var false|Data\Filter |
||
25 | */ |
||
26 | public $filter = false; |
||
27 | /** |
||
28 | * An array of properties to expand or false if not set |
||
29 | * @var false|array |
||
30 | */ |
||
31 | public $expand = false; |
||
32 | /** |
||
33 | * An array of properties to display or false if not set |
||
34 | * @var false|array |
||
35 | */ |
||
36 | public $select = false; |
||
37 | /** |
||
38 | * An array of properties to sort by or false if not set |
||
39 | * @var false|array |
||
40 | */ |
||
41 | public $orderby = false; |
||
42 | /** |
||
43 | * The number of results to display or false if not set |
||
44 | * @var false|integer |
||
45 | */ |
||
46 | public $top = false; |
||
47 | /** |
||
48 | * The number of results to skip or false if not set |
||
49 | * @var false|integer |
||
50 | */ |
||
51 | public $skip = false; |
||
52 | /** |
||
53 | * Display the count of results |
||
54 | * @var boolean |
||
55 | */ |
||
56 | public $count = false; |
||
57 | /** |
||
58 | * Not yet implemented |
||
59 | */ |
||
60 | public $search = false; |
||
61 | |||
62 | /** |
||
63 | * Parse the parameter array into an ODataParams instance |
||
64 | * |
||
65 | * @param string[] $params An key=>value array of strings representing the query string. |
||
66 | */ |
||
67 | public function __construct($params) |
||
78 | |||
79 | /** |
||
80 | * Take the parameter array and find the Filter parameter and convert that to a \Data\Filter if present |
||
81 | * |
||
82 | * @param string[] $params An key=>value array of strings representing the query string. |
||
83 | */ |
||
84 | protected function processFilter($params) |
||
85 | { |
||
86 | View Code Duplication | if(isset($params['filter'])) |
|
87 | { |
||
88 | $this->filter = new \Data\Filter($params['filter']); |
||
89 | } |
||
90 | else if(isset($params['$filter'])) |
||
91 | { |
||
92 | $this->filter = new \Data\Filter($params['$filter']); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Take the parameter array and find the Expand parameter and convert it to a PHP array |
||
98 | * |
||
99 | * @param string[] $params An key=>value array of strings representing the query string. |
||
100 | */ |
||
101 | protected function processExpand($params) |
||
108 | |||
109 | /** |
||
110 | * Take the parameter array and find the Select parameter and convert it to a PHP array |
||
111 | * |
||
112 | * @param string[] $params An key=>value array of strings representing the query string. |
||
113 | */ |
||
114 | protected function processSelect($params) |
||
125 | |||
126 | /** |
||
127 | * Take the parameter array and find the OrderBy parameter and convert it to a PHP array |
||
128 | * |
||
129 | * @param string[] $params An key=>value array of strings representing the query string. |
||
130 | */ |
||
131 | protected function processOrderBy($params) |
||
163 | |||
164 | /** |
||
165 | * Take the parameter array and find the Top parameter and convert it to an int |
||
166 | * |
||
167 | * @param string[] $params An key=>value array of strings representing the query string. |
||
168 | */ |
||
169 | protected function processTop($params) |
||
176 | |||
177 | /** |
||
178 | * Take the parameter array and find the Skip parameter and convert it to an int |
||
179 | * |
||
180 | * @param string[] $params An key=>value array of strings representing the query string. |
||
181 | */ |
||
182 | protected function processSkip($params) |
||
189 | |||
190 | /** |
||
191 | * Take the parameter array and find the Count parameter and convert it to a boolean |
||
192 | * |
||
193 | * @param string[] $params An key=>value array of strings representing the query string. |
||
194 | */ |
||
195 | protected function processCount($params) |
||
202 | |||
203 | /** |
||
204 | * Take the parameter array and find the Search parameter and process it |
||
205 | * |
||
206 | * @param string[] $params An key=>value array of strings representing the query string. |
||
207 | */ |
||
208 | protected function processSearch($params) |
||
215 | |||
216 | /** |
||
217 | * Take an input array and filter the array based on the select parameter |
||
218 | * |
||
219 | * @param array $array The array to be filtered |
||
220 | * |
||
221 | * @return array The filtered array |
||
222 | */ |
||
223 | public function filterArrayPerSelect($array) |
||
242 | } |
||
243 |
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: