Complex classes like Engine 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 Engine, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Engine |
||
28 | { |
||
29 | use AccessorTrait; |
||
30 | |||
31 | const PREFIX = 'p:'; |
||
32 | |||
33 | protected $trace_templates = false; |
||
34 | |||
35 | /** |
||
36 | * @var MarkupCollection |
||
37 | */ |
||
38 | private $markups; |
||
39 | |||
40 | protected function get_markups() |
||
44 | |||
45 | /** |
||
46 | * @var FunctionCollection |
||
47 | */ |
||
48 | private $functions; |
||
49 | |||
50 | protected function get_functions() |
||
54 | |||
55 | /** |
||
56 | * Expression evaluator. |
||
57 | * |
||
58 | * @var Evaluator |
||
59 | */ |
||
60 | private $evaluator; |
||
61 | |||
62 | /** |
||
63 | * Initializes the {@link $evaluator} property, and a bunch of functions. |
||
64 | * |
||
65 | * @param MarkupCollection $markups |
||
66 | * @param FunctionCollection $functions |
||
67 | */ |
||
68 | public function __construct(MarkupCollection $markups, FunctionCollection $functions) |
||
77 | |||
78 | public function __clone() |
||
82 | |||
83 | /** |
||
84 | * Evaluate an expression relative to a context. |
||
85 | * |
||
86 | * @param mixed $context |
||
87 | * @param string $expression |
||
88 | * @param bool $silent |
||
89 | */ |
||
90 | public function evaluate($expression, $silent=false, $context=null) |
||
96 | |||
97 | /** |
||
98 | * @return Engine |
||
99 | * |
||
100 | * @deprecated |
||
101 | */ |
||
102 | static public function get_singleton() |
||
106 | |||
107 | /* |
||
108 | ** |
||
109 | |||
110 | SYSTEM |
||
111 | |||
112 | ** |
||
113 | */ |
||
114 | |||
115 | protected $trace = []; |
||
116 | protected $errors = []; |
||
117 | |||
118 | public function trace_enter($a) |
||
122 | |||
123 | public function trace_exit() |
||
127 | |||
128 | public function error($alert, array $args=[]) |
||
184 | |||
185 | public function handle_exception(\Exception $e) |
||
198 | |||
199 | public function fetchErrors() |
||
207 | |||
208 | public function get_file() |
||
220 | |||
221 | public function get_template_dir() |
||
225 | |||
226 | /* |
||
227 | ** |
||
228 | |||
229 | TEMPLATES |
||
230 | |||
231 | ** |
||
232 | */ |
||
233 | |||
234 | protected $templates = []; |
||
235 | |||
236 | public function addTemplate($name, $template) |
||
251 | |||
252 | protected function resolve_template($name) |
||
298 | |||
299 | protected function create_template_from_file($pathname) |
||
306 | |||
307 | protected function get_template($name) |
||
316 | |||
317 | /** |
||
318 | * Calls a template. |
||
319 | * |
||
320 | * @param $name |
||
321 | * @param array $args |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | public function callTemplate($name, array $args=[]) |
||
355 | |||
356 | /* |
||
357 | ** |
||
358 | |||
359 | CONTEXT |
||
360 | |||
361 | ** |
||
362 | */ |
||
363 | |||
364 | public $context; |
||
365 | |||
366 | protected function init_context() |
||
370 | |||
371 | /* |
||
372 | ** |
||
373 | |||
374 | PUBLISH |
||
375 | |||
376 | ** |
||
377 | */ |
||
378 | |||
379 | protected function get_compiled($template) |
||
390 | |||
391 | public function __invoke($template, $bind=null, array $options=[]) |
||
500 | |||
501 | /* |
||
502 | |||
503 | # |
||
504 | # $context_markup is used to keep track of two variables associated with each markup : |
||
505 | # self and this. |
||
506 | # |
||
507 | # 'self' is a reference to the markup itself, holding its name and the arguments with which |
||
508 | # it was called, it is also used to store special markup data as for the foreach markup |
||
509 | # |
||
510 | # 'this' is a reference to the object of the markup, that being an array, an object or a value |
||
511 | # |
||
512 | # |
||
513 | |||
514 | <p:articles> |
||
515 | |||
516 | self.range.start |
||
517 | self.range.limit |
||
518 | self.range.count |
||
519 | |||
520 | this = array of Articles |
||
521 | |||
522 | <p:foreach> |
||
523 | |||
524 | self.name = foreach |
||
525 | self.arguments = [] |
||
526 | self.position |
||
527 | self.key |
||
528 | self.left |
||
529 | |||
530 | this = an Article object |
||
531 | |||
532 | </p:foreach> |
||
533 | </p:articles> |
||
534 | |||
535 | */ |
||
536 | |||
537 | public $context_markup = []; // should be protected |
||
538 | } |
||
539 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.