Complex classes like ColorNode 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.
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 ColorNode, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class ColorNode extends Node implements ComparableInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Node type. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $type = 'Color'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $originalForm; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Constructor. |
||
| 38 | * |
||
| 39 | * @param string|array $rgb The rgb value |
||
| 40 | * @param int $alpha Alpha channel |
||
| 41 | * @param string $originalForm Original form of the color |
||
| 42 | * |
||
| 43 | * @throws InvalidArgumentException |
||
| 44 | */ |
||
| 45 | public function __construct($rgb, $alpha = 1, $originalForm = null) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Returns the color object. |
||
| 59 | * |
||
| 60 | * @return Color |
||
| 61 | */ |
||
| 62 | public function getColor() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritdoc} |
||
| 69 | */ |
||
| 70 | public function generateCSS(Context $context, OutputInterface $output) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns the RGB channels. |
||
| 77 | * |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | public function getRGB() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Returns the HSV components of the color. |
||
| 87 | * |
||
| 88 | * @return array |
||
| 89 | */ |
||
| 90 | public function toHSV() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns the red channel. |
||
| 97 | * |
||
| 98 | * @param bool $raw Return raw value? |
||
| 99 | * |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | public function getRed($raw = false) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Returns the green channel. |
||
| 109 | * |
||
| 110 | * @param bool $raw Return raw value? |
||
| 111 | * |
||
| 112 | * @return mixed |
||
| 113 | */ |
||
| 114 | public function getGreen($raw = false) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns the blue channel. |
||
| 121 | * |
||
| 122 | * @param bool $raw Return raw value? |
||
| 123 | * |
||
| 124 | * @return mixed |
||
| 125 | */ |
||
| 126 | public function getBlue($raw = false) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the alpha channel. |
||
| 133 | * |
||
| 134 | * @param bool $raw Return raw value? |
||
| 135 | * |
||
| 136 | * @return mixed |
||
| 137 | */ |
||
| 138 | public function getAlpha($raw = false) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Returns the color saturation. |
||
| 145 | * |
||
| 146 | * @param bool $raw Return raw value? |
||
| 147 | * |
||
| 148 | * @return mixed |
||
| 149 | */ |
||
| 150 | public function getSaturation($raw = false) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Returns the color hue. |
||
| 158 | * |
||
| 159 | * @param bool $raw Raw value? |
||
| 160 | * |
||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | public function getHue($raw = false) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Returns the lightness. |
||
| 170 | * |
||
| 171 | * @param bool $raw Return raw value? |
||
| 172 | * |
||
| 173 | * @return mixed ILessNode\DimensionNode if $raw is false |
||
| 174 | */ |
||
| 175 | public function getLightness($raw = false) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the luma. |
||
| 183 | * |
||
| 184 | * @param bool $raw Return raw value? |
||
| 185 | * |
||
| 186 | * @return mixed ILessNode\DimensionNode if $raw is false |
||
| 187 | */ |
||
| 188 | public function getLuma($raw = false) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Returns the luminance. |
||
| 197 | * |
||
| 198 | * @param bool $raw Return raw value? |
||
| 199 | * |
||
| 200 | * @return mixed ILessNode\DimensionNode if $raw is false |
||
| 201 | */ |
||
| 202 | public function getLuminance($raw = false) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Converts the node to ARGB. |
||
| 211 | * |
||
| 212 | * @return AnonymousNode |
||
| 213 | */ |
||
| 214 | public function toARGB() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Returns the HSL components of the color. |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | public function toHSL() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Converts the node to string. |
||
| 231 | * |
||
| 232 | * @param Context $context |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function toCSS(Context $context) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Operations have to be done per-channel, if not, |
||
| 243 | * channels will spill onto each other. Once we have |
||
| 244 | * our result, in the form of an integer triplet, |
||
| 245 | * we create a new color node to hold the result. |
||
| 246 | * |
||
| 247 | * @param Context $context |
||
| 248 | * @param string $op |
||
| 249 | * @param Node $other |
||
| 250 | * |
||
| 251 | * @return ColorNode |
||
| 252 | * |
||
| 253 | * @throws InvalidArgumentException |
||
| 254 | */ |
||
| 255 | public function operate(Context $context, $op, Node $other) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Compares with another node. |
||
| 288 | * |
||
| 289 | * @param Node $other |
||
| 290 | * |
||
| 291 | * @return int |
||
| 292 | * |
||
| 293 | * @throws InvalidArgumentException |
||
| 294 | */ |
||
| 295 | public function compare(Node $other) |
||
| 319 | } |
||
| 320 |
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 sub-classes 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 parent class: