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 | class TemplateParser { |
||
24 | /** |
||
25 | * @var string The path to the Mustache templates |
||
26 | */ |
||
27 | protected $templateDir; |
||
28 | |||
29 | /** |
||
30 | * @var callable[] Array of cached rendering functions |
||
31 | */ |
||
32 | protected $renderers; |
||
33 | |||
34 | /** |
||
35 | * @var bool Always compile template files |
||
36 | */ |
||
37 | protected $forceRecompile = false; |
||
38 | |||
39 | /** |
||
40 | * @param string $templateDir |
||
41 | * @param bool $forceRecompile |
||
42 | */ |
||
43 | public function __construct( $templateDir = null, $forceRecompile = false ) { |
||
47 | |||
48 | /** |
||
49 | * Constructs the location of the the source Mustache template |
||
50 | * @param string $templateName The name of the template |
||
51 | * @return string |
||
52 | * @throws UnexpectedValueException If $templateName attempts upwards directory traversal |
||
53 | */ |
||
54 | protected function getTemplateFilename( $templateName ) { |
||
73 | |||
74 | /** |
||
75 | * Returns a given template function if found, otherwise throws an exception. |
||
76 | * @param string $templateName The name of the template (without file suffix) |
||
77 | * @return callable |
||
78 | * @throws RuntimeException |
||
79 | */ |
||
80 | protected function getTemplate( $templateName ) { |
||
136 | |||
137 | /** |
||
138 | * Wrapper for compile() function that verifies successful compilation and strips |
||
139 | * out the '<?php' part so that the code is ready for eval() |
||
140 | * @param string $fileContents Mustache code |
||
141 | * @param string $filename Name of the template |
||
142 | * @return string PHP code (without '<?php') |
||
143 | * @throws RuntimeException |
||
144 | */ |
||
145 | protected function compileForEval( $fileContents, $filename ) { |
||
160 | |||
161 | /** |
||
162 | * Compile the Mustache code into PHP code using LightnCandy |
||
163 | * @param string $code Mustache code |
||
164 | * @return string PHP code (with '<?php') |
||
165 | * @throws RuntimeException |
||
166 | */ |
||
167 | protected function compile( $code ) { |
||
182 | |||
183 | /** |
||
184 | * Returns HTML for a given template by calling the template function with the given args |
||
185 | * |
||
186 | * @code |
||
187 | * echo $templateParser->processTemplate( |
||
188 | * 'ExampleTemplate', |
||
189 | * [ |
||
190 | * 'username' => $user->getName(), |
||
191 | * 'message' => 'Hello!' |
||
192 | * ] |
||
193 | * ); |
||
194 | * @endcode |
||
195 | * @param string $templateName The name of the template |
||
196 | * @param mixed $args |
||
197 | * @param array $scopes |
||
198 | * @return string |
||
199 | */ |
||
200 | public function processTemplate( $templateName, $args, array $scopes = [] ) { |
||
204 | } |
||
205 |
On one hand,
eval
might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM,eval
prevents some optimization that they perform.