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 | 78 | public function __construct(TemplateLoaderInterface $templateLoader, $cachePath, CompilerInterface $compiler = null) { |
|
108 | |||
109 | /** |
||
110 | * Register a runtime function. |
||
111 | * |
||
112 | * @param string $name The name of the function. |
||
113 | * @param callable $function The function callback. |
||
114 | */ |
||
115 | 78 | public function defineFunction($name, $function = null) { |
|
123 | |||
124 | 78 | private function mb($func) { |
|
127 | |||
128 | /** |
||
129 | * Write a component to the output buffer. |
||
130 | * |
||
131 | * @param string $component The name of the component. |
||
132 | * @param array ...$args |
||
133 | */ |
||
134 | 22 | public function write($component, ...$args) { |
|
153 | |||
154 | /** |
||
155 | * Lookup a component with a given name. |
||
156 | * |
||
157 | * @param string $component The component to lookup. |
||
158 | * @return callable|null Returns the component function or **null** if the component is not found. |
||
159 | */ |
||
160 | 73 | public function lookup($component) { |
|
176 | |||
177 | /** |
||
178 | * Check to see if a component exists. |
||
179 | * |
||
180 | * @param string $component The name of the component. |
||
181 | * @param bool $loader Whether or not to use the component loader or just look in the component cache. |
||
182 | * @return bool Returns **true** if the component exists or **false** otherwise. |
||
183 | */ |
||
184 | 2 | public function componentExists($component, $loader = true) { |
|
193 | |||
194 | /** |
||
195 | * Strip the namespace off a component name to get the component key. |
||
196 | * |
||
197 | * @param string $component The full name of the component with a possible namespace. |
||
198 | * @return string Returns the component key. |
||
199 | */ |
||
200 | 75 | protected function componentKey($component) { |
|
206 | |||
207 | /** |
||
208 | * Load a component. |
||
209 | * |
||
210 | * @param string $component The name of the component to load. |
||
211 | * @return callable|null Returns the component or **null** if the component isn't found. |
||
212 | */ |
||
213 | 68 | protected function loadComponent($component) { |
|
237 | |||
238 | 8 | protected function writeCompileException($props) { |
|
290 | |||
291 | /** |
||
292 | * Check to see if a specific cache key exists in the cache. |
||
293 | * |
||
294 | * @param string $cacheKey The cache key to check. |
||
295 | * @return bool Returns **true** if there is a cache key at the file or **false** otherwise. |
||
296 | */ |
||
297 | 1 | public function cacheKeyExists($cacheKey) { |
|
301 | |||
302 | /** |
||
303 | * Compile a component from source, cache it and include it. |
||
304 | * |
||
305 | * @param string $component The name of the component. |
||
306 | * @param string $src The component source. |
||
307 | * @param string $cacheKey The cache key of the component. |
||
308 | * @return callable|null Returns the compiled component closure. |
||
309 | */ |
||
310 | 70 | public function compile($component, $src, $cacheKey) { |
|
321 | |||
322 | /** |
||
323 | * Include a cached component. |
||
324 | * |
||
325 | * @param string $component The component key. |
||
326 | * @param string $cachePath The path to the component. |
||
327 | * @return callable|null Returns the component function or **null** if the component wasn't properly defined. |
||
328 | */ |
||
329 | 62 | private function includeComponent($component, $cachePath) { |
|
343 | |||
344 | /** |
||
345 | * A safe version of {@link file_put_contents()} that also clears op caches. |
||
346 | * |
||
347 | * @param string $path The path to save to. |
||
348 | * @param string $contents The contents of the file. |
||
349 | * @return bool Returns **true** on success or **false** on failure. |
||
350 | */ |
||
351 | 62 | private function filePutContents($path, $contents) { |
|
371 | |||
372 | /** |
||
373 | * Include a file. |
||
374 | * |
||
375 | * This is method is useful for including a file bound to this object instance. |
||
376 | * |
||
377 | * @param string $path The path to the file to include. |
||
378 | * @return mixed Returns the result of the include. |
||
379 | */ |
||
380 | 62 | public function requireFile($path) { |
|
383 | |||
384 | /** |
||
385 | * Register a component. |
||
386 | * |
||
387 | * @param string $name The name of the component to register. |
||
388 | * @param callable $component The component function. |
||
389 | */ |
||
390 | 78 | public function defineComponent($name, callable $component) { |
|
393 | |||
394 | /** |
||
395 | * Render a component to a string. |
||
396 | * |
||
397 | * @param string $component The name of the component to render. |
||
398 | * @param array ...$args Arguments to pass to the component. |
||
399 | * @return string|null Returns the rendered component or **null** if the component was not found. |
||
400 | */ |
||
401 | 69 | public function render($component, ...$args) { |
|
414 | |||
415 | /** |
||
416 | * Set the error reporting appropriate for template rendering. |
||
417 | * |
||
418 | * @return int Returns the previous error level. |
||
419 | */ |
||
420 | public function setErrorReporting() { |
||
424 | |||
425 | /** |
||
426 | * Call a function registered with **defineFunction()**. |
||
427 | * |
||
428 | * If a static or global function is registered then it's simply rendered in the compiled template. |
||
429 | * This method is for closures or callbacks. |
||
430 | * |
||
431 | * @param string $name The name of the registered function. |
||
432 | * @param array ...$args The function's argument. |
||
433 | * @return mixed Returns the result of the function |
||
434 | * @throws RuntimeException Throws an exception when the function isn't found. |
||
435 | */ |
||
436 | 4 | public function call($name, ...$args) { |
|
443 | |||
444 | /** |
||
445 | * Render a variable appropriately for CSS. |
||
446 | * |
||
447 | * This is a convenience runtime function. |
||
448 | * |
||
449 | * @param string|array $expr A CSS class, an array of CSS classes, or an associative array where the keys are class |
||
450 | * names and the values are truthy conditions to include the class (or not). |
||
451 | * @return string Returns a space-delimited CSS class string. |
||
452 | */ |
||
453 | 7 | public function attributeClass($expr) { |
|
470 | |||
471 | /** |
||
472 | * Render a variable appropriately for a style attribute. |
||
473 | * |
||
474 | * This function expects a string or an array. If an array is supplied then the keys represent style properties and |
||
475 | * the values are style values. |
||
476 | * |
||
477 | * @param mixed $expr The expression to render. |
||
478 | * @return string Returns a style attribute. |
||
479 | */ |
||
480 | 4 | public function attributeStyle($expr) { |
|
508 | |||
509 | /** |
||
510 | * Format a data. |
||
511 | * |
||
512 | * @param mixed $date The date to format. This can be a string data, a timestamp or an instance of **DateTimeInterface**. |
||
513 | * @param string $format The format of the date. |
||
514 | * @return string Returns the formatted data. |
||
515 | * @see date_format() |
||
516 | */ |
||
517 | 1 | public function formatDate($date, $format = 'c') { |
|
538 | |||
539 | /** |
||
540 | * Get a single item from the meta array. |
||
541 | * |
||
542 | * @param string $name The key to get from. |
||
543 | * @param mixed $default The default value if no item at the key exists. |
||
544 | * @return mixed Returns the meta value. |
||
545 | */ |
||
546 | public function getMeta($name, $default = null) { |
||
549 | |||
550 | /** |
||
551 | * Set a single item to the meta array. |
||
552 | * |
||
553 | * @param string $name The key to set. |
||
554 | * @param mixed $value The new value. |
||
555 | * @return $this |
||
556 | */ |
||
557 | 1 | public function setMeta($name, $value) { |
|
561 | |||
562 | /** |
||
563 | * Get the template loader. |
||
564 | * |
||
565 | * The template loader translates component names into template contents. |
||
566 | * |
||
567 | * @return TemplateLoaderInterface Returns the template loader. |
||
568 | */ |
||
569 | 1 | public function getTemplateLoader() { |
|
572 | |||
573 | /** |
||
574 | * Set the template loader. |
||
575 | * |
||
576 | * The template loader translates component names into template contents. |
||
577 | * |
||
578 | * @param TemplateLoaderInterface $templateLoader The new template loader. |
||
579 | * @return $this |
||
580 | */ |
||
581 | public function setTemplateLoader($templateLoader) { |
||
585 | |||
586 | /** |
||
587 | * Get the entire meta array. |
||
588 | * |
||
589 | * @return array Returns the meta. |
||
590 | */ |
||
591 | public function getMetaArray() { |
||
594 | |||
595 | /** |
||
596 | * Set the entire meta array. |
||
597 | * |
||
598 | * @param array $meta The new meta array. |
||
599 | * @return $this |
||
600 | */ |
||
601 | public function setMetaArray(array $meta) { |
||
605 | |||
606 | /** |
||
607 | * Return a dynamic attribute. |
||
608 | * |
||
609 | * The attribute renders differently depending on the value. |
||
610 | * |
||
611 | * - If the value is **true** then it will render as an HTML5 boolean attribute. |
||
612 | * - If the value is **false** or **null** then the attribute will not render. |
||
613 | * - Other values render as attribute values. |
||
614 | * - Attributes that start with **aria-** render **true** and **false** as values. |
||
615 | * |
||
616 | * @param string $name The name of the attribute. |
||
617 | * @param mixed $value The value of the attribute. |
||
618 | * @return string Returns the attribute definition or an empty string. |
||
619 | */ |
||
620 | 20 | public function attribute($name, $value) { |
|
632 | |||
633 | /** |
||
634 | * Escape a value for echoing to HTML with a bit of non-scalar checking. |
||
635 | * |
||
636 | * @param mixed $val The value to escape. |
||
637 | * @return string The escaped value. |
||
638 | */ |
||
639 | 27 | public function escape($val = null) { |
|
650 | |||
651 | /** |
||
652 | * Write children blocks. |
||
653 | * |
||
654 | * @param array|callable|null $children The children blocks to write. |
||
655 | */ |
||
656 | 4 | public function writeChildren($children) { |
|
665 | } |
||
666 |
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.