Complex classes like ImportNode 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 ImportNode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class ImportNode extends Node implements \Serializable |
||
25 | { |
||
26 | /** |
||
27 | * Node type. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $type = 'Import'; |
||
32 | |||
33 | /** |
||
34 | * The path. |
||
35 | * |
||
36 | * @var QuotedNode|UrlNode |
||
37 | */ |
||
38 | public $path; |
||
39 | |||
40 | /** |
||
41 | * Current file index. |
||
42 | * |
||
43 | * @var int |
||
44 | */ |
||
45 | public $index = 0; |
||
46 | |||
47 | /** |
||
48 | * Array of options. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | public $options = [ |
||
53 | 'inline' => false, |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * The features. |
||
58 | * |
||
59 | * @var Node |
||
60 | */ |
||
61 | public $features; |
||
62 | |||
63 | /** |
||
64 | * Import CSS flag. |
||
65 | * |
||
66 | * @var bool |
||
67 | */ |
||
68 | public $css = false; |
||
69 | |||
70 | /** |
||
71 | * Skip import? |
||
72 | * |
||
73 | * @var bool |
||
74 | * |
||
75 | * @see Visitor_Import::visitImport |
||
76 | */ |
||
77 | public $skip = false; |
||
78 | |||
79 | /** |
||
80 | * The root node. |
||
81 | * |
||
82 | * @var Node |
||
83 | */ |
||
84 | public $root; |
||
85 | |||
86 | /** |
||
87 | * Error. |
||
88 | * |
||
89 | * @var Exception |
||
90 | */ |
||
91 | public $error; |
||
92 | |||
93 | /** |
||
94 | * Imported filename. |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | public $importedFilename; |
||
99 | |||
100 | /** |
||
101 | * Constructor. |
||
102 | * |
||
103 | * @param Node $path The path |
||
104 | * @param Node $features The features |
||
105 | * @param array $options Array of options |
||
106 | * @param int $index The index |
||
107 | * @param FileInfo $currentFileInfo Current file info |
||
108 | */ |
||
109 | public function __construct( |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function accept(VisitorInterface $visitor) |
||
145 | |||
146 | /** |
||
147 | * Returns the path. |
||
148 | * |
||
149 | * @return string|null |
||
150 | */ |
||
151 | public function getPath() |
||
155 | |||
156 | /** |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function isVariableImport() |
||
172 | |||
173 | /** |
||
174 | * Compiles the node. |
||
175 | * |
||
176 | * @param Context $context The context |
||
177 | * @param array|null $arguments Array of arguments |
||
178 | * @param bool|null $important Important flag |
||
179 | * |
||
180 | * @return array|ImportNode|MediaNode |
||
181 | * |
||
182 | * @throws Exception |
||
183 | * @throws LogicException |
||
184 | */ |
||
185 | public function compile(Context $context, $arguments = null, $important = null) |
||
227 | |||
228 | /** |
||
229 | * Compiles the path. |
||
230 | * |
||
231 | * @param Context $context |
||
232 | * |
||
233 | * @return UrlNode |
||
234 | */ |
||
235 | public function compilePath(Context $context) |
||
252 | |||
253 | /** |
||
254 | * Compiles the node for import. |
||
255 | * |
||
256 | * @param Context $context |
||
257 | * |
||
258 | * @return ImportNode |
||
259 | */ |
||
260 | public function compileForImport(Context $context) |
||
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | */ |
||
274 | public function generateCSS(Context $context, OutputInterface $output) |
||
289 | |||
290 | /** |
||
291 | * Returns the option with given $name. |
||
292 | * |
||
293 | * @param string $name The option name |
||
294 | * @param string $default The default value |
||
295 | * |
||
296 | * @return mixed |
||
297 | */ |
||
298 | public function getOption($name, $default = null) |
||
302 | |||
303 | /** |
||
304 | * Has the import an error? |
||
305 | * |
||
306 | * @return bool |
||
307 | */ |
||
308 | public function hasError() |
||
312 | |||
313 | /** |
||
314 | * Returns the error. |
||
315 | * |
||
316 | * @return null|Exception |
||
317 | */ |
||
318 | public function getError() |
||
322 | |||
323 | public function serialize() |
||
330 | |||
331 | public function unserialize($serialized) |
||
338 | } |
||
339 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..