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 |
||
27 | class TwigEngine extends AbstractEngine |
||
28 | { |
||
29 | use BenchmarkTrait; |
||
30 | |||
31 | /** |
||
32 | * Set of class names (processors) to be applied to view sources befor giving it to Twig. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $modifiers = []; |
||
37 | |||
38 | /** |
||
39 | * @var \Twig_Environment |
||
40 | */ |
||
41 | protected $twig; |
||
42 | |||
43 | /** |
||
44 | * @var FilesInterface |
||
45 | */ |
||
46 | protected $files; |
||
47 | |||
48 | /** |
||
49 | * @var ContainerInterface |
||
50 | */ |
||
51 | protected $container; |
||
52 | |||
53 | /** |
||
54 | * TwigEngine constructor. |
||
55 | * |
||
56 | * @param EnvironmentInterface $environment |
||
57 | * @param LoaderInterface $loader |
||
58 | * @param FilesInterface|null $files |
||
59 | * @param ContainerInterface|null $container |
||
60 | * @param array $modifiers |
||
61 | * @param array $extensions |
||
62 | * @param array $options |
||
63 | */ |
||
64 | public function __construct( |
||
81 | |||
82 | /** |
||
83 | * Get associated twig environment. |
||
84 | * |
||
85 | * @return \Twig_Environment |
||
86 | */ |
||
87 | public function getTwig() |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | * |
||
95 | * @throws CompileException |
||
96 | */ |
||
97 | public function get(string $path): ViewInterface |
||
98 | { |
||
99 | $benchmark = $this->benchmark('load', $path); |
||
100 | |||
101 | try { |
||
102 | return new TwigView($this->twig->load($path)); |
||
103 | } catch (\Twig_Error_Syntax $exception) { |
||
104 | //Let's clarify exception location |
||
105 | throw CompileException::fromTwig($exception); |
||
106 | } finally { |
||
107 | $this->benchmark($benchmark); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function render(string $path, array $context = []): string |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | public function compile(string $path, bool $reset = false) |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | public function withLoader(LoaderInterface $loader): EngineInterface |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | * |
||
162 | * @return $this |
||
163 | */ |
||
164 | public function withEnvironment(EnvironmentInterface $environment): EngineInterface |
||
177 | |||
178 | /** |
||
179 | * Initiating twig environment. |
||
180 | * |
||
181 | * @param array $extensions |
||
182 | * @param array $options |
||
183 | * |
||
184 | * @return \Twig_Environment |
||
185 | */ |
||
186 | protected function makeTwig(array $extensions, array $options): \Twig_Environment |
||
203 | |||
204 | /** |
||
205 | * In most of cases twig will get view processors to be executed before twig itself, to run |
||
206 | * such processors we need special wrapper at top of environment. |
||
207 | * |
||
208 | * @return \Twig_LoaderInterface |
||
209 | */ |
||
210 | View Code Duplication | private function makeLoader(): \Twig_LoaderInterface |
|
225 | |||
226 | /** |
||
227 | * @return bool|TwigCache |
||
228 | */ |
||
229 | private function makeCache() |
||
237 | } |
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.