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 |
||
18 | class MustacheEngine extends AbstractEngine |
||
19 | { |
||
20 | const DEFAULT_CACHE_PATH = '../cache/mustache'; |
||
21 | |||
22 | /** |
||
23 | * A collection of helpers. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | private $helpers = []; |
||
28 | |||
29 | /** |
||
30 | * The renderering framework. |
||
31 | * |
||
32 | * @var Mustache_Engine |
||
33 | */ |
||
34 | private $mustache; |
||
35 | |||
36 | /** |
||
37 | * @return string |
||
38 | */ |
||
39 | public function type(): string |
||
43 | |||
44 | /** |
||
45 | * Build the Mustache Engine with an array of dependencies. |
||
46 | * |
||
47 | * @param array $data Engine dependencie. |
||
48 | */ |
||
49 | public function __construct(array $data) |
||
57 | |||
58 | /** |
||
59 | * Set the engine's helpers. |
||
60 | * |
||
61 | * @param array|Traversable|HelpersInterface $helpers Mustache helpers. |
||
62 | * @throws InvalidArgumentException If the given helper(s) are invalid. |
||
63 | * @return self |
||
64 | */ |
||
65 | View Code Duplication | public function setHelpers($helpers) |
|
85 | |||
86 | /** |
||
87 | * Merge (replacing or adding) the engine's helpers. |
||
88 | * |
||
89 | * @param array|Traversable|HelpersInterface $helpers Mustache helpers. |
||
90 | * @throws InvalidArgumentException If the given helper(s) are invalid. |
||
91 | * @return self |
||
92 | */ |
||
93 | View Code Duplication | public function mergeHelpers($helpers) |
|
112 | |||
113 | /** |
||
114 | * Add a helper. |
||
115 | * |
||
116 | * @param string $name The tag name. |
||
117 | * @param mixed $helper The tag value. |
||
118 | * @throws RuntimeException If the mustache engine was already initialized. |
||
119 | * @return self |
||
120 | */ |
||
121 | public function addHelper(string $name, $helper) |
||
133 | |||
134 | /** |
||
135 | * Retrieve the engine's helpers. |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | public function helpers(): array |
||
143 | |||
144 | /** |
||
145 | * @param string $templateIdent The template identifier to load and render. |
||
146 | * @param mixed $context The rendering context. |
||
147 | * @return string The rendered template string. |
||
148 | */ |
||
149 | public function render(string $templateIdent, $context): string |
||
153 | |||
154 | /** |
||
155 | * @param string $templateString The template string to render. |
||
156 | * @param mixed $context The rendering context. |
||
157 | * @return string The rendered template string. |
||
158 | */ |
||
159 | public function renderTemplate(string $templateString, $context): string |
||
163 | |||
164 | /** |
||
165 | * @return Mustache_Engine |
||
166 | */ |
||
167 | protected function mustache(): Mustache_Engine |
||
175 | |||
176 | /** |
||
177 | * @return Mustache_Engine |
||
178 | */ |
||
179 | protected function createMustache(): Mustache_Engine |
||
191 | |||
192 | /** |
||
193 | * Set the engine's cache implementation. |
||
194 | * |
||
195 | * @param mixed $cache A Mustache cache option. |
||
196 | * @return void |
||
197 | */ |
||
198 | protected function setCache($cache): void |
||
210 | } |
||
211 |
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.