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 |
||
9 | class UriTemplate |
||
10 | { |
||
11 | /** @var string URI template */ |
||
12 | private $template; |
||
13 | |||
14 | /** @var array Variables to use in the template expansion */ |
||
15 | private $variables; |
||
16 | |||
17 | /** @var array Hash for quick operator lookups */ |
||
18 | private static $operatorHash = [ |
||
19 | '' => ['prefix' => '', 'joiner' => ',', 'query' => false], |
||
20 | '+' => ['prefix' => '', 'joiner' => ',', 'query' => false], |
||
21 | '#' => ['prefix' => '#', 'joiner' => ',', 'query' => false], |
||
22 | '.' => ['prefix' => '.', 'joiner' => '.', 'query' => false], |
||
23 | '/' => ['prefix' => '/', 'joiner' => '/', 'query' => false], |
||
24 | ';' => ['prefix' => ';', 'joiner' => ';', 'query' => true], |
||
25 | '?' => ['prefix' => '?', 'joiner' => '&', 'query' => true], |
||
26 | '&' => ['prefix' => '&', 'joiner' => '&', 'query' => true] |
||
27 | ]; |
||
28 | |||
29 | /** @var array Delimiters */ |
||
30 | private static $delims = [':', '/', '?', '#', '[', ']', '@', '!', '$', |
||
31 | '&', '\'', '(', ')', '*', '+', ',', ';', '=']; |
||
32 | |||
33 | /** @var array Percent encoded delimiters */ |
||
34 | private static $delimsPct = ['%3A', '%2F', '%3F', '%23', '%5B', '%5D', |
||
35 | '%40', '%21', '%24', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C', |
||
36 | '%3B', '%3D']; |
||
37 | |||
38 | public function expand($template, array $variables) |
||
53 | |||
54 | /** |
||
55 | * Parse an expression into parts |
||
56 | * |
||
57 | * @param string $expression Expression to parse |
||
58 | * |
||
59 | * @return array Returns an associative array of parts |
||
60 | */ |
||
61 | private function parseExpression($expression) |
||
91 | |||
92 | /** |
||
93 | * Process an expansion |
||
94 | * |
||
95 | * @param array $matches Matches met in the preg_replace_callback |
||
96 | * |
||
97 | * @return string Returns the replacement string |
||
98 | */ |
||
99 | private function expandMatch(array $matches) |
||
211 | |||
212 | /** |
||
213 | * Determines if an array is associative. |
||
214 | * |
||
215 | * This makes the assumption that input arrays are sequences or hashes. |
||
216 | * This assumption is a tradeoff for accuracy in favor of speed, but it |
||
217 | * should work in almost every case where input is supplied for a URI |
||
218 | * template. |
||
219 | * |
||
220 | * @param array $array Array to check |
||
221 | * |
||
222 | * @return bool |
||
223 | */ |
||
224 | private function isAssoc(array $array) |
||
228 | |||
229 | /** |
||
230 | * Removes percent encoding on reserved characters (used with + and # |
||
231 | * modifiers). |
||
232 | * |
||
233 | * @param string $string String to fix |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | private function decodeReserved($string) |
||
241 | } |
||
242 |
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.