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 declare(strict_types=1); |
||
26 | class TwigView extends View |
||
27 | { |
||
28 | const EXT = '.twig'; |
||
29 | |||
30 | const ENV_CONFIG = 'WyriHaximus.TwigView.environment'; |
||
31 | |||
32 | /** |
||
33 | * Extension to use. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $_ext = self::EXT; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $extensions = [ |
||
43 | self::EXT, |
||
44 | '.tpl', |
||
45 | '.ctp', |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * Twig instance. |
||
50 | * |
||
51 | * @var \Twig\Environment |
||
52 | */ |
||
53 | protected $twig; |
||
54 | |||
55 | /** |
||
56 | * Return empty string when View instance is cast to string. |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | public function __toString(): string |
||
64 | |||
65 | /** |
||
66 | * Initialize view. |
||
67 | * |
||
68 | */ |
||
69 | 8 | public function initialize() |
|
79 | |||
80 | /** |
||
81 | * @param string $extension |
||
82 | */ |
||
83 | public function unshiftExtension($extension) |
||
87 | |||
88 | /** |
||
89 | * Get twig environment instance. |
||
90 | * |
||
91 | * @return \Twig\Environment |
||
92 | */ |
||
93 | 3 | public function getTwig(): Environment |
|
97 | |||
98 | /** |
||
99 | * @return array |
||
100 | */ |
||
101 | 8 | protected function resolveConfig(): array |
|
125 | |||
126 | /** |
||
127 | * @return array |
||
128 | */ |
||
129 | 8 | protected function readConfig(): array |
|
142 | |||
143 | /** |
||
144 | * Create the template loader. |
||
145 | * |
||
146 | * @return \WyriHaximus\TwigView\Lib\Twig\Loader |
||
147 | */ |
||
148 | 8 | protected function getLoader(): Loader |
|
155 | |||
156 | /** |
||
157 | * Render the template. |
||
158 | * |
||
159 | * @param string $viewFile Template file. |
||
160 | * @param array $data Data that can be used by the template. |
||
161 | * |
||
162 | * @throws \Exception |
||
163 | * @return string |
||
164 | */ |
||
165 | 4 | protected function _render($viewFile, $data = []): string |
|
197 | |||
198 | /** |
||
199 | * @param string|null $name |
||
200 | * @throws \Exception |
||
201 | * @return string |
||
202 | */ |
||
203 | 2 | View Code Duplication | protected function _getViewFileName($name = null): string |
217 | |||
218 | /** |
||
219 | * @param string|null $name |
||
220 | * @throws \Exception |
||
221 | * @return string |
||
222 | */ |
||
223 | View Code Duplication | protected function _getLayoutFileName($name = null): string |
|
237 | |||
238 | /** |
||
239 | * @param string $name |
||
240 | * @param bool $pluginCheck |
||
241 | * @return string|bool |
||
242 | */ |
||
243 | protected function _getElementFileName($name, $pluginCheck = true) |
||
255 | } |
||
256 |
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.