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) { |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Register a runtime function. |
||
| 110 | * |
||
| 111 | * @param string $name The name of the function. |
||
| 112 | * @param callable $function The function callback. |
||
| 113 | */ |
||
| 114 | 72 | public function defineFunction($name, $function = null) { |
|
| 122 | |||
| 123 | 72 | private function mb($func) { |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Write a component to the output buffer. |
||
| 129 | * |
||
| 130 | * @param string $component The name of the component. |
||
| 131 | * @param array ...$args |
||
| 132 | */ |
||
| 133 | 22 | public function write($component, ...$args) { |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Lookup a component with a given name. |
||
| 155 | * |
||
| 156 | * @param string $component The component to lookup. |
||
| 157 | * @return callable|null Returns the component function or **null** if the component is not found. |
||
| 158 | */ |
||
| 159 | 67 | public function lookup($component) { |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Check to see if a component exists. |
||
| 178 | * |
||
| 179 | * @param string $component The name of the component. |
||
| 180 | * @param bool $loader Whether or not to use the component loader or just look in the component cache. |
||
| 181 | * @return bool Returns **true** if the component exists or **false** otherwise. |
||
| 182 | */ |
||
| 183 | 2 | public function componentExists($component, $loader = true) { |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Strip the namespace off a component name to get the component key. |
||
| 195 | * |
||
| 196 | * @param string $component The full name of the component with a possible namespace. |
||
| 197 | * @return string Returns the component key. |
||
| 198 | */ |
||
| 199 | 69 | protected function componentKey($component) { |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Load a component. |
||
| 208 | * |
||
| 209 | * @param string $component The name of the component to load. |
||
| 210 | * @return callable|null Returns the component or **null** if the component isn't found. |
||
| 211 | */ |
||
| 212 | 62 | protected function loadComponent($component) { |
|
| 236 | |||
| 237 | 8 | protected function writeCompileException($props) { |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Check to see if a specific cache key exists in the cache. |
||
| 292 | * |
||
| 293 | * @param string $cacheKey The cache key to check. |
||
| 294 | * @return bool Returns **true** if there is a cache key at the file or **false** otherwise. |
||
| 295 | */ |
||
| 296 | 1 | public function cacheKeyExists($cacheKey) { |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Compile a component from source, cache it and include it. |
||
| 303 | * |
||
| 304 | * @param string $component The name of the component. |
||
| 305 | * @param string $src The component source. |
||
| 306 | * @param string $cacheKey The cache key of the component. |
||
| 307 | * @return callable|null Returns the compiled component closure. |
||
| 308 | */ |
||
| 309 | 64 | public function compile($component, $src, $cacheKey) { |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Include a cached component. |
||
| 323 | * |
||
| 324 | * @param string $component The component key. |
||
| 325 | * @param string $cachePath The path to the component. |
||
| 326 | * @return callable|null Returns the component function or **null** if the component wasn't properly defined. |
||
| 327 | */ |
||
| 328 | 56 | private function includeComponent($component, $cachePath) { |
|
| 342 | |||
| 343 | /** |
||
| 344 | * A safe version of {@link file_put_contents()} that also clears op caches. |
||
| 345 | * |
||
| 346 | * @param string $path The path to save to. |
||
| 347 | * @param string $contents The contents of the file. |
||
| 348 | * @return bool Returns **true** on success or **false** on failure. |
||
| 349 | */ |
||
| 350 | 56 | private function filePutContents($path, $contents) { |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Include a file. |
||
| 373 | * |
||
| 374 | * This is method is useful for including a file bound to this object instance. |
||
| 375 | * |
||
| 376 | * @param string $path The path to the file to include. |
||
| 377 | * @return mixed Returns the result of the include. |
||
| 378 | */ |
||
| 379 | 56 | public function requireFile($path) { |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Register a component. |
||
| 385 | * |
||
| 386 | * @param string $name The name of the component to register. |
||
| 387 | * @param callable $component The component function. |
||
| 388 | */ |
||
| 389 | 72 | public function defineComponent($name, callable $component) { |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Render a component to a string. |
||
| 395 | * |
||
| 396 | * @param string $component The name of the component to render. |
||
| 397 | * @param array ...$args Arguments to pass to the component. |
||
| 398 | * @return string|null Returns the rendered component or **null** if the component was not found. |
||
| 399 | */ |
||
| 400 | 63 | public function render($component, ...$args) { |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Set the error reporting appropriate for template rendering. |
||
| 416 | * |
||
| 417 | * @return int Returns the previous error level. |
||
| 418 | */ |
||
| 419 | public function setErrorReporting() { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Call a function registered with **defineFunction()**. |
||
| 426 | * |
||
| 427 | * If a static or global function is registered then it's simply rendered in the compiled template. |
||
| 428 | * This method is for closures or callbacks. |
||
| 429 | * |
||
| 430 | * @param string $name The name of the registered function. |
||
| 431 | * @param array ...$args The function's argument. |
||
| 432 | * @return mixed Returns the result of the function |
||
| 433 | * @throws RuntimeException Throws an exception when the function isn't found. |
||
| 434 | */ |
||
| 435 | 3 | public function call($name, ...$args) { |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Render a variable appropriately for CSS. |
||
| 445 | * |
||
| 446 | * This is a convenience runtime function. |
||
| 447 | * |
||
| 448 | * @param string|array $expr A CSS class, an array of CSS classes, or an associative array where the keys are class |
||
| 449 | * names and the values are truthy conditions to include the class (or not). |
||
| 450 | * @return string Returns a space-delimited CSS class string. |
||
| 451 | */ |
||
| 452 | 6 | public function attributeClass($expr) { |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Format a data. |
||
| 472 | * |
||
| 473 | * @param mixed $date The date to format. This can be a string data, a timestamp or an instance of **DateTimeInterface**. |
||
| 474 | * @param string $format The format of the date. |
||
| 475 | * @return string Returns the formatted data. |
||
| 476 | * @see date_format() |
||
| 477 | */ |
||
| 478 | 1 | public function formatDate($date, $format = 'c') { |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Get a single item from the meta array. |
||
| 502 | * |
||
| 503 | * @param string $name The key to get from. |
||
| 504 | * @param mixed $default The default value if no item at the key exists. |
||
| 505 | * @return mixed Returns the meta value. |
||
| 506 | */ |
||
| 507 | public function getMeta($name, $default = null) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Set a single item to the meta array. |
||
| 513 | * |
||
| 514 | * @param string $name The key to set. |
||
| 515 | * @param mixed $value The new value. |
||
| 516 | * @return $this |
||
| 517 | */ |
||
| 518 | 1 | public function setMeta($name, $value) { |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Get the template loader. |
||
| 525 | * |
||
| 526 | * The template loader translates component names into template contents. |
||
| 527 | * |
||
| 528 | * @return TemplateLoaderInterface Returns the template loader. |
||
| 529 | */ |
||
| 530 | 1 | public function getTemplateLoader() { |
|
| 533 | |||
| 534 | /** |
||
| 535 | * Set the template loader. |
||
| 536 | * |
||
| 537 | * The template loader translates component names into template contents. |
||
| 538 | * |
||
| 539 | * @param TemplateLoaderInterface $templateLoader The new template loader. |
||
| 540 | * @return $this |
||
| 541 | */ |
||
| 542 | public function setTemplateLoader($templateLoader) { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Get the entire meta array. |
||
| 549 | * |
||
| 550 | * @return array Returns the meta. |
||
| 551 | */ |
||
| 552 | public function getMetaArray() { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Set the entire meta array. |
||
| 558 | * |
||
| 559 | * @param array $meta The new meta array. |
||
| 560 | * @return $this |
||
| 561 | */ |
||
| 562 | public function setMetaArray(array $meta) { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Return a dynamic attribute. |
||
| 569 | * |
||
| 570 | * The attribute renders differently depending on the value. |
||
| 571 | * |
||
| 572 | * - If the value is **true** then it will render as an HTML5 boolean attribute. |
||
| 573 | * - If the value is **false** or **null** then the attribute will not render. |
||
| 574 | * - Other values render as attribute values. |
||
| 575 | * - Attributes that start with **aria-** render **true** and **false** as values. |
||
| 576 | * |
||
| 577 | * @param string $name The name of the attribute. |
||
| 578 | * @param mixed $value The value of the attribute. |
||
| 579 | * @return string Returns the attribute definition or an empty string. |
||
| 580 | */ |
||
| 581 | 15 | public function attribute($name, $value) { |
|
| 593 | |||
| 594 | /** |
||
| 595 | * Escape a value for echoing to HTML with a bit of non-scalar checking. |
||
| 596 | * |
||
| 597 | * @param mixed $val The value to escape. |
||
| 598 | * @return string The escaped value. |
||
| 599 | */ |
||
| 600 | 25 | public function escape($val = null) { |
|
| 611 | |||
| 612 | /** |
||
| 613 | * Write children blocks. |
||
| 614 | * |
||
| 615 | * @param array|callable|null $children The children blocks to write. |
||
| 616 | */ |
||
| 617 | 4 | public function writeChildren($children) { |
|
| 626 | } |
||
| 627 |
This check looks for
@paramannotations 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.