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 Transform 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 Transform, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Transform implements TransformInterface |
||
|
|||
12 | { |
||
13 | private $trans; |
||
14 | |||
15 | private $data; |
||
16 | |||
17 | private $sequence; |
||
18 | |||
19 | private $argDelimiter = TransformInterface::ARG_DELIM_SPACE; |
||
20 | |||
21 | const ROTATE_PATTERN = "/rotate\s*\(\s*(?<a>[+-]?\d+(?:\.\d+)?)((?:\s{1,}\,?\s*|\,\s*)(?<x>[+-]?\d+(?:\.\d+)?)(?:\s{1,}\,?\s*|\,\s*)(?<y>[+-]?\d+(?:\.\d+)?))?\s*\)/"; |
||
22 | |||
23 | const TRANSLATE_PATTERN = "/translate\s*\(\s*(?<x>[+-]?\d+(?:\.\d+)?)((?:\s{1,}\,?\s*|\,\s*)(?<y>[+-]?\d+(?:\.\d+)?))?\)/"; |
||
24 | |||
25 | const SCALE_PATTERN = "/scale\s*\(\s*(?<x>[+-]?\d+(?:\.\d+)?)((?:\s{1,}\,?\s*|\,\s*)(?<y>[+-]?\d+(?:\.\d+)?))?\)/"; |
||
26 | |||
27 | const SKEW_PATTERN = "/skew([XY])\s*\(\s*(?<x>[+-]?\d+(?:\.\d+)?)?\)/"; |
||
28 | |||
29 | const MATRIX_PATTERN = "/matrix\s*\(\s*((([+-]?\d+(?:\.\d+)?)(?:\s+,?\s*|,\s*)){5}([+-]?\d+(?:\.\d+)?)\s*)\)/"; |
||
30 | |||
31 | private function __construct() |
||
34 | |||
35 | /** |
||
36 | * Use this method to instantiate Transform class. |
||
37 | * |
||
38 | * @param $transformString |
||
39 | * |
||
40 | * @uses matchRotate() |
||
41 | * @uses matchSkewX() |
||
42 | * @uses matchSkewY() |
||
43 | * @uses matchTranslate() |
||
44 | * @uses matchScale() |
||
45 | * @uses matchMatrix() |
||
46 | * |
||
47 | * @return Transform |
||
48 | */ |
||
49 | public static function newInstance($transformString = null) |
||
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | public function result() |
||
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | public function setArgumentDelimiter($delim) |
||
85 | |||
86 | /** |
||
87 | * @inheritdoc |
||
88 | */ |
||
89 | public function rotate($angle, $cx = null, $cy = null) |
||
104 | |||
105 | /** |
||
106 | * @inheritdoc |
||
107 | */ |
||
108 | View Code Duplication | public function translate($x, $y = null) |
|
116 | |||
117 | /** |
||
118 | * @inheritdoc |
||
119 | */ |
||
120 | View Code Duplication | public function scale($x, $y = null) |
|
128 | |||
129 | /** |
||
130 | * @inheritdoc |
||
131 | */ |
||
132 | public function skewX($x) |
||
140 | |||
141 | /** |
||
142 | * @inheritdoc |
||
143 | */ |
||
144 | public function skewY($y) |
||
152 | |||
153 | /** |
||
154 | * @inheritdoc |
||
155 | */ |
||
156 | public function matrix(array $matrix) |
||
168 | |||
169 | /** |
||
170 | * @return mixed |
||
171 | */ |
||
172 | View Code Duplication | private function matchRotate() |
|
182 | |||
183 | private function matchSkewX() |
||
187 | |||
188 | private function matchSkewY() |
||
192 | |||
193 | private function matchSkew() |
||
201 | |||
202 | View Code Duplication | private function matchScale() |
|
210 | |||
211 | private function matchMatrix() |
||
222 | |||
223 | View Code Duplication | private function matchTranslate() |
|
231 | |||
232 | /** |
||
233 | * @return array |
||
234 | */ |
||
235 | public function getData() |
||
239 | |||
240 | private function hasTransform($transform) |
||
244 | |||
245 | private function getTransform($transform) |
||
253 | |||
254 | private function makeSequence() |
||
260 | |||
261 | private function addTransformSequence($transform) |
||
265 | |||
266 | private function buildTransformString() |
||
276 | |||
277 | private function setTransformData($transform, $data) |
||
289 | |||
290 | private function addTransformIfNeeded($transform) |
||
296 | } |