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 | final class Reserved |
||
19 | { |
||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | protected static $regex = [ |
||
24 | Tokenizer::TOKEN_TYPE_RESERVED_TOP_LEVEL => 'getRegexReservedTopLevel', |
||
25 | Tokenizer::TOKEN_TYPE_RESERVED_NEWLINE => 'getRegexReservedNewLine', |
||
26 | Tokenizer::TOKEN_TYPE_RESERVED => 'getRegexReserved', |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * @param Tokenizer $tokenizer |
||
31 | * @param string $string |
||
32 | * @param array|null $previous |
||
33 | * |
||
34 | * @return array |
||
35 | */ |
||
36 | public static function isReserved(Tokenizer $tokenizer, $string, $previous) |
||
50 | |||
51 | /** |
||
52 | * A reserved word cannot be preceded by a "." in order to differentiate "mytable.from" from the token "from". |
||
53 | * |
||
54 | * @param $previous |
||
55 | * |
||
56 | * @return bool |
||
57 | */ |
||
58 | protected static function isReservedPrecededByDotCharacter($previous) |
||
62 | |||
63 | /** |
||
64 | * @param array $tokenData |
||
65 | * @param $type |
||
66 | * @param string $string |
||
67 | * @param Tokenizer $tokenizer |
||
68 | */ |
||
69 | protected static function getReservedString(array &$tokenData, $type, $string, Tokenizer $tokenizer) |
||
84 | |||
85 | /** |
||
86 | * @param string $upper |
||
87 | * @param array $matches |
||
88 | * @param string $regexReserved |
||
89 | * @param string $regexBoundaries |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | protected static function isReservedString($upper, array &$matches, $regexReserved, $regexBoundaries) |
||
101 | |||
102 | /** |
||
103 | * @param string $type |
||
104 | * @param string $string |
||
105 | * @param array $matches |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | View Code Duplication | protected static function getStringTypeArray($type, $string, array &$matches) |
|
116 | } |
||
117 |
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.