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 | 48 | public function __construct(TemplateLoaderInterface $templateLoader, $cachePath, CompilerInterface $compiler = null) { |
|
45 | 48 | $this->templateLoader = $templateLoader; |
|
46 | 48 | $this->cachePath = $cachePath; |
|
47 | 48 | $this->compiler = $compiler ?: new Compiler(); |
|
48 | |||
49 | 48 | $this->defineFunction('abs'); |
|
50 | 48 | $this->defineFunction('ceil'); |
|
51 | 48 | $this->defineFunction('componentExists', [$this, 'componentExists']); |
|
52 | 48 | $this->defineFunction('count'); |
|
53 | 48 | $this->defineFunction('formatDate', [$this, 'formatDate']); |
|
54 | 48 | $this->defineFunction('empty'); |
|
55 | 48 | $this->defineFunction('floor'); |
|
56 | 48 | $this->defineFunction('htmlEncode', 'htmlspecialchars'); |
|
57 | 48 | $this->defineFunction('join'); |
|
58 | 48 | $this->defineFunction('lcase', $this->mb('strtolower')); |
|
59 | 48 | $this->defineFunction('lcfirst'); |
|
60 | 48 | $this->defineFunction('ltrim'); |
|
61 | 48 | $this->defineFunction('max'); |
|
62 | 48 | $this->defineFunction('min'); |
|
63 | 48 | $this->defineFunction('queryEncode', 'http_build_query'); |
|
64 | 48 | $this->defineFunction('round'); |
|
65 | 48 | $this->defineFunction('rtrim'); |
|
66 | 48 | $this->defineFunction('sprintf'); |
|
67 | 48 | $this->defineFunction('strlen', $this->mb('strlen')); |
|
68 | 48 | $this->defineFunction('substr', $this->mb('substr')); |
|
69 | 48 | $this->defineFunction('trim'); |
|
70 | 48 | $this->defineFunction('ucase', $this->mb('strtoupper')); |
|
71 | 48 | $this->defineFunction('ucfirst'); |
|
72 | 48 | $this->defineFunction('ucwords'); |
|
73 | 48 | $this->defineFunction('urlencode', 'rawurlencode'); |
|
74 | |||
75 | 48 | $this->defineFunction('@class', [$this, 'attributeClass']); |
|
76 | |||
77 | // Define a simple component not found component to help troubleshoot. |
||
78 | 48 | $this->defineComponent('@component-not-found', function ($props) { |
|
79 | 1 | echo '<!-- Component "'.htmlspecialchars($props['component']).'" not found. -->'; |
|
80 | 48 | }); |
|
81 | |||
82 | // Define a simple component exception. |
||
83 | 48 | $this->defineComponent('@exception', function ($props) { |
|
84 | 1 | echo "\n<!--\nException in component \"".htmlspecialchars($props['component'])."\"\n". |
|
85 | 1 | htmlspecialchars($props['message'])."\n-->\n"; |
|
86 | |||
87 | 48 | }); |
|
88 | 48 | } |
|
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 | 48 | public function defineFunction($name, $function = null) { |
|
97 | 48 | if ($function === null) { |
|
98 | 48 | $function = $name; |
|
99 | } |
||
100 | |||
101 | 48 | $this->functions[strtolower($name)] = $function; |
|
102 | 48 | $this->compiler->defineFunction($name, $function); |
|
103 | 48 | } |
|
104 | |||
105 | 48 | 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) { |
|
116 | 11 | $component = strtolower($component); |
|
117 | |||
118 | try { |
||
119 | 11 | $callback = $this->lookup($component); |
|
120 | |||
121 | 11 | if (is_callable($callback)) { |
|
122 | 11 | call_user_func($callback, ...$args); |
|
123 | } else { |
||
124 | 11 | $this->write('@component-not-found', ['component' => $component]); |
|
125 | } |
||
126 | 1 | } catch (\Exception $ex) { |
|
127 | 1 | $this->write('@exception', ['message' => $ex->getMessage(), 'code', $ex->getCode(), 'component' => $component]); |
|
128 | 1 | return; |
|
129 | } |
||
130 | 11 | } |
|
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 | 43 | public function lookup($component) { |
|
139 | 43 | $component = strtolower($component); |
|
140 | 43 | $key = $this->componentKey($component); |
|
141 | |||
142 | 43 | if (!array_key_exists($key, $this->components)) { |
|
143 | 38 | $this->loadComponent($component); |
|
144 | } |
||
145 | |||
146 | 43 | if (isset($this->components[$key])) { |
|
147 | 42 | return $this->components[$key]; |
|
148 | } else { |
||
149 | // Mark a tombstone to the component array so it doesn't keep getting loaded. |
||
150 | 2 | $this->components[$key] = null; |
|
151 | 2 | return null; |
|
152 | } |
||
153 | } |
||
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 | 45 | protected function componentKey($component) { |
|
179 | 45 | if (false !== $pos = strpos($component, ':')) { |
|
180 | 1 | $component = substr($component, $pos + 1); |
|
181 | } |
||
182 | 45 | return strtolower($component); |
|
183 | } |
||
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 | 38 | 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 | 40 | 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 | 40 | 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 | 40 | private function filePutContents($path, $contents) { |
|
270 | 40 | if (!file_exists(dirname($path))) { |
|
271 | 4 | mkdir(dirname($path), 0777, true); |
|
272 | } |
||
273 | 40 | $tmpPath = tempnam(dirname($path), 'ebi-'); |
|
274 | 40 | $r = false; |
|
275 | 40 | if (file_put_contents($tmpPath, $contents) !== false) { |
|
276 | 40 | chmod($tmpPath, 0664); |
|
277 | 40 | $r = rename($tmpPath, $path); |
|
278 | } |
||
279 | |||
280 | 40 | if (function_exists('apc_delete_file')) { |
|
281 | // This fixes a bug with some configurations of apc. |
||
282 | @apc_delete_file($path); |
||
283 | 40 | } elseif (function_exists('opcache_invalidate')) { |
|
284 | 40 | @opcache_invalidate($path); |
|
285 | } |
||
286 | |||
287 | 40 | return $r; |
|
288 | } |
||
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 | 40 | 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 | 48 | 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 | 39 | 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 | 3 | 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 | 5 | public function attributeClass($expr) { |
|
372 | 5 | if (is_array($expr)) { |
|
373 | 3 | $classes = []; |
|
374 | 3 | foreach ($expr as $i => $val) { |
|
375 | 3 | if (is_array($val)) { |
|
376 | 1 | $classes[] = $this->attributeClass($val); |
|
377 | 3 | } elseif (is_int($i)) { |
|
378 | 1 | $classes[] = $val; |
|
379 | 2 | } elseif (!empty($val)) { |
|
380 | 3 | $classes[] = $i; |
|
381 | } |
||
382 | } |
||
383 | 3 | return implode(' ', $classes); |
|
384 | } else { |
||
385 | 2 | return (string)$expr; |
|
386 | } |
||
387 | } |
||
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') { |
|
398 | 1 | if (is_string($date)) { |
|
399 | try { |
||
400 | 1 | $date = new \DateTimeImmutable($date); |
|
401 | } catch (\Exception $ex) { |
||
402 | 1 | return '#error#'; |
|
403 | } |
||
404 | } elseif (empty($date)) { |
||
405 | return ''; |
||
406 | } elseif (is_int($date)) { |
||
407 | try { |
||
408 | $date = new \DateTimeImmutable('@'.$date); |
||
409 | } catch (\Exception $ex) { |
||
410 | return '#error#'; |
||
411 | } |
||
412 | } elseif (!$date instanceof \DateTimeInterface) { |
||
413 | return '#error#'; |
||
414 | } |
||
415 | |||
416 | 1 | return $date->format($format); |
|
417 | } |
||
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
@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.