Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 18 | abstract class AbstractLoader implements |
||
| 19 | LoggerAwareInterface, |
||
| 20 | LoaderInterface |
||
| 21 | { |
||
| 22 | use LoggerAwareTrait; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $basePath = ''; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string[] |
||
| 31 | */ |
||
| 32 | private $paths = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $dynamicTemplates = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Default constructor, if none is provided by the concrete class implementations. |
||
| 41 | * |
||
| 42 | * ## Required dependencies |
||
| 43 | * - `logger` A PSR-3 logger |
||
| 44 | * |
||
| 45 | * @param array $data The class dependencies map. |
||
| 46 | */ |
||
| 47 | public function __construct(array $data = null) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Load a template content |
||
| 56 | * |
||
| 57 | * @param string $ident The template ident to load and render. |
||
| 58 | * @throws InvalidArgumentException If the dynamic template identifier is not a string. |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | public function load($ident) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | protected function basePath() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @see FileLoader::path() |
||
| 99 | * @return string[] |
||
| 100 | */ |
||
| 101 | protected function paths() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the template file (full path + filename) to load from an ident. |
||
| 108 | * |
||
| 109 | * This method first generates the filename for an identifier and search for it in all of the loader's paths. |
||
| 110 | * |
||
| 111 | * @param string $ident The template identifier to load. |
||
| 112 | * @throws InvalidArgumentException If the template ident is not a string. |
||
| 113 | * @return string|null The full path + filename of the found template. Null if nothing was found. |
||
| 114 | */ |
||
| 115 | protected function findTemplateFile($ident) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $ident The template identifier to convert to a filename. |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | abstract protected function filenameFromIdent($ident); |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param string[] $paths The list of path to add. |
||
| 143 | * @return LoaderInterface Chainable |
||
| 144 | */ |
||
| 145 | private function setPaths(array $paths) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $basePath The base path to set. |
||
| 158 | * @throws InvalidArgumentException If the base path parameter is not a string. |
||
| 159 | * @return LoaderInterface Chainable |
||
| 160 | */ |
||
| 161 | private function setBasePath($basePath) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $path The path to add to the load. |
||
| 175 | * @return LoaderInterface Chainable |
||
| 176 | */ |
||
| 177 | private function addPath($path) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param string $path The path to resolve. |
||
| 186 | * @throws InvalidArgumentException If the path argument is not a string. |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | private function resolvePath($path) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $varName The name of the variable to set this template unto. |
||
| 208 | * @param string|null $templateIdent The "dynamic template" to set. null to clear. |
||
| 209 | * @throws InvalidArgumentException If var name is not a string or if the template is not a string (and not null). |
||
| 210 | * @return void |
||
| 211 | */ |
||
| 212 | View Code Duplication | public function setDynamicTemplate($varName, $templateIdent) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $varName The name of the variable to get template ident from. |
||
| 236 | * @throws InvalidArgumentException If the var name is not a string. |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function dynamicTemplate($varName) |
||
| 253 | } |
||
| 254 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: