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 | 43 | public function __construct(TemplateLoaderInterface $templateLoader, $cachePath, CompilerInterface $compiler = null) { |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Register a runtime function. |
||
| 92 | * |
||
| 93 | * @param string $name The name of the function. |
||
| 94 | * @param callable $function The function callback. |
||
| 95 | */ |
||
| 96 | 43 | public function defineFunction($name, $function = null) { |
|
| 104 | |||
| 105 | 43 | private function mb($func) { |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Write a component to the output buffer. |
||
| 111 | * |
||
| 112 | * @param string $component The name of the component. |
||
| 113 | * @param array ...$args |
||
| 114 | */ |
||
| 115 | 11 | public function write($component, ...$args) { |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Lookup a component with a given name. |
||
| 134 | * |
||
| 135 | * @param string $component The component to lookup. |
||
| 136 | * @return callable|null Returns the component function or **null** if the component is not found. |
||
| 137 | */ |
||
| 138 | 38 | public function lookup($component) { |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Check to see if a component exists. |
||
| 157 | * |
||
| 158 | * @param string $component The name of the component. |
||
| 159 | * @param bool $loader Whether or not to use the component loader or just look in the component cache. |
||
| 160 | * @return bool Returns **true** if the component exists or **false** otherwise. |
||
| 161 | */ |
||
| 162 | 2 | public function componentExists($component, $loader = true) { |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Strip the namespace off a component name to get the component key. |
||
| 174 | * |
||
| 175 | * @param string $component The full name of the component with a possible namespace. |
||
| 176 | * @return string Returns the component key. |
||
| 177 | */ |
||
| 178 | 40 | protected function componentKey($component) { |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Load a component. |
||
| 187 | * |
||
| 188 | * @param string $component The name of the component to load. |
||
| 189 | * @return callable|null Returns the component or **null** if the component isn't found. |
||
| 190 | */ |
||
| 191 | 36 | protected function loadComponent($component) { |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Check to see if a specific cache key exists in the cache. |
||
| 211 | * |
||
| 212 | * @param string $cacheKey The cache key to check. |
||
| 213 | * @return bool Returns **true** if there is a cache key at the file or **false** otherwise. |
||
| 214 | */ |
||
| 215 | 1 | public function cacheKeyExists($cacheKey) { |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Compile a component from source, cache it and include it. |
||
| 222 | * |
||
| 223 | * @param string $component The name of the component. |
||
| 224 | * @param string $src The component source. |
||
| 225 | * @param string $cacheKey The cache key of the component. |
||
| 226 | * @return callable|null Returns the compiled component closure. |
||
| 227 | */ |
||
| 228 | 35 | public function compile($component, $src, $cacheKey) { |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Include a cached component. |
||
| 242 | * |
||
| 243 | * @param string $component The component key. |
||
| 244 | * @param string $cachePath The path to the component. |
||
| 245 | * @return callable|null Returns the component function or **null** if the component wasn't properly defined. |
||
| 246 | */ |
||
| 247 | 35 | private function includeComponent($component, $cachePath) { |
|
| 261 | |||
| 262 | /** |
||
| 263 | * A safe version of {@link file_put_contents()} that also clears op caches. |
||
| 264 | * |
||
| 265 | * @param string $path The path to save to. |
||
| 266 | * @param string $contents The contents of the file. |
||
| 267 | * @return bool Returns **true** on success or **false** on failure. |
||
| 268 | */ |
||
| 269 | 35 | private function filePutContents($path, $contents) { |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Include a file. |
||
| 292 | * |
||
| 293 | * This is method is useful for including a file bound to this object instance. |
||
| 294 | * |
||
| 295 | * @param string $path The path to the file to include. |
||
| 296 | * @return mixed Returns the result of the include. |
||
| 297 | */ |
||
| 298 | 35 | public function requireFile($path) { |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Register a component. |
||
| 304 | * |
||
| 305 | * @param string $name The name of the component to register. |
||
| 306 | * @param callable $component The component function. |
||
| 307 | */ |
||
| 308 | 43 | public function defineComponent($name, callable $component) { |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Render a component to a string. |
||
| 314 | * |
||
| 315 | * @param string $component The name of the component to render. |
||
| 316 | * @param array ...$args Arguments to pass to the component. |
||
| 317 | * @return string|null Returns the rendered component or **null** if the component was not found. |
||
| 318 | */ |
||
| 319 | 34 | public function render($component, ...$args) { |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Set the error reporting appropriate for template rendering. |
||
| 335 | * |
||
| 336 | * @return int Returns the previous error level. |
||
| 337 | */ |
||
| 338 | public function setErrorReporting() { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Call a function registered with **defineFunction()**. |
||
| 345 | * |
||
| 346 | * If a static or global function is registered then it's simply rendered in the compiled template. |
||
| 347 | * This method is for closures or callbacks. |
||
| 348 | * |
||
| 349 | * @param string $name The name of the registered function. |
||
| 350 | * @param array ...$args The function's argument. |
||
| 351 | * @return mixed Returns the result of the function |
||
| 352 | * @throws RuntimeException Throws an exception when the function isn't found. |
||
| 353 | */ |
||
| 354 | 2 | public function call($name, ...$args) { |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Render a variable appropriately for CSS. |
||
| 364 | * |
||
| 365 | * This is a convenience runtime function. |
||
| 366 | * |
||
| 367 | * @param string|array $expr A CSS class, an array of CSS classes, or an associative array where the keys are class |
||
| 368 | * names and the values are truthy conditions to include the class (or not). |
||
| 369 | * @return string Returns a space-delimited CSS class string. |
||
| 370 | */ |
||
| 371 | 4 | public function attributeClass($expr) { |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Format a data. |
||
| 391 | * |
||
| 392 | * @param mixed $date The date to format. This can be a string data, a timestamp or an instance of **DateTimeInterface**. |
||
| 393 | * @param string $format The format of the date. |
||
| 394 | * @return string Returns the formatted data. |
||
| 395 | * @see date_format() |
||
| 396 | */ |
||
| 397 | 1 | public function formatDate($date, $format = 'c') { |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Get a single item from the meta array. |
||
| 421 | * |
||
| 422 | * @param string $name The key to get from. |
||
| 423 | * @param mixed $default The default value if no item at the key exists. |
||
| 424 | * @return mixed Returns the meta value. |
||
| 425 | */ |
||
| 426 | public function getMeta($name, $default = null) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Set a single item to the meta array. |
||
| 432 | * |
||
| 433 | * @param string $name The key to set. |
||
| 434 | * @param mixed $value The new value. |
||
| 435 | * @return $this |
||
| 436 | */ |
||
| 437 | 1 | public function setMeta($name, $value) { |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Get the template loader. |
||
| 444 | * |
||
| 445 | * The template loader translates component names into template contents. |
||
| 446 | * |
||
| 447 | * @return TemplateLoaderInterface Returns the template loader. |
||
| 448 | */ |
||
| 449 | 1 | public function getTemplateLoader() { |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Set the template loader. |
||
| 455 | * |
||
| 456 | * The template loader translates component names into template contents. |
||
| 457 | * |
||
| 458 | * @param TemplateLoaderInterface $templateLoader The new template loader. |
||
| 459 | * @return $this |
||
| 460 | */ |
||
| 461 | public function setTemplateLoader($templateLoader) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get the entire meta array. |
||
| 468 | * |
||
| 469 | * @return array Returns the meta. |
||
| 470 | */ |
||
| 471 | public function getMetaArray() { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Set the entire meta array. |
||
| 477 | * |
||
| 478 | * @param array $meta The new meta array. |
||
| 479 | * @return $this |
||
| 480 | */ |
||
| 481 | public function setMetaArray(array $meta) { |
||
| 485 | } |
||
| 486 |
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.