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 Path 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 Path, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Path extends Shape implements ContainerInterface |
||
18 | { |
||
19 | use ElementTrait; |
||
20 | |||
21 | /** |
||
22 | * The path points positions |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | public $data; |
||
27 | |||
28 | /** |
||
29 | * Path constructor. |
||
30 | * |
||
31 | * @param ElementInterface $parent |
||
32 | * @param float $x |
||
33 | * @param float $y |
||
34 | * @param bool $absolute |
||
35 | */ |
||
36 | public function __construct(ElementInterface $parent, $x, $y, $absolute = true) |
||
42 | |||
43 | /** |
||
44 | * Start a new sub-path at the given (x,y) coordinate. M (uppercase) indicates that absolute coordinates will |
||
45 | * follow; m (lowercase) indicates that relative coordinates will follow. If a moveto is followed by multiple pairs |
||
46 | * of coordinates, the subsequent pairs are treated as implicit lineto commands. Hence, implicit lineto commands |
||
47 | * will be relative if the moveto is relative, and absolute if the moveto is absolute. If a relative moveto (m) |
||
48 | * appears as the first element of the path, then it is treated as a pair of absolute coordinates. In this case, |
||
49 | * subsequent pairs of coordinates are treated as relative even though the initial moveto is interpreted as an |
||
50 | * absolute moveto. |
||
51 | * |
||
52 | * @link https://www.w3.org/TR/SVG/paths.html#PathDataMovetoCommands |
||
53 | * |
||
54 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
55 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
56 | * @param bool $absolute |
||
57 | * |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function moveTo($x, $y, $absolute = true) |
||
66 | |||
67 | /** |
||
68 | * @param string $type |
||
69 | * @param mixed $params |
||
70 | */ |
||
71 | private function buildPath($type, $params = null) |
||
98 | |||
99 | private function addData($type, array $params) |
||
103 | |||
104 | /** |
||
105 | * Draw a line from the current point to the given (x,y) coordinate which becomes the new current point. L |
||
106 | * (uppercase) indicates that absolute coordinates will follow; l (lowercase) indicates that relative coordinates |
||
107 | * will follow. A number of coordinates pairs may be specified to draw a polyline. At the end of the command, the |
||
108 | * new current point is set to the final set of coordinates provided. |
||
109 | * |
||
110 | * @link https://www.w3.org/TR/SVG/paths.html#PathDataLinetoCommands |
||
111 | * |
||
112 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
113 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
114 | * @param bool $absolute |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function lineTo($x, $y, $absolute = true) |
||
124 | |||
125 | /** |
||
126 | * Draws a horizontal line from the current point (cpx, cpy) to (x, cpy). H (uppercase) indicates that absolute |
||
127 | * coordinates will follow; h (lowercase) indicates that relative coordinates will follow. Multiple x values can be |
||
128 | * provided (although usually this doesn't make sense). At the end of the command, the new current point becomes |
||
129 | * (x, cpy) for the final value of x. |
||
130 | * |
||
131 | * @link https://www.w3.org/TR/SVG/paths.html#PathDataLinetoCommands |
||
132 | * |
||
133 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
134 | * |
||
135 | * @param bool $absolute |
||
136 | * |
||
137 | * @return $this |
||
138 | */ |
||
139 | public function hLineTo($x, $absolute = true) |
||
145 | |||
146 | /** |
||
147 | * Draws a vertical line from the current point (cpx, cpy) to (cpx, y). V (uppercase) indicates that absolute |
||
148 | * coordinates will follow; v (lowercase) indicates that relative coordinates will follow. Multiple y values can be |
||
149 | * provided (although usually this doesn't make sense). At the end of the command, the new current point becomes |
||
150 | * (cpx, y) for the final value of y. |
||
151 | * |
||
152 | * @link https://www.w3.org/TR/SVG/paths.html#PathDataLinetoCommands |
||
153 | * |
||
154 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
155 | * |
||
156 | * @param bool $absolute |
||
157 | * |
||
158 | * @return $this |
||
159 | */ |
||
160 | public function vLineTo($y, $absolute = true) |
||
166 | |||
167 | /** |
||
168 | * Draws a cubic Bézier curve from the current point to (x,y) using (x1,y1) as the control point at the beginning |
||
169 | * of the curve and (x2,y2) as the control point at the end of the curve. |
||
170 | * |
||
171 | * @link https://www.w3.org/TR/SVG11/paths.html#PathDataCurveCommands |
||
172 | * |
||
173 | * @param float $x1 The absolute (relative) X coordinate for the first control point. |
||
174 | * @param float $y1 The absolute (relative) Y coordinate for the first control point. |
||
175 | * @param float $x2 The absolute (relative) X coordinate for the second control point. |
||
176 | * @param float $y2 The absolute (relative) Y coordinate for the second control point. |
||
177 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
178 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
179 | * |
||
180 | * @param bool $absolute |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | public function curveTo($x1, $y1, $x2, $y2, $x, $y, $absolute = true) |
||
190 | |||
191 | /** |
||
192 | * Draws a cubic Bézier curve from the current point to (x,y). The first control point is assumed to be the |
||
193 | * reflection of the second control point on the previous command relative to the current point. (If there is no |
||
194 | * previous command or if the previous command was not an C, c, S or s, assume the first control point is |
||
195 | * coincident with the current point.) (x2,y2) is the second control point (i.e., the control point at the end of |
||
196 | * the curve). S (uppercase) indicates that absolute coordinates will follow; s (lowercase) indicates that relative |
||
197 | * coordinates will follow. Multiple sets of coordinates may be specified to draw a polybézier. At the end of the |
||
198 | * command, the new current point becomes the final (x,y) coordinate pair used in the polybézier. |
||
199 | * |
||
200 | * @link https://www.w3.org/TR/SVG11/paths.html#PathDataCurveCommands |
||
201 | * |
||
202 | * @param float $x2 The absolute (relative) X coordinate for the second control point. |
||
203 | * @param float $y2 The absolute (relative) Y coordinate for the second control point. |
||
204 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
205 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
206 | * |
||
207 | * @param bool $absolute |
||
208 | * |
||
209 | * @return $this |
||
210 | */ |
||
211 | public function smoothCurveTo($x2, $y2, $x, $y, $absolute = true) |
||
217 | |||
218 | /** |
||
219 | * Draws a quadratic Bézier curve from the current point to (x,y) using (x1,y1) as the control point. Q (uppercase) |
||
220 | * indicates that absolute coordinates will follow; q (lowercase) indicates that relative coordinates will follow. |
||
221 | * Multiple sets of coordinates may be specified to draw a polybézier. At the end of the command, the new current |
||
222 | * point becomes the final (x,y) coordinate pair used in the polybézier. |
||
223 | * |
||
224 | * @link https://www.w3.org/TR/SVG/paths.html#PathDataQuadraticBezierCommands |
||
225 | * |
||
226 | * @param float $x1 The absolute (relative) X coordinate for the first control point. |
||
227 | * @param float $y1 The absolute (relative) Y coordinate for the first control point. |
||
228 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
229 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
230 | * |
||
231 | * @param bool $absolute |
||
232 | * |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function quadraticCurveTo($x1, $y1, $x, $y, $absolute = true) |
||
241 | |||
242 | /** |
||
243 | * Draws a quadratic Bézier curve from the current point to (x,y). The control point is assumed to be the |
||
244 | * reflection of the control point on the previous command relative to the current point. (If there is no previous |
||
245 | * command or if the previous command was not a Q, q, T or t, assume the control point is coincident with the |
||
246 | * current point.) T (uppercase) indicates that absolute coordinates will follow; t (lowercase) indicates that |
||
247 | * relative coordinates will follow. At the end of the command, the new current point becomes the final (x,y) |
||
248 | * coordinate pair used in the polybézier. |
||
249 | * |
||
250 | * @link https://www.w3.org/TR/SVG/paths.html#PathDataQuadraticBezierCommands |
||
251 | * |
||
252 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
253 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
254 | * |
||
255 | * @param bool $absolute |
||
256 | * |
||
257 | * @return $this |
||
258 | */ |
||
259 | public function smoothQuadraticCurveTo($x, $y, $absolute = true) |
||
265 | |||
266 | /** |
||
267 | * Draws an elliptical arc from the current point to (x, y). The size and orientation of the ellipse are defined by |
||
268 | * two radii (rx, ry) and an x-axis-rotation, which indicates how the ellipse as a whole is rotated relative to the |
||
269 | * current coordinate system. The center (cx, cy) of the ellipse is calculated automatically to satisfy the |
||
270 | * constraints imposed by the other parameters. large-arc-flag and sweep-flag contribute to the automatic |
||
271 | * calculations and help determine how the arc is drawn. |
||
272 | * |
||
273 | * @link https://www.w3.org/TR/SVG11/paths.html#PathDataEllipticalArcCommands |
||
274 | * |
||
275 | * @param float $rx The x-axis radius for the ellipse (i.e., r1). |
||
276 | * @param float $ry The y-axis radius for the ellipse |
||
277 | * @param float $xRotation The rotation angle in degrees for the ellipse's x-axis relative to the x-axis of |
||
278 | * the user coordinate system. |
||
279 | * @param boolean $largeArcFlag The value of the large-arc-flag parameter. |
||
280 | * @param boolean $sweepFlag The value of the sweep-flag parameter. |
||
281 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
282 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
283 | * @param bool $absolute |
||
284 | * |
||
285 | * @return $this |
||
286 | */ |
||
287 | public function arcTo($rx, $ry, $xRotation, $largeArcFlag, $sweepFlag, $x, $y, $absolute = true) |
||
293 | |||
294 | /** |
||
295 | * Close the current subpath by drawing a straight line from the current point to current subpath's initial point. |
||
296 | * Since the Z and z commands take no parameters, they have an identical effect. |
||
297 | * |
||
298 | * @link https://www.w3.org/TR/SVG/paths.html#PathDataClosePathCommand |
||
299 | * |
||
300 | * @param bool $absolute |
||
301 | */ |
||
302 | public function closePath($absolute = true) |
||
306 | |||
307 | public function getName() |
||
311 | |||
312 | public function getBoundingBox() |
||
380 | |||
381 | protected function getCenterX() |
||
385 | |||
386 | protected function getCenterY() |
||
390 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.