Complex classes like Compiler 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 Compiler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Compiler implements ArgumentParser { |
||
21 | /** |
||
22 | * @var V\Variable[] |
||
23 | */ |
||
24 | protected $predefined_variables; |
||
25 | |||
26 | /** |
||
27 | * @var R\Schema[] |
||
28 | */ |
||
29 | protected $schemas; |
||
30 | |||
31 | /** |
||
32 | * @var array<string,R\Property[]> |
||
33 | */ |
||
34 | protected $properties; |
||
35 | |||
36 | /** |
||
37 | * @var array<string,V\Variable[]> |
||
38 | */ |
||
39 | protected $variables = array(); |
||
40 | |||
41 | /** |
||
42 | * @var R\Rule[] |
||
43 | */ |
||
44 | protected $rules = array(); |
||
45 | |||
46 | /** |
||
47 | * @var string|null |
||
48 | */ |
||
49 | protected $last_explanation = null; |
||
50 | |||
51 | /** |
||
52 | * @var Parameter[]|null |
||
53 | */ |
||
54 | protected $current_parameters = []; |
||
55 | |||
56 | /** |
||
57 | * @param V\Variable[] $predefined_variables |
||
58 | * @param R\Schema[] $schemas |
||
59 | * @param V\Property[] $properties |
||
60 | */ |
||
61 | public function __construct( array $predefined_variables |
||
78 | |||
79 | /** |
||
80 | * Compile an AST to an according entity. |
||
81 | * |
||
82 | * @param AST\Node $node |
||
83 | * @return mixed |
||
84 | */ |
||
85 | public function compile(AST\Root $node) { |
||
92 | |||
93 | protected function compile_root(AST\Root $node) { |
||
113 | |||
114 | protected function compile_assignment(AST\Assignment $node) { |
||
124 | |||
125 | protected function compile_rule(AST\Rule $node) { |
||
145 | |||
146 | protected function compile_definition(AST\Definition $node) { |
||
161 | |||
162 | protected function compile_any(AST\Any $node) { |
||
179 | |||
180 | protected function compile_except(AST\Except $node) { |
||
186 | |||
187 | protected function compile_property(AST\Property $node) { |
||
193 | |||
194 | protected function compile_parameters($prop_or_schema, array $parameters) { |
||
205 | |||
206 | protected function compile_qualifier(AST\Qualifier $node) { |
||
219 | |||
220 | /** |
||
221 | * Add a variable to the variables currently known. |
||
222 | * |
||
223 | * @param string $name |
||
224 | * @param V\Variable $def |
||
225 | * @return null |
||
226 | */ |
||
227 | protected function add_variable($name, V\Variable $def) { |
||
235 | |||
236 | /** |
||
237 | * Get a predefined variable. |
||
238 | * |
||
239 | * @param string $name |
||
240 | * @return V\Variable |
||
241 | */ |
||
242 | protected function get_variable($name) { |
||
248 | |||
249 | /** |
||
250 | * Get a property. Yes. Really. |
||
251 | * |
||
252 | * @param string $name |
||
253 | * @return V\Property |
||
254 | */ |
||
255 | protected function get_property($name) { |
||
261 | |||
262 | /** |
||
263 | * Get a schema. By name. |
||
264 | * |
||
265 | * @param string $name |
||
266 | * @return V\Schema |
||
267 | */ |
||
268 | protected function get_schema($name) { |
||
274 | |||
275 | /** |
||
276 | * Add all predefined variables to the current set of variables. |
||
277 | * |
||
278 | * @return null |
||
279 | */ |
||
280 | protected function add_predefined_variables() { |
||
285 | |||
286 | /** |
||
287 | * Purge all predefined variables from the current set of variables. |
||
288 | * |
||
289 | * @return null |
||
290 | */ |
||
291 | protected function purge_predefined_variables() { |
||
296 | |||
297 | // IMPLEMENTATION OF ArgumentParser |
||
298 | |||
299 | /** |
||
300 | * @inheritdoc |
||
301 | */ |
||
302 | public function fetch_string() { |
||
306 | |||
307 | /** |
||
308 | * @inheritdoc |
||
309 | */ |
||
310 | public function fetch_variable() { |
||
314 | |||
315 | protected function next_current_parameter($class) { |
||
327 | } |
||
328 |
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..