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 | 60 | 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 | 60 | public function defineFunction($name, $function = null) { |
|
117 | |||
118 | 60 | 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 | 13 | 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 | 55 | 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 | 57 | 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 | 50 | protected function loadComponent($component) { |
|
231 | |||
232 | protected function writeCompileException($props) { |
||
275 | |||
276 | /** |
||
277 | * Check to see if a specific cache key exists in the cache. |
||
278 | * |
||
279 | * @param string $cacheKey The cache key to check. |
||
280 | * @return bool Returns **true** if there is a cache key at the file or **false** otherwise. |
||
281 | */ |
||
282 | 1 | public function cacheKeyExists($cacheKey) { |
|
286 | |||
287 | /** |
||
288 | * Compile a component from source, cache it and include it. |
||
289 | * |
||
290 | * @param string $component The name of the component. |
||
291 | * @param string $src The component source. |
||
292 | * @param string $cacheKey The cache key of the component. |
||
293 | * @return callable|null Returns the compiled component closure. |
||
294 | */ |
||
295 | 51 | public function compile($component, $src, $cacheKey) { |
|
306 | |||
307 | /** |
||
308 | * Include a cached component. |
||
309 | * |
||
310 | * @param string $component The component key. |
||
311 | * @param string $cachePath The path to the component. |
||
312 | * @return callable|null Returns the component function or **null** if the component wasn't properly defined. |
||
313 | */ |
||
314 | 51 | private function includeComponent($component, $cachePath) { |
|
328 | |||
329 | /** |
||
330 | * A safe version of {@link file_put_contents()} that also clears op caches. |
||
331 | * |
||
332 | * @param string $path The path to save to. |
||
333 | * @param string $contents The contents of the file. |
||
334 | * @return bool Returns **true** on success or **false** on failure. |
||
335 | */ |
||
336 | 51 | private function filePutContents($path, $contents) { |
|
356 | |||
357 | /** |
||
358 | * Include a file. |
||
359 | * |
||
360 | * This is method is useful for including a file bound to this object instance. |
||
361 | * |
||
362 | * @param string $path The path to the file to include. |
||
363 | * @return mixed Returns the result of the include. |
||
364 | */ |
||
365 | 51 | public function requireFile($path) { |
|
368 | |||
369 | /** |
||
370 | * Register a component. |
||
371 | * |
||
372 | * @param string $name The name of the component to register. |
||
373 | * @param callable $component The component function. |
||
374 | */ |
||
375 | 60 | public function defineComponent($name, callable $component) { |
|
378 | |||
379 | /** |
||
380 | * Render a component to a string. |
||
381 | * |
||
382 | * @param string $component The name of the component to render. |
||
383 | * @param array ...$args Arguments to pass to the component. |
||
384 | * @return string|null Returns the rendered component or **null** if the component was not found. |
||
385 | */ |
||
386 | 51 | public function render($component, ...$args) { |
|
399 | |||
400 | /** |
||
401 | * Set the error reporting appropriate for template rendering. |
||
402 | * |
||
403 | * @return int Returns the previous error level. |
||
404 | */ |
||
405 | public function setErrorReporting() { |
||
409 | |||
410 | /** |
||
411 | * Call a function registered with **defineFunction()**. |
||
412 | * |
||
413 | * If a static or global function is registered then it's simply rendered in the compiled template. |
||
414 | * This method is for closures or callbacks. |
||
415 | * |
||
416 | * @param string $name The name of the registered function. |
||
417 | * @param array ...$args The function's argument. |
||
418 | * @return mixed Returns the result of the function |
||
419 | * @throws RuntimeException Throws an exception when the function isn't found. |
||
420 | */ |
||
421 | 3 | public function call($name, ...$args) { |
|
428 | |||
429 | /** |
||
430 | * Render a variable appropriately for CSS. |
||
431 | * |
||
432 | * This is a convenience runtime function. |
||
433 | * |
||
434 | * @param string|array $expr A CSS class, an array of CSS classes, or an associative array where the keys are class |
||
435 | * names and the values are truthy conditions to include the class (or not). |
||
436 | * @return string Returns a space-delimited CSS class string. |
||
437 | */ |
||
438 | 6 | public function attributeClass($expr) { |
|
455 | |||
456 | /** |
||
457 | * Format a data. |
||
458 | * |
||
459 | * @param mixed $date The date to format. This can be a string data, a timestamp or an instance of **DateTimeInterface**. |
||
460 | * @param string $format The format of the date. |
||
461 | * @return string Returns the formatted data. |
||
462 | * @see date_format() |
||
463 | */ |
||
464 | 1 | public function formatDate($date, $format = 'c') { |
|
485 | |||
486 | /** |
||
487 | * Get a single item from the meta array. |
||
488 | * |
||
489 | * @param string $name The key to get from. |
||
490 | * @param mixed $default The default value if no item at the key exists. |
||
491 | * @return mixed Returns the meta value. |
||
492 | */ |
||
493 | public function getMeta($name, $default = null) { |
||
496 | |||
497 | /** |
||
498 | * Set a single item to the meta array. |
||
499 | * |
||
500 | * @param string $name The key to set. |
||
501 | * @param mixed $value The new value. |
||
502 | * @return $this |
||
503 | */ |
||
504 | 1 | public function setMeta($name, $value) { |
|
508 | |||
509 | /** |
||
510 | * Get the template loader. |
||
511 | * |
||
512 | * The template loader translates component names into template contents. |
||
513 | * |
||
514 | * @return TemplateLoaderInterface Returns the template loader. |
||
515 | */ |
||
516 | 1 | public function getTemplateLoader() { |
|
519 | |||
520 | /** |
||
521 | * Set the template loader. |
||
522 | * |
||
523 | * The template loader translates component names into template contents. |
||
524 | * |
||
525 | * @param TemplateLoaderInterface $templateLoader The new template loader. |
||
526 | * @return $this |
||
527 | */ |
||
528 | public function setTemplateLoader($templateLoader) { |
||
532 | |||
533 | /** |
||
534 | * Get the entire meta array. |
||
535 | * |
||
536 | * @return array Returns the meta. |
||
537 | */ |
||
538 | public function getMetaArray() { |
||
541 | |||
542 | /** |
||
543 | * Set the entire meta array. |
||
544 | * |
||
545 | * @param array $meta The new meta array. |
||
546 | * @return $this |
||
547 | */ |
||
548 | public function setMetaArray(array $meta) { |
||
552 | |||
553 | /** |
||
554 | * Return a dynamic attribute. |
||
555 | * |
||
556 | * The attribute renders differently depending on the value. |
||
557 | * |
||
558 | * - If the value is **true** then it will render as an HTML5 boolean attribute. |
||
559 | * - If the value is **false** or **null** then the attribute will not render. |
||
560 | * - Other values render as attribute values. |
||
561 | * - Attributes that start with **aria-** render **true** and **false** as values. |
||
562 | * |
||
563 | * @param string $name The name of the attribute. |
||
564 | * @param mixed $value The value of the attribute. |
||
565 | * @return string Returns the attribute definition or an empty string. |
||
566 | */ |
||
567 | 15 | protected function attribute($name, $value) { |
|
579 | } |
||
580 |
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.