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 |
||
13 | class MetaCharacters |
||
14 | { |
||
15 | /** |
||
16 | * @const Bit value that indicates whether a meta-character represents a single character |
||
17 | */ |
||
18 | const IS_CHAR = 1; |
||
19 | |||
20 | /** |
||
21 | * @const Bit value that indicates whether a meta-character represents a quantifiable expression |
||
22 | */ |
||
23 | const IS_QUANTIFIABLE = 2; |
||
24 | |||
25 | /** |
||
26 | * @var array Map of meta values and the expression they represent |
||
27 | */ |
||
28 | protected $exprs = []; |
||
29 | |||
30 | /** |
||
31 | * @var InputInterface |
||
32 | */ |
||
33 | protected $input; |
||
34 | |||
35 | /** |
||
36 | * @var array Map of meta-characters' codepoints and their value |
||
37 | */ |
||
38 | protected $meta = []; |
||
39 | |||
40 | /** |
||
41 | * @param InputInterface $input |
||
42 | */ |
||
43 | public function __construct(InputInterface $input) |
||
47 | |||
48 | /** |
||
49 | * Add a meta-character to the list |
||
50 | * |
||
51 | * @param string $char Meta-character |
||
52 | * @param string $expr Regular expression |
||
53 | * @return void |
||
54 | */ |
||
55 | public function add($char, $expr) |
||
73 | |||
74 | /** |
||
75 | * Get the expression associated with a meta value |
||
76 | * |
||
77 | * @param integer $metaValue |
||
78 | * @return string |
||
79 | */ |
||
80 | public function getExpression($metaValue) |
||
89 | |||
90 | /** |
||
91 | * Return whether a given value represents a single character |
||
92 | * |
||
93 | * @param integer $value |
||
94 | * @return bool |
||
95 | */ |
||
96 | public function isChar($value) |
||
100 | |||
101 | /** |
||
102 | * Return whether a given value represents a quantifiable expression |
||
103 | * |
||
104 | * @param integer $value |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function isQuantifiable($value) |
||
111 | |||
112 | /** |
||
113 | * Replace values from meta-characters in a list of strings with their meta value |
||
114 | * |
||
115 | * @param array[] $strings |
||
116 | * @return array[] |
||
117 | */ |
||
118 | public function replaceMeta(array $strings) |
||
133 | |||
134 | /** |
||
135 | * Compute and return a value for given expression |
||
136 | * |
||
137 | * Values are meant to be a unique negative integer. The last 2 bits indicate whether the |
||
138 | * expression is quantifiable and/or represents a single character. |
||
139 | * |
||
140 | * @param string $expr Regular expression |
||
141 | * @return integer |
||
142 | */ |
||
143 | protected function computeValue($expr) |
||
157 | |||
158 | /** |
||
159 | * Test whether given expression represents a single character usable in a character class |
||
160 | * |
||
161 | * @param string $expr |
||
162 | * @return bool |
||
163 | */ |
||
164 | View Code Duplication | protected function exprIsChar($expr) |
|
186 | |||
187 | /** |
||
188 | * Test whether given expression is quantifiable |
||
189 | * |
||
190 | * @param string $expr |
||
191 | * @return bool |
||
192 | */ |
||
193 | View Code Duplication | protected function exprIsQuantifiable($expr) |
|
212 | } |
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.