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 |
||
24 | final class Template extends Base |
||
25 | { |
||
26 | |||
27 | /** Known engines */ |
||
28 | const ENGINE_TWIG = 'Slick\Template\Engine\Twig'; |
||
29 | |||
30 | /** Known engine extensions */ |
||
31 | const EXTENSION_TWIG_TEXT = 'Slick\Template\Extension\Text'; |
||
32 | const EXTENSION_TWIG_I18N = 'Slick\Template\Extension\I18n'; |
||
33 | |||
34 | /** @var string Engine interface */ |
||
35 | private static $interface = 'Slick\Template\TemplateEngineInterface'; |
||
36 | private static $extensionInterface = |
||
37 | 'Slick\Template\EngineExtensionInterface'; |
||
38 | |||
39 | /** |
||
40 | * @readwrite |
||
41 | * @var string The engine to use |
||
42 | */ |
||
43 | protected $engine = self::ENGINE_TWIG; |
||
44 | |||
45 | /** |
||
46 | * @readwrite |
||
47 | * @var array Options for template initializing |
||
48 | */ |
||
49 | protected $options = array(); |
||
50 | |||
51 | /** |
||
52 | * @var string[] a list of available paths |
||
53 | */ |
||
54 | protected static $paths = ['./']; |
||
55 | |||
56 | /** |
||
57 | * @readwrite |
||
58 | * @var array Array containing template extensions |
||
59 | */ |
||
60 | protected $extensions = [ |
||
61 | self::EXTENSION_TWIG_TEXT => null, |
||
62 | self::EXTENSION_TWIG_I18N => null, |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * Prepends a searchable path to available paths list. |
||
67 | * |
||
68 | * @param string $path |
||
69 | */ |
||
70 | 2 | View Code Duplication | public static function addPath($path) |
77 | |||
78 | /** |
||
79 | * Prepends a searchable path to available paths list. |
||
80 | * |
||
81 | * @param string $path |
||
82 | */ |
||
83 | 2 | View Code Duplication | public static function appendPath($path) |
90 | |||
91 | /** |
||
92 | * Gets the list of defined paths |
||
93 | * |
||
94 | * @return \string[] |
||
95 | */ |
||
96 | 4 | public static function getPaths() |
|
100 | |||
101 | /** |
||
102 | * Initializes the engine |
||
103 | * |
||
104 | * @throws Exception\InvalidArgumentException |
||
105 | * |
||
106 | * @return TemplateEngineInterface |
||
107 | */ |
||
108 | 4 | public function initialize() |
|
116 | |||
117 | /** |
||
118 | * Adds an extension to the template engine |
||
119 | * |
||
120 | * @param string|object $className The class name or an instance |
||
121 | * of EngineExtensionInterface interface |
||
122 | * |
||
123 | * @return self|$this|Template |
||
124 | */ |
||
125 | 4 | public function addExtension($className) |
|
136 | |||
137 | /** |
||
138 | * Apply defined extensions to the provided template engine |
||
139 | * |
||
140 | * @param TemplateEngineInterface $engine |
||
141 | * |
||
142 | * @return TemplateEngineInterface |
||
143 | */ |
||
144 | 2 | protected function applyExtensions(TemplateEngineInterface $engine) |
|
154 | |||
155 | /** |
||
156 | * @param string $class |
||
157 | * @param EngineExtensionInterface $extension |
||
158 | * |
||
159 | * @return EngineExtensionInterface |
||
160 | */ |
||
161 | 2 | protected function getExtension($class, $extension) |
|
170 | |||
171 | /** |
||
172 | * Check if type is a valid configuration driver |
||
173 | * |
||
174 | * @param null $name |
||
175 | * @param null $interface |
||
176 | */ |
||
177 | 8 | protected function checkClass($name = null, $interface = null) |
|
195 | } |
||
196 |
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.