Complex classes like AbstractLoader 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 AbstractLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | abstract class AbstractLoader implements |
||
| 22 | LoggerAwareInterface, |
||
| 23 | LoaderInterface |
||
| 24 | { |
||
| 25 | use LoggerAwareTrait; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $basePath = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string[] |
||
| 34 | */ |
||
| 35 | private $paths = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var boolean |
||
| 39 | */ |
||
| 40 | private $silent = true; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var LoaderRegistryInterface |
||
| 44 | */ |
||
| 45 | private $templateRegistry; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Default constructor, if none is provided by the concrete class implementations. |
||
| 49 | * |
||
| 50 | * ## Required dependencies |
||
| 51 | * - `logger` A PSR-3 logger |
||
| 52 | * |
||
| 53 | * @param array $data The class dependencies map. |
||
| 54 | */ |
||
| 55 | public function __construct(array $data = null) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Load a template content |
||
| 74 | * |
||
| 75 | * @param string $ident The template ident to load and render. |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public function load($ident) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Converts a dynamic template identifer into a template path. |
||
| 101 | * |
||
| 102 | * @param string $ident The template key. |
||
| 103 | * @throws InvalidArgumentException If the legacy dynamic template is empty. |
||
| 104 | * @return string|null The template path or the template key. |
||
| 105 | */ |
||
| 106 | public function resolve($ident) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $varName The name of the variable to get template ident from. |
||
| 151 | * @return string|null Returns the template ident or NULL if no template found. |
||
| 152 | */ |
||
| 153 | public function dynamicTemplate($varName) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $varName The name of the variable to get template ident from. |
||
| 160 | * @return boolean |
||
| 161 | */ |
||
| 162 | public function hasDynamicTemplate($varName) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $varName The name of the variable to set this template unto. |
||
| 169 | * @param string|null $templateIdent The "dynamic template" to set or NULL to clear. |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | public function setDynamicTemplate($varName, $templateIdent) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $varName The name of the variable to remove. |
||
| 189 | * @return void |
||
| 190 | */ |
||
| 191 | public function removeDynamicTemplate($varName) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return void |
||
| 203 | */ |
||
| 204 | public function clearDynamicTemplates() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | protected function basePath() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $basePath The base path to set. |
||
| 222 | * @throws InvalidArgumentException If the base path parameter is not a string. |
||
| 223 | * @return LoaderInterface Chainable |
||
| 224 | */ |
||
| 225 | private function setBasePath($basePath) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return string[] |
||
| 239 | */ |
||
| 240 | protected function paths() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param string[] $paths The list of path to add. |
||
| 247 | * @return LoaderInterface Chainable |
||
| 248 | */ |
||
| 249 | private function setPaths(array $paths) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $path The path to add to the load. |
||
| 262 | * @return LoaderInterface Chainable |
||
| 263 | */ |
||
| 264 | private function addPath($path) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $path The path to resolve. |
||
| 273 | * @throws InvalidArgumentException If the path argument is not a string. |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | private function resolvePath($path) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get error reporting. |
||
| 295 | * |
||
| 296 | * @return boolean Whether to be quiet when an error occurs. |
||
| 297 | */ |
||
| 298 | public function silent() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Set error reporting. |
||
| 305 | * |
||
| 306 | * @param boolean $silent Whether to be quiet when an error occurs. |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | private function setSilent($silent) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return LoaderRegistryInterface |
||
| 316 | */ |
||
| 317 | public function templateRegistry() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param LoaderRegistryInterface $registry A loader registry instance. |
||
| 324 | * @return void |
||
| 325 | */ |
||
| 326 | private function setTemplateRegistry(LoaderRegistryInterface $registry) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get the template file (full path + filename) to load from an ident. |
||
| 333 | * |
||
| 334 | * This method first generates the filename for an identifier and search for it in all of the loader's paths. |
||
| 335 | * |
||
| 336 | * @param string $ident The template identifier to load. |
||
| 337 | * @throws InvalidArgumentException If the template ident is not a string. |
||
| 338 | * @return string|null The full path + filename of the found template. Null if nothing was found. |
||
| 339 | */ |
||
| 340 | protected function findTemplateFile($ident) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param string $ident The template identifier to convert to a filename. |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | abstract protected function filenameFromIdent($ident); |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Handle when a template is not found. |
||
| 369 | * |
||
| 370 | * @param string $ident The template ident. |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | abstract protected function handleNotFound($ident); |
||
| 374 | } |
||
| 375 |