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 |
||
| 16 | class Path extends Shape implements ContainerInterface |
||
| 17 | { |
||
| 18 | use ElementTrait; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var PathBounds |
||
| 22 | */ |
||
| 23 | private $boundingBox; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Path constructor. |
||
| 27 | * |
||
| 28 | * @param ElementInterface $parent |
||
| 29 | * @param float $x |
||
| 30 | * @param float $y |
||
| 31 | * @param bool $absolute |
||
| 32 | */ |
||
| 33 | 11 | public function __construct(ElementInterface $parent, $x, $y, $absolute = true) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Start a new sub-path at the given (x,y) coordinate. M (uppercase) indicates that absolute coordinates will |
||
| 43 | * follow; m (lowercase) indicates that relative coordinates will follow. If a moveto is followed by multiple pairs |
||
| 44 | * of coordinates, the subsequent pairs are treated as implicit lineto commands. Hence, implicit lineto commands |
||
| 45 | * will be relative if the moveto is relative, and absolute if the moveto is absolute. If a relative moveto (m) |
||
| 46 | * appears as the first element of the path, then it is treated as a pair of absolute coordinates. In this case, |
||
| 47 | * subsequent pairs of coordinates are treated as relative even though the initial moveto is interpreted as an |
||
| 48 | * absolute moveto. |
||
| 49 | * |
||
| 50 | * @link https://www.w3.org/TR/SVG/paths.html#PathDataMovetoCommands |
||
| 51 | * |
||
| 52 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
| 53 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
| 54 | * @param bool $absolute |
||
| 55 | * |
||
| 56 | * @return $this |
||
| 57 | */ |
||
| 58 | 11 | public function moveTo($x, $y, $absolute = true) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @param ElementInterface $parent |
||
| 67 | * @param $x0 |
||
| 68 | * @param $y0 |
||
| 69 | * @param $x1 |
||
| 70 | * @param $y1 |
||
| 71 | * @param $x |
||
| 72 | * @param $y |
||
| 73 | * @param bool $absolute |
||
| 74 | * |
||
| 75 | * @return Path|ContainerInterface |
||
| 76 | */ |
||
| 77 | public static function quadraticCurve(ElementInterface $parent, $x0, $y0, $x1, $y1, $x, $y, $absolute = true) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Draws a quadratic Bézier curve from the current point to (x,y) using (x1,y1) as the control point. Q (uppercase) |
||
| 85 | * indicates that absolute coordinates will follow; q (lowercase) indicates that relative coordinates will follow. |
||
| 86 | * Multiple sets of coordinates may be specified to draw a polybézier. At the end of the command, the new current |
||
| 87 | * point becomes the final (x,y) coordinate pair used in the polybézier. |
||
| 88 | * |
||
| 89 | * @link https://www.w3.org/TR/SVG/paths.html#PathDataQuadraticBezierCommands |
||
| 90 | * |
||
| 91 | * @param float $x1 The absolute (relative) X coordinate for the first control point. |
||
| 92 | * @param float $y1 The absolute (relative) Y coordinate for the first control point. |
||
| 93 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
| 94 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
| 95 | * |
||
| 96 | * @param bool $absolute |
||
| 97 | * |
||
| 98 | * @return $this |
||
| 99 | */ |
||
| 100 | 1 | public function quadraticCurveTo($x1, $y1, $x, $y, $absolute = true) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $type |
||
| 109 | */ |
||
| 110 | 11 | private function buildPath($type) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @param $value |
||
| 131 | */ |
||
| 132 | 4 | private function addArrayToPath(array $value) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @param ElementInterface $parent |
||
| 142 | * @param $x |
||
| 143 | * @param $y |
||
| 144 | * @param bool $absolute |
||
| 145 | * |
||
| 146 | * @return Path|ContainerInterface |
||
| 147 | */ |
||
| 148 | 1 | public static function create(ElementInterface $parent, $x, $y, $absolute = true) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param ElementInterface $parent |
||
| 155 | * @param $x |
||
| 156 | * @param $y |
||
| 157 | * @param $x1 |
||
| 158 | * @param $y1 |
||
| 159 | * @param bool $absolute |
||
| 160 | * |
||
| 161 | * @return ContainerInterface|Path |
||
| 162 | */ |
||
| 163 | 1 | public static function line(ElementInterface $parent, $x, $y, $x1, $y1, $absolute = true) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Draw a line from the current point to the given (x,y) coordinate which becomes the new current point. L |
||
| 170 | * (uppercase) indicates that absolute coordinates will follow; l (lowercase) indicates that relative coordinates |
||
| 171 | * will follow. A number of coordinates pairs may be specified to draw a polyline. At the end of the command, the |
||
| 172 | * new current point is set to the final set of coordinates provided. |
||
| 173 | * |
||
| 174 | * @link https://www.w3.org/TR/SVG/paths.html#PathDataLinetoCommands |
||
| 175 | * |
||
| 176 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
| 177 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
| 178 | * @param bool $absolute |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | 5 | public function lineTo($x, $y, $absolute = true) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param ElementInterface $parent |
||
| 191 | * @param $x |
||
| 192 | * @param $y |
||
| 193 | * @param $x1 |
||
| 194 | * @param bool $absolute |
||
| 195 | * |
||
| 196 | * @return ContainerInterface|Path |
||
| 197 | */ |
||
| 198 | public static function hLine(ElementInterface $parent, $x, $y, $x1, $absolute = true) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Draws a horizontal line from the current point (cpx, cpy) to (x, cpy). H (uppercase) indicates that absolute |
||
| 205 | * coordinates will follow; h (lowercase) indicates that relative coordinates will follow. Multiple x values can be |
||
| 206 | * provided (although usually this doesn't make sense). At the end of the command, the new current point becomes |
||
| 207 | * (x, cpy) for the final value of x. |
||
| 208 | * |
||
| 209 | * @link https://www.w3.org/TR/SVG/paths.html#PathDataLinetoCommands |
||
| 210 | * |
||
| 211 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
| 212 | * |
||
| 213 | * @param bool $absolute |
||
| 214 | * |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | 4 | public function hLineTo($x, $absolute = true) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @param ElementInterface $parent |
||
| 226 | * @param $x |
||
| 227 | * @param $y |
||
| 228 | * @param $y1 |
||
| 229 | * @param bool $absolute |
||
| 230 | * |
||
| 231 | * @return ContainerInterface|Path |
||
| 232 | */ |
||
| 233 | public static function vLine(ElementInterface $parent, $x, $y, $y1, $absolute = true) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Draws a vertical line from the current point (cpx, cpy) to (cpx, y). V (uppercase) indicates that absolute |
||
| 240 | * coordinates will follow; v (lowercase) indicates that relative coordinates will follow. Multiple y values can be |
||
| 241 | * provided (although usually this doesn't make sense). At the end of the command, the new current point becomes |
||
| 242 | * (cpx, y) for the final value of y. |
||
| 243 | * |
||
| 244 | * @link https://www.w3.org/TR/SVG/paths.html#PathDataLinetoCommands |
||
| 245 | * |
||
| 246 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
| 247 | * |
||
| 248 | * @param bool $absolute |
||
| 249 | * |
||
| 250 | * @return $this |
||
| 251 | */ |
||
| 252 | 2 | public function vLineTo($y, $absolute = true) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Draws a cubic Bézier curve from the current point to (x,y) using (x1,y1) as the control point at the beginning |
||
| 261 | * of the curve and (x2,y2) as the control point at the end of the curve. |
||
| 262 | * |
||
| 263 | * @link https://www.w3.org/TR/SVG11/paths.html#PathDataCurveCommands |
||
| 264 | * |
||
| 265 | * @param float $x1 The absolute (relative) X coordinate for the first control point. |
||
| 266 | * @param float $y1 The absolute (relative) Y coordinate for the first control point. |
||
| 267 | * @param float $x2 The absolute (relative) X coordinate for the second control point. |
||
| 268 | * @param float $y2 The absolute (relative) Y coordinate for the second control point. |
||
| 269 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
| 270 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
| 271 | * |
||
| 272 | * @param bool $absolute |
||
| 273 | * |
||
| 274 | * @return $this |
||
| 275 | */ |
||
| 276 | 1 | public function curveTo($x1, $y1, $x2, $y2, $x, $y, $absolute = true) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Draws a cubic Bézier curve from the current point to (x,y). The first control point is assumed to be the |
||
| 285 | * reflection of the second control point on the previous command relative to the current point. (If there is no |
||
| 286 | * previous command or if the previous command was not an C, c, S or s, assume the first control point is |
||
| 287 | * coincident with the current point.) (x2,y2) is the second control point (i.e., the control point at the end of |
||
| 288 | * the curve). S (uppercase) indicates that absolute coordinates will follow; s (lowercase) indicates that relative |
||
| 289 | * coordinates will follow. Multiple sets of coordinates may be specified to draw a polybézier. At the end of the |
||
| 290 | * command, the new current point becomes the final (x,y) coordinate pair used in the polybézier. |
||
| 291 | * |
||
| 292 | * @link https://www.w3.org/TR/SVG11/paths.html#PathDataCurveCommands |
||
| 293 | * |
||
| 294 | * @param float $x2 The absolute (relative) X coordinate for the second control point. |
||
| 295 | * @param float $y2 The absolute (relative) Y coordinate for the second control point. |
||
| 296 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
| 297 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
| 298 | * |
||
| 299 | * @param bool $absolute |
||
| 300 | * |
||
| 301 | * @return $this |
||
| 302 | */ |
||
| 303 | 1 | public function smoothCurveTo($x2, $y2, $x, $y, $absolute = true) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Draws a quadratic Bézier curve from the current point to (x,y). The control point is assumed to be the |
||
| 312 | * reflection of the control point on the previous command relative to the current point. (If there is no previous |
||
| 313 | * command or if the previous command was not a Q, q, T or t, assume the control point is coincident with the |
||
| 314 | * current point.) T (uppercase) indicates that absolute coordinates will follow; t (lowercase) indicates that |
||
| 315 | * relative coordinates will follow. At the end of the command, the new current point becomes the final (x,y) |
||
| 316 | * coordinate pair used in the polybézier. |
||
| 317 | * |
||
| 318 | * @link https://www.w3.org/TR/SVG/paths.html#PathDataQuadraticBezierCommands |
||
| 319 | * |
||
| 320 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
| 321 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
| 322 | * |
||
| 323 | * @param bool $absolute |
||
| 324 | * |
||
| 325 | * @return $this |
||
| 326 | */ |
||
| 327 | 1 | public function smoothQuadraticCurveTo($x, $y, $absolute = true) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Draws an elliptical arc from the current point to (x, y). The size and orientation of the ellipse are defined by |
||
| 336 | * two radii (rx, ry) and an x-axis-rotation, which indicates how the ellipse as a whole is rotated relative to the |
||
| 337 | * current coordinate system. The center (cx, cy) of the ellipse is calculated automatically to satisfy the |
||
| 338 | * constraints imposed by the other parameters. large-arc-flag and sweep-flag contribute to the automatic |
||
| 339 | * calculations and help determine how the arc is drawn. |
||
| 340 | * |
||
| 341 | * @link https://www.w3.org/TR/SVG11/paths.html#PathDataEllipticalArcCommands |
||
| 342 | * |
||
| 343 | * @param float $rx The x-axis radius for the ellipse (i.e., r1). |
||
| 344 | * @param float $ry The y-axis radius for the ellipse |
||
| 345 | * @param float $xRotation The rotation angle in degrees for the ellipse's x-axis relative to the x-axis of |
||
| 346 | * the user coordinate system. |
||
| 347 | * @param boolean $largeArcFlag The value of the large-arc-flag parameter. |
||
| 348 | * @param boolean $sweepFlag The value of the sweep-flag parameter. |
||
| 349 | * @param float $x The absolute (relative) X coordinate for the end point of this path segment. |
||
| 350 | * @param float $y The absolute (relative) Y coordinate for the end point of this path segment. |
||
| 351 | * @param bool $absolute |
||
| 352 | * |
||
| 353 | * @return $this |
||
| 354 | */ |
||
| 355 | 4 | public function arcTo($rx, $ry, $xRotation, $largeArcFlag, $sweepFlag, $x, $y, $absolute = true) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Close the current subpath by drawing a straight line from the current point to current subpath's initial point. |
||
| 364 | * Since the Z and z commands take no parameters, they have an identical effect. |
||
| 365 | * |
||
| 366 | * @link https://www.w3.org/TR/SVG/paths.html#PathDataClosePathCommand |
||
| 367 | * |
||
| 368 | * @param bool $absolute |
||
| 369 | */ |
||
| 370 | 3 | public function closePath($absolute = true) |
|
| 374 | |||
| 375 | 11 | public function getName() |
|
| 379 | |||
| 380 | 1 | public function getBoundingBox() |
|
| 384 | |||
| 385 | 1 | protected function getCenterX() |
|
| 389 | |||
| 390 | 1 | protected function getCenterY() |
|
| 394 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: