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 |
||
21 | class Template |
||
22 | { |
||
23 | |||
24 | /** Known engines */ |
||
25 | const ENGINE_TWIG = Twig::class; |
||
26 | |||
27 | /** Known engine extensions */ |
||
28 | const EXTENSION_TWIG_TEXT = Text::class; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $defaultOptions = []; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private $options; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $engine; |
||
44 | |||
45 | /** |
||
46 | * @var string[] a list of available paths |
||
47 | */ |
||
48 | private static $paths = ['./']; |
||
49 | |||
50 | /** |
||
51 | * @var array Array containing template extensions |
||
52 | */ |
||
53 | private static $extensions = [ |
||
54 | self::EXTENSION_TWIG_TEXT => null, |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * Creates a template factory |
||
59 | * |
||
60 | * @param array $options |
||
61 | */ |
||
62 | public function __construct(array $options = []) |
||
72 | |||
73 | 2 | /** |
|
74 | * Prepends a searchable path to available paths list. |
||
75 | 2 | * |
|
76 | 2 | * @param string $path |
|
77 | 2 | */ |
|
78 | 1 | View Code Duplication | public static function addPath($path) |
85 | |||
86 | 2 | /** |
|
87 | * Appends a searchable path to available paths list. |
||
88 | 2 | * |
|
89 | 2 | * @param string $path |
|
90 | 2 | */ |
|
91 | 1 | View Code Duplication | public static function appendPath($path) |
98 | |||
99 | 4 | /** |
|
100 | * Adds an extension to the template engine |
||
101 | 4 | * |
|
102 | * @param string|object $className The class name or an instance |
||
103 | * of EngineExtensionInterface interface |
||
104 | * |
||
105 | * @return self|$this|Template |
||
106 | */ |
||
107 | public function addExtension($className) |
||
117 | 2 | ||
118 | 2 | /** |
|
119 | * Initializes the engine |
||
120 | * |
||
121 | * @return TemplateEngineInterface |
||
122 | */ |
||
123 | public function initialize() |
||
131 | 6 | ||
132 | 6 | /** |
|
133 | * Checks if provided class implements the TemplateEngineInterface |
||
134 | 6 | * |
|
135 | * @param string $engine |
||
136 | 4 | * |
|
137 | 4 | * @return string |
|
138 | */ |
||
139 | private function checkEngine($engine) |
||
149 | |||
150 | 2 | /** |
|
151 | 2 | * Apply defined extensions to the provided template engine |
|
152 | * |
||
153 | * @param TemplateEngineInterface $engine |
||
154 | * |
||
155 | * @return TemplateEngineInterface |
||
156 | */ |
||
157 | private function applyExtensions(TemplateEngineInterface $engine) |
||
167 | 1 | ||
168 | 1 | /** |
|
169 | 2 | * Creates the extension |
|
170 | * |
||
171 | * @param string $class |
||
172 | * @param EngineExtensionInterface $extension |
||
173 | * |
||
174 | * @return EngineExtensionInterface |
||
175 | */ |
||
176 | private function getExtension($class, $extension) |
||
184 | 2 | ||
185 | 2 | /** |
|
186 | * Checks if provided class implements the EngineExtensionInterface |
||
187 | * |
||
188 | * @param string $class |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | private function checkExtension($class) |
||
202 | } |
||
203 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.