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 |
||
23 | final class Template extends Base |
||
24 | { |
||
25 | |||
26 | /** Known engines */ |
||
27 | const ENGINE_TWIG = 'Slick\Template\Engine\Twig'; |
||
28 | |||
29 | /** Known engine extensions */ |
||
30 | const EXTENSION_TWIG_TEXT = 'Slick\Template\Extension\Text'; |
||
31 | const EXTENSION_TWIG_I18N = 'Slick\Template\Extension\I18n'; |
||
32 | |||
33 | /** @var string Engine interface */ |
||
34 | private static $interface = 'Slick\Template\TemplateEngineInterface'; |
||
35 | private static $extensionInterface = |
||
36 | 'Slick\Template\EngineExtensionInterface'; |
||
37 | |||
38 | /** |
||
39 | * @readwrite |
||
40 | * @var string The engine to use |
||
41 | */ |
||
42 | protected $engine = self::ENGINE_TWIG; |
||
43 | |||
44 | /** |
||
45 | * @readwrite |
||
46 | * @var array Options for template initializing |
||
47 | */ |
||
48 | protected $options = array(); |
||
49 | |||
50 | /** |
||
51 | * @var string[] a list of available paths |
||
52 | */ |
||
53 | protected static $paths = ['./']; |
||
54 | |||
55 | /** |
||
56 | * @var array Default options for template initializing |
||
57 | */ |
||
58 | protected static $defaultOptions = []; |
||
59 | |||
60 | /** |
||
61 | * @var array Array containing template extensions |
||
62 | */ |
||
63 | private static $extensions = [ |
||
64 | self::EXTENSION_TWIG_TEXT => null, |
||
65 | self::EXTENSION_TWIG_I18N => null, |
||
66 | ]; |
||
67 | |||
68 | /** |
||
69 | * Prepends a searchable path to available paths list. |
||
70 | * |
||
71 | * @param string $path |
||
72 | */ |
||
73 | 2 | View Code Duplication | public static function addPath($path) |
80 | |||
81 | /** |
||
82 | * Prepends a searchable path to available paths list. |
||
83 | * |
||
84 | * @param string $path |
||
85 | */ |
||
86 | 2 | View Code Duplication | public static function appendPath($path) |
93 | |||
94 | /** |
||
95 | * Gets the list of defined paths |
||
96 | * |
||
97 | * @return \string[] |
||
98 | */ |
||
99 | 4 | public static function getPaths() |
|
103 | |||
104 | /** |
||
105 | * Initializes the engine |
||
106 | * |
||
107 | * @throws Exception\InvalidArgumentException |
||
108 | * |
||
109 | * @return TemplateEngineInterface |
||
110 | */ |
||
111 | 4 | public function initialize() |
|
120 | |||
121 | /** |
||
122 | * Adds an extension to the template engine |
||
123 | * |
||
124 | * @param string|object $className The class name or an instance |
||
125 | * of EngineExtensionInterface interface |
||
126 | * |
||
127 | * @return self|$this|Template |
||
128 | */ |
||
129 | 6 | public function addExtension($className) |
|
139 | |||
140 | /** |
||
141 | * Registers the provided class name as an extension |
||
142 | * |
||
143 | * @param string|object $extension The class name or an instance |
||
144 | * of EngineExtensionInterface interface |
||
145 | * |
||
146 | * @return Template |
||
147 | */ |
||
148 | 2 | public static function register($extension) |
|
153 | |||
154 | /** |
||
155 | * Apply defined extensions to the provided template engine |
||
156 | * |
||
157 | * @param TemplateEngineInterface $engine |
||
158 | * |
||
159 | * @return TemplateEngineInterface |
||
160 | */ |
||
161 | 2 | protected function applyExtensions(TemplateEngineInterface $engine) |
|
171 | |||
172 | /** |
||
173 | * @param string $class |
||
174 | * @param EngineExtensionInterface $extension |
||
175 | * |
||
176 | * @return EngineExtensionInterface |
||
177 | */ |
||
178 | 2 | protected function getExtension($class, $extension) |
|
187 | |||
188 | /** |
||
189 | * Check if type is a valid configuration driver |
||
190 | * |
||
191 | * @param null $name |
||
192 | * @param null $interface |
||
193 | */ |
||
194 | 10 | protected function checkClass($name = null, $interface = null) |
|
212 | |||
213 | /** |
||
214 | * Get current configured extensions |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | 4 | public function getExtensions() |
|
222 | |||
223 | /** |
||
224 | * Set or reset the list of extensions |
||
225 | * |
||
226 | * @param array $extensions |
||
227 | * |
||
228 | * @return Template |
||
229 | */ |
||
230 | 2 | public function setExtensions(array $extensions) |
|
235 | |||
236 | /** |
||
237 | * Set default options |
||
238 | * |
||
239 | * If an array is given on $option parameter it should be assigned |
||
240 | * to the static $defaultOptions property. |
||
241 | * |
||
242 | * For other values only the key provided in $option parameter should |
||
243 | * be overridden. |
||
244 | * |
||
245 | * @param array|string|int $option |
||
246 | * |
||
247 | * @param mixed $value |
||
248 | */ |
||
249 | 4 | public static function setDefaultOptions($option, $value = null) |
|
259 | |||
260 | /** |
||
261 | * Get current default options |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | 4 | public static function getDefaultOptions() |
|
269 | } |
||
270 |
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.