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 | 57 | public function __construct(TemplateLoaderInterface $templateLoader, $cachePath, CompilerInterface $compiler = null) { |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Register a runtime function. |
||
| 103 | * |
||
| 104 | * @param string $name The name of the function. |
||
| 105 | * @param callable $function The function callback. |
||
| 106 | */ |
||
| 107 | 57 | public function defineFunction($name, $function = null) { |
|
| 115 | |||
| 116 | 57 | private function mb($func) { |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Write a component to the output buffer. |
||
| 122 | * |
||
| 123 | * @param string $component The name of the component. |
||
| 124 | * @param array ...$args |
||
| 125 | */ |
||
| 126 | 13 | public function write($component, ...$args) { |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Lookup a component with a given name. |
||
| 145 | * |
||
| 146 | * @param string $component The component to lookup. |
||
| 147 | * @return callable|null Returns the component function or **null** if the component is not found. |
||
| 148 | */ |
||
| 149 | 52 | public function lookup($component) { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Check to see if a component exists. |
||
| 168 | * |
||
| 169 | * @param string $component The name of the component. |
||
| 170 | * @param bool $loader Whether or not to use the component loader or just look in the component cache. |
||
| 171 | * @return bool Returns **true** if the component exists or **false** otherwise. |
||
| 172 | */ |
||
| 173 | 2 | public function componentExists($component, $loader = true) { |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Strip the namespace off a component name to get the component key. |
||
| 185 | * |
||
| 186 | * @param string $component The full name of the component with a possible namespace. |
||
| 187 | * @return string Returns the component key. |
||
| 188 | */ |
||
| 189 | 54 | protected function componentKey($component) { |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Load a component. |
||
| 198 | * |
||
| 199 | * @param string $component The name of the component to load. |
||
| 200 | * @return callable|null Returns the component or **null** if the component isn't found. |
||
| 201 | */ |
||
| 202 | 47 | protected function loadComponent($component) { |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Check to see if a specific cache key exists in the cache. |
||
| 222 | * |
||
| 223 | * @param string $cacheKey The cache key to check. |
||
| 224 | * @return bool Returns **true** if there is a cache key at the file or **false** otherwise. |
||
| 225 | */ |
||
| 226 | 1 | public function cacheKeyExists($cacheKey) { |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Compile a component from source, cache it and include it. |
||
| 233 | * |
||
| 234 | * @param string $component The name of the component. |
||
| 235 | * @param string $src The component source. |
||
| 236 | * @param string $cacheKey The cache key of the component. |
||
| 237 | * @return callable|null Returns the compiled component closure. |
||
| 238 | */ |
||
| 239 | 49 | public function compile($component, $src, $cacheKey) { |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Include a cached component. |
||
| 253 | * |
||
| 254 | * @param string $component The component key. |
||
| 255 | * @param string $cachePath The path to the component. |
||
| 256 | * @return callable|null Returns the component function or **null** if the component wasn't properly defined. |
||
| 257 | */ |
||
| 258 | 49 | private function includeComponent($component, $cachePath) { |
|
| 272 | |||
| 273 | /** |
||
| 274 | * A safe version of {@link file_put_contents()} that also clears op caches. |
||
| 275 | * |
||
| 276 | * @param string $path The path to save to. |
||
| 277 | * @param string $contents The contents of the file. |
||
| 278 | * @return bool Returns **true** on success or **false** on failure. |
||
| 279 | */ |
||
| 280 | 49 | private function filePutContents($path, $contents) { |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Include a file. |
||
| 303 | * |
||
| 304 | * This is method is useful for including a file bound to this object instance. |
||
| 305 | * |
||
| 306 | * @param string $path The path to the file to include. |
||
| 307 | * @return mixed Returns the result of the include. |
||
| 308 | */ |
||
| 309 | 49 | public function requireFile($path) { |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Register a component. |
||
| 315 | * |
||
| 316 | * @param string $name The name of the component to register. |
||
| 317 | * @param callable $component The component function. |
||
| 318 | */ |
||
| 319 | 57 | public function defineComponent($name, callable $component) { |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Render a component to a string. |
||
| 325 | * |
||
| 326 | * @param string $component The name of the component to render. |
||
| 327 | * @param array ...$args Arguments to pass to the component. |
||
| 328 | * @return string|null Returns the rendered component or **null** if the component was not found. |
||
| 329 | */ |
||
| 330 | 48 | public function render($component, ...$args) { |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Set the error reporting appropriate for template rendering. |
||
| 346 | * |
||
| 347 | * @return int Returns the previous error level. |
||
| 348 | */ |
||
| 349 | public function setErrorReporting() { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Call a function registered with **defineFunction()**. |
||
| 356 | * |
||
| 357 | * If a static or global function is registered then it's simply rendered in the compiled template. |
||
| 358 | * This method is for closures or callbacks. |
||
| 359 | * |
||
| 360 | * @param string $name The name of the registered function. |
||
| 361 | * @param array ...$args The function's argument. |
||
| 362 | * @return mixed Returns the result of the function |
||
| 363 | * @throws RuntimeException Throws an exception when the function isn't found. |
||
| 364 | */ |
||
| 365 | 3 | public function call($name, ...$args) { |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Render a variable appropriately for CSS. |
||
| 375 | * |
||
| 376 | * This is a convenience runtime function. |
||
| 377 | * |
||
| 378 | * @param string|array $expr A CSS class, an array of CSS classes, or an associative array where the keys are class |
||
| 379 | * names and the values are truthy conditions to include the class (or not). |
||
| 380 | * @return string Returns a space-delimited CSS class string. |
||
| 381 | */ |
||
| 382 | 5 | public function attributeClass($expr) { |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Format a data. |
||
| 402 | * |
||
| 403 | * @param mixed $date The date to format. This can be a string data, a timestamp or an instance of **DateTimeInterface**. |
||
| 404 | * @param string $format The format of the date. |
||
| 405 | * @return string Returns the formatted data. |
||
| 406 | * @see date_format() |
||
| 407 | */ |
||
| 408 | 1 | public function formatDate($date, $format = 'c') { |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Get a single item from the meta array. |
||
| 432 | * |
||
| 433 | * @param string $name The key to get from. |
||
| 434 | * @param mixed $default The default value if no item at the key exists. |
||
| 435 | * @return mixed Returns the meta value. |
||
| 436 | */ |
||
| 437 | public function getMeta($name, $default = null) { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Set a single item to the meta array. |
||
| 443 | * |
||
| 444 | * @param string $name The key to set. |
||
| 445 | * @param mixed $value The new value. |
||
| 446 | * @return $this |
||
| 447 | */ |
||
| 448 | 1 | public function setMeta($name, $value) { |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Get the template loader. |
||
| 455 | * |
||
| 456 | * The template loader translates component names into template contents. |
||
| 457 | * |
||
| 458 | * @return TemplateLoaderInterface Returns the template loader. |
||
| 459 | */ |
||
| 460 | 1 | public function getTemplateLoader() { |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Set the template loader. |
||
| 466 | * |
||
| 467 | * The template loader translates component names into template contents. |
||
| 468 | * |
||
| 469 | * @param TemplateLoaderInterface $templateLoader The new template loader. |
||
| 470 | * @return $this |
||
| 471 | */ |
||
| 472 | public function setTemplateLoader($templateLoader) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get the entire meta array. |
||
| 479 | * |
||
| 480 | * @return array Returns the meta. |
||
| 481 | */ |
||
| 482 | public function getMetaArray() { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Set the entire meta array. |
||
| 488 | * |
||
| 489 | * @param array $meta The new meta array. |
||
| 490 | * @return $this |
||
| 491 | */ |
||
| 492 | public function setMetaArray(array $meta) { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Return a dynamic attribute. |
||
| 499 | * |
||
| 500 | * The attribute renders differently depending on the value. |
||
| 501 | * |
||
| 502 | * - If the value is **true** then it will render as an HTML5 boolean attribute. |
||
| 503 | * - If the value is **false** or **null** then the attribute will not render. |
||
| 504 | * - Other values render as attribute values. |
||
| 505 | * - Attributes that start with **aria-** render **true** and **false** as values. |
||
| 506 | * |
||
| 507 | * @param string $name The name of the attribute. |
||
| 508 | * @param mixed $value The value of the attribute. |
||
| 509 | * @return string Returns the attribute definition or an empty string. |
||
| 510 | */ |
||
| 511 | 14 | protected function attribute($name, $value) { |
|
| 523 | } |
||
| 524 |
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.