Complex classes like DimensionNode 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 DimensionNode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class DimensionNode extends Node implements ComparableInterface, ToColorConvertibleInterface |
||
25 | { |
||
26 | /** |
||
27 | * Node type. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $type = 'Dimension'; |
||
32 | |||
33 | /** |
||
34 | * The unit. |
||
35 | * |
||
36 | * @var UnitNode |
||
37 | */ |
||
38 | public $unit; |
||
39 | |||
40 | /** |
||
41 | * Constructor. |
||
42 | * |
||
43 | * @param string $value |
||
44 | * @param UnitNode|string $unit |
||
45 | */ |
||
46 | public function __construct($value, $unit = null) |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function accept(VisitorInterface $visitor) |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function toColor() |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function generateCSS(Context $context, OutputInterface $output) |
||
114 | |||
115 | /** |
||
116 | * Convert the value to string. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | public function toString() |
||
124 | |||
125 | /** |
||
126 | * Operates with the dimension. In an operation between two dimensions, |
||
127 | * we default to the first Dimension's unit, |
||
128 | * so `1px + 2` will yield `3px`. |
||
129 | * |
||
130 | * @param Context $context |
||
131 | * @param string $op |
||
132 | * @param DimensionNode $other |
||
133 | * |
||
134 | * @return DimensionNode |
||
135 | * |
||
136 | * @throws CompilerException |
||
137 | */ |
||
138 | public function operate(Context $context, $op, DimensionNode $other) |
||
180 | |||
181 | /** |
||
182 | * Compares with another dimension. |
||
183 | * |
||
184 | * @param Node $other |
||
185 | * |
||
186 | * @return int |
||
187 | */ |
||
188 | public function compare(Node $other) |
||
207 | |||
208 | /** |
||
209 | * Converts to the unified dimensions. |
||
210 | * |
||
211 | * @return DimensionNode |
||
212 | */ |
||
213 | public function unify() |
||
223 | |||
224 | /** |
||
225 | * Converts to another unit. |
||
226 | * |
||
227 | * @param array|string $conversions |
||
228 | * |
||
229 | * @return DimensionNode |
||
230 | */ |
||
231 | public function convertTo($conversions) |
||
277 | } |
||
278 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: