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 Point 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 Point, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Point |
||
15 | { |
||
16 | private static $delimiter = '|'; |
||
17 | |||
18 | /** @var int|float */ |
||
19 | private $x; |
||
20 | |||
21 | /** @var int|float */ |
||
22 | private $y; |
||
23 | |||
24 | /** |
||
25 | * @param int $x |
||
26 | * @param int $y |
||
27 | */ |
||
28 | public function __construct($x, $y) |
||
36 | |||
37 | /** |
||
38 | * @param string $hash |
||
39 | * |
||
40 | * @return Point |
||
41 | */ |
||
42 | public static function parse($hash) |
||
48 | |||
49 | /** |
||
50 | * @return string |
||
51 | */ |
||
52 | public function hash() |
||
56 | |||
57 | /** |
||
58 | * @return int|float |
||
59 | */ |
||
60 | public function getX() |
||
64 | |||
65 | /** |
||
66 | * @return int|float |
||
67 | */ |
||
68 | public function getY() |
||
72 | |||
73 | /** |
||
74 | * @return int|float |
||
75 | */ |
||
76 | public function length() |
||
80 | |||
81 | /** |
||
82 | * @return float |
||
83 | */ |
||
84 | public function distance() |
||
88 | |||
89 | /** |
||
90 | * @return Point |
||
91 | */ |
||
92 | public function left() |
||
96 | |||
97 | /** |
||
98 | * @return Point |
||
99 | */ |
||
100 | public function right() |
||
104 | |||
105 | /** |
||
106 | * @return Point |
||
107 | */ |
||
108 | public function up() |
||
112 | |||
113 | /** |
||
114 | * @return Point |
||
115 | */ |
||
116 | public function down() |
||
120 | |||
121 | /** |
||
122 | * @param Point $by |
||
123 | * |
||
124 | * @return Point |
||
125 | */ |
||
126 | public function move(Point $by) |
||
130 | |||
131 | /** |
||
132 | * @param Point $of |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | public function isLeft(Point $of) |
||
140 | |||
141 | /** |
||
142 | * @param Point $of |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | public function isRight(Point $of) |
||
150 | |||
151 | /** |
||
152 | * @param Point $of |
||
153 | * |
||
154 | * @return bool |
||
155 | */ |
||
156 | public function isAbove(Point $of) |
||
160 | |||
161 | /** |
||
162 | * @param Point $of |
||
163 | * |
||
164 | * @return bool |
||
165 | */ |
||
166 | public function isBelow(Point $of) |
||
170 | |||
171 | /** |
||
172 | * @param Point $as |
||
173 | * |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function sameX(Point $as) |
||
180 | |||
181 | /** |
||
182 | * @param Point $as |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | public function sameY(Point $as) |
||
190 | |||
191 | /** |
||
192 | * @param Point $other |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function equals(Point $other) |
||
200 | |||
201 | /** |
||
202 | * @param Point $a |
||
203 | * @param Point $b |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function betweenX(Point $a, Point $b) |
||
211 | |||
212 | /** |
||
213 | * @param Point $a |
||
214 | * @param Point $b |
||
215 | * |
||
216 | * @return bool |
||
217 | */ |
||
218 | public function betweenY(Point $a, Point $b) |
||
222 | |||
223 | /** |
||
224 | * @param Point|int $upTo |
||
225 | * @param int|float $step |
||
226 | * |
||
227 | * @return \Traversable |
||
228 | */ |
||
229 | View Code Duplication | public function forXUpTo($upTo, $step = 1) |
|
241 | |||
242 | /** |
||
243 | * @param Point|int $upTo |
||
244 | * @param int|float $step |
||
245 | * |
||
246 | * @return \Traversable |
||
247 | */ |
||
248 | View Code Duplication | public function forYUpTo($upTo, $step = 1) |
|
260 | |||
261 | /** |
||
262 | * @param int $count |
||
263 | * |
||
264 | * @return \Traversable |
||
265 | */ |
||
266 | public function forXTimes($count) |
||
276 | |||
277 | /** |
||
278 | * @param int $count |
||
279 | * |
||
280 | * @return \Traversable |
||
281 | */ |
||
282 | public function forYTimes($count) |
||
292 | } |
||
293 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.