Complex classes like UnitNode 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 UnitNode, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class UnitNode extends Node implements ComparableInterface |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Length regular expression. |
||
| 24 | */ |
||
| 25 | const LENGTH_REGEXP = '/px|em|%|in|cm|mm|pc|pt|ex/'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Numerator. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | public $numerator = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Denominator. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | public $denominator = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The backup unit to use when unit is empty. |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $backupUnit; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Constructor. |
||
| 50 | * |
||
| 51 | * @param array $numerator |
||
| 52 | * @param array $denominator |
||
| 53 | * @param string $backupUnit |
||
| 54 | */ |
||
| 55 | public function __construct(array $numerator = [], array $denominator = [], $backupUnit = null) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Converts to string. |
||
| 72 | * |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public function toString() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * {@inheritdoc} |
||
| 87 | */ |
||
| 88 | public function compare(Node $other) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Take care about backup unit when cloning, see the constructor. |
||
| 98 | */ |
||
| 99 | public function __clone() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Is the unit equal to $unitString? |
||
| 108 | * |
||
| 109 | * @param string $unitString |
||
| 110 | * |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | public function is($unitString) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Is the unit length unit? |
||
| 120 | * |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | public function isLength() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Is the unit angle unit? |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | public function isAngle() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Is the unit empty? |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function isEmpty() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Is singular? |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | public function isSingular() |
||
| 161 | |||
| 162 | public function usedUnits() |
||
| 183 | |||
| 184 | public function cancel() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | public function generateCSS(Context $context, OutputInterface $output) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Convert to string. |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function __toString() |
||
| 240 | } |
||
| 241 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: