Complex classes like Ebi 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 Ebi, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Ebi { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $cachePath; |
||
16 | /** |
||
17 | * @var callable[] |
||
18 | */ |
||
19 | protected $functions; |
||
20 | /** |
||
21 | * @var TemplateLoaderInterface |
||
22 | */ |
||
23 | private $templateLoader; |
||
24 | /** |
||
25 | * @var CompilerInterface |
||
26 | */ |
||
27 | private $compiler; |
||
28 | /** |
||
29 | * @var callable[] |
||
30 | */ |
||
31 | private $components = []; |
||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $meta; |
||
36 | |||
37 | /** |
||
38 | * Ebi constructor. |
||
39 | * |
||
40 | * @param TemplateLoaderInterface $templateLoader Used to load template sources from component names. |
||
41 | * @param string $cachePath The path to cache compiled templates. |
||
42 | * @param CompilerInterface $compiler The compiler used to compile templates. |
||
|
|||
43 | */ |
||
44 | 72 | public function __construct(TemplateLoaderInterface $templateLoader, $cachePath, CompilerInterface $compiler = null) { |
|
102 | |||
103 | /** |
||
104 | * Register a runtime function. |
||
105 | * |
||
106 | * @param string $name The name of the function. |
||
107 | * @param callable $function The function callback. |
||
108 | */ |
||
109 | 72 | public function defineFunction($name, $function = null) { |
|
117 | |||
118 | 72 | private function mb($func) { |
|
121 | |||
122 | /** |
||
123 | * Write a component to the output buffer. |
||
124 | * |
||
125 | * @param string $component The name of the component. |
||
126 | * @param array ...$args |
||
127 | */ |
||
128 | 22 | public function write($component, ...$args) { |
|
147 | |||
148 | /** |
||
149 | * Lookup a component with a given name. |
||
150 | * |
||
151 | * @param string $component The component to lookup. |
||
152 | * @return callable|null Returns the component function or **null** if the component is not found. |
||
153 | */ |
||
154 | 67 | public function lookup($component) { |
|
170 | |||
171 | /** |
||
172 | * Check to see if a component exists. |
||
173 | * |
||
174 | * @param string $component The name of the component. |
||
175 | * @param bool $loader Whether or not to use the component loader or just look in the component cache. |
||
176 | * @return bool Returns **true** if the component exists or **false** otherwise. |
||
177 | */ |
||
178 | 2 | public function componentExists($component, $loader = true) { |
|
187 | |||
188 | /** |
||
189 | * Strip the namespace off a component name to get the component key. |
||
190 | * |
||
191 | * @param string $component The full name of the component with a possible namespace. |
||
192 | * @return string Returns the component key. |
||
193 | */ |
||
194 | 69 | protected function componentKey($component) { |
|
200 | |||
201 | /** |
||
202 | * Load a component. |
||
203 | * |
||
204 | * @param string $component The name of the component to load. |
||
205 | * @return callable|null Returns the component or **null** if the component isn't found. |
||
206 | */ |
||
207 | 62 | protected function loadComponent($component) { |
|
231 | |||
232 | 8 | protected function writeCompileException($props) { |
|
284 | |||
285 | /** |
||
286 | * Check to see if a specific cache key exists in the cache. |
||
287 | * |
||
288 | * @param string $cacheKey The cache key to check. |
||
289 | * @return bool Returns **true** if there is a cache key at the file or **false** otherwise. |
||
290 | */ |
||
291 | 1 | public function cacheKeyExists($cacheKey) { |
|
295 | |||
296 | /** |
||
297 | * Compile a component from source, cache it and include it. |
||
298 | * |
||
299 | * @param string $component The name of the component. |
||
300 | * @param string $src The component source. |
||
301 | * @param string $cacheKey The cache key of the component. |
||
302 | * @return callable|null Returns the compiled component closure. |
||
303 | */ |
||
304 | 64 | public function compile($component, $src, $cacheKey) { |
|
315 | |||
316 | /** |
||
317 | * Include a cached component. |
||
318 | * |
||
319 | * @param string $component The component key. |
||
320 | * @param string $cachePath The path to the component. |
||
321 | * @return callable|null Returns the component function or **null** if the component wasn't properly defined. |
||
322 | */ |
||
323 | 56 | private function includeComponent($component, $cachePath) { |
|
337 | |||
338 | /** |
||
339 | * A safe version of {@link file_put_contents()} that also clears op caches. |
||
340 | * |
||
341 | * @param string $path The path to save to. |
||
342 | * @param string $contents The contents of the file. |
||
343 | * @return bool Returns **true** on success or **false** on failure. |
||
344 | */ |
||
345 | 56 | private function filePutContents($path, $contents) { |
|
365 | |||
366 | /** |
||
367 | * Include a file. |
||
368 | * |
||
369 | * This is method is useful for including a file bound to this object instance. |
||
370 | * |
||
371 | * @param string $path The path to the file to include. |
||
372 | * @return mixed Returns the result of the include. |
||
373 | */ |
||
374 | 56 | public function requireFile($path) { |
|
377 | |||
378 | /** |
||
379 | * Register a component. |
||
380 | * |
||
381 | * @param string $name The name of the component to register. |
||
382 | * @param callable $component The component function. |
||
383 | */ |
||
384 | 72 | public function defineComponent($name, callable $component) { |
|
387 | |||
388 | /** |
||
389 | * Render a component to a string. |
||
390 | * |
||
391 | * @param string $component The name of the component to render. |
||
392 | * @param array ...$args Arguments to pass to the component. |
||
393 | * @return string|null Returns the rendered component or **null** if the component was not found. |
||
394 | */ |
||
395 | 63 | public function render($component, ...$args) { |
|
408 | |||
409 | /** |
||
410 | * Set the error reporting appropriate for template rendering. |
||
411 | * |
||
412 | * @return int Returns the previous error level. |
||
413 | */ |
||
414 | public function setErrorReporting() { |
||
418 | |||
419 | /** |
||
420 | * Call a function registered with **defineFunction()**. |
||
421 | * |
||
422 | * If a static or global function is registered then it's simply rendered in the compiled template. |
||
423 | * This method is for closures or callbacks. |
||
424 | * |
||
425 | * @param string $name The name of the registered function. |
||
426 | * @param array ...$args The function's argument. |
||
427 | * @return mixed Returns the result of the function |
||
428 | * @throws RuntimeException Throws an exception when the function isn't found. |
||
429 | */ |
||
430 | 3 | public function call($name, ...$args) { |
|
437 | |||
438 | /** |
||
439 | * Render a variable appropriately for CSS. |
||
440 | * |
||
441 | * This is a convenience runtime function. |
||
442 | * |
||
443 | * @param string|array $expr A CSS class, an array of CSS classes, or an associative array where the keys are class |
||
444 | * names and the values are truthy conditions to include the class (or not). |
||
445 | * @return string Returns a space-delimited CSS class string. |
||
446 | */ |
||
447 | 6 | public function attributeClass($expr) { |
|
464 | |||
465 | /** |
||
466 | * Format a data. |
||
467 | * |
||
468 | * @param mixed $date The date to format. This can be a string data, a timestamp or an instance of **DateTimeInterface**. |
||
469 | * @param string $format The format of the date. |
||
470 | * @return string Returns the formatted data. |
||
471 | * @see date_format() |
||
472 | */ |
||
473 | 1 | public function formatDate($date, $format = 'c') { |
|
494 | |||
495 | /** |
||
496 | * Get a single item from the meta array. |
||
497 | * |
||
498 | * @param string $name The key to get from. |
||
499 | * @param mixed $default The default value if no item at the key exists. |
||
500 | * @return mixed Returns the meta value. |
||
501 | */ |
||
502 | public function getMeta($name, $default = null) { |
||
505 | |||
506 | /** |
||
507 | * Set a single item to the meta array. |
||
508 | * |
||
509 | * @param string $name The key to set. |
||
510 | * @param mixed $value The new value. |
||
511 | * @return $this |
||
512 | */ |
||
513 | 1 | public function setMeta($name, $value) { |
|
517 | |||
518 | /** |
||
519 | * Get the template loader. |
||
520 | * |
||
521 | * The template loader translates component names into template contents. |
||
522 | * |
||
523 | * @return TemplateLoaderInterface Returns the template loader. |
||
524 | */ |
||
525 | 1 | public function getTemplateLoader() { |
|
528 | |||
529 | /** |
||
530 | * Set the template loader. |
||
531 | * |
||
532 | * The template loader translates component names into template contents. |
||
533 | * |
||
534 | * @param TemplateLoaderInterface $templateLoader The new template loader. |
||
535 | * @return $this |
||
536 | */ |
||
537 | public function setTemplateLoader($templateLoader) { |
||
541 | |||
542 | /** |
||
543 | * Get the entire meta array. |
||
544 | * |
||
545 | * @return array Returns the meta. |
||
546 | */ |
||
547 | public function getMetaArray() { |
||
550 | |||
551 | /** |
||
552 | * Set the entire meta array. |
||
553 | * |
||
554 | * @param array $meta The new meta array. |
||
555 | * @return $this |
||
556 | */ |
||
557 | public function setMetaArray(array $meta) { |
||
561 | |||
562 | /** |
||
563 | * Return a dynamic attribute. |
||
564 | * |
||
565 | * The attribute renders differently depending on the value. |
||
566 | * |
||
567 | * - If the value is **true** then it will render as an HTML5 boolean attribute. |
||
568 | * - If the value is **false** or **null** then the attribute will not render. |
||
569 | * - Other values render as attribute values. |
||
570 | * - Attributes that start with **aria-** render **true** and **false** as values. |
||
571 | * |
||
572 | * @param string $name The name of the attribute. |
||
573 | * @param mixed $value The value of the attribute. |
||
574 | * @return string Returns the attribute definition or an empty string. |
||
575 | */ |
||
576 | 15 | public function attribute($name, $value) { |
|
588 | |||
589 | /** |
||
590 | * Escape a value for echoing to HTML with a bit of non-scalar checking. |
||
591 | * |
||
592 | * @param mixed $val The value to escape. |
||
593 | * @return string The escaped value. |
||
594 | */ |
||
595 | 25 | public function escape($val = null) { |
|
606 | |||
607 | /** |
||
608 | * Write children blocks. |
||
609 | * |
||
610 | * @param array|callable|null $children The children blocks to write. |
||
611 | */ |
||
612 | 4 | public function writeChildren($children) { |
|
621 | } |
||
622 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.