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 |
||
17 | class MbRegex |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * Regular expression match and return if pattern matches given subject. |
||
22 | * |
||
23 | * @param string $pattern Pattern |
||
24 | * @param string $subject Subject |
||
25 | * @param string $option Option |
||
26 | * @return bool True if pattern matches given subject, false otherwise |
||
27 | * @throws MbRegexException When compilation error occurs |
||
28 | * @link http://php.net/function.mb-ereg-search.php |
||
29 | */ |
||
30 | 5 | public static function match($pattern, $subject, $option = '') |
|
39 | |||
40 | /** |
||
41 | * Regular expression match and return first match. |
||
42 | * |
||
43 | * @param string $pattern Pattern |
||
44 | * @param string $subject Subject |
||
45 | * @param string $option Option |
||
46 | * @return string[] Array with first match that matches given subject, empty array otherwise |
||
47 | * @throws MbRegexException When compilation error occurs |
||
48 | * @link http://php.net/function.mb-ereg-search-regs.php |
||
49 | */ |
||
50 | 5 | public static function get($pattern, $subject, $option = '') |
|
59 | |||
60 | /** |
||
61 | * Global regular expression match and return all matches. |
||
62 | * |
||
63 | * @param string $pattern Pattern |
||
64 | * @param string $subject Subject |
||
65 | * @param string $option Option |
||
66 | * @return string[][] Array of matches that match given subject, empty array otherwise |
||
67 | * @throws MbRegexException When compilation error occurs |
||
68 | * @link http://php.net/function.mb-ereg-search-regs.php |
||
69 | */ |
||
70 | 6 | public static function getAll($pattern, $subject, $option = '') |
|
98 | |||
99 | /** |
||
100 | * Regular expression replace and return replaced. |
||
101 | * |
||
102 | * Warning, take care that callback does not trigger any errors or the PHP will just die with some weird exit code. |
||
103 | * |
||
104 | * @param string|string[] $pattern Pattern or array of patterns |
||
105 | * @param callable|string|mixed[] $replacement Replacement (string or callback) or array of replacements |
||
106 | * @param string|string[] $subject Subject or array of subjects |
||
107 | * @param string $option Option |
||
108 | * @return string|string[] Replaced subject or array of subjects |
||
109 | * @throws MbRegexException When compilation error occurs |
||
110 | * @link http://php.net/function.mb-ereg-replace.php |
||
111 | * @link http://php.net/function.mb-ereg-replace-callback.php |
||
112 | */ |
||
113 | 18 | View Code Duplication | public static function replace($pattern, $replacement, $subject, $option = '') |
133 | |||
134 | /** |
||
135 | * Regular expression split and return all parts. |
||
136 | * |
||
137 | * @param string $pattern Pattern |
||
138 | * @param string $subject Subject |
||
139 | * @param int $limit Limit |
||
140 | * @param string $option Option |
||
141 | * @return string[] Array of split parts, array with original string otherwise |
||
142 | * @throws MbRegexException When compilation error occurs |
||
143 | * @link http://php.net/function.mb-split.php |
||
144 | */ |
||
145 | 6 | public static function split($pattern, $subject, $option = '', $limit = -1) |
|
176 | |||
177 | /** |
||
178 | * Regular expression grep and return matching items. |
||
179 | * |
||
180 | * @param string $pattern Pattern |
||
181 | * @param string|string[] $subject Subject or array of subjects |
||
182 | * @param string $option Option |
||
183 | * @return string[] Array with items that matches given pattern, empty array otherwise |
||
184 | * @throws MbRegexException When compilation error occurs |
||
185 | * @link http://php.net/function.mb-ereg-search.php |
||
186 | */ |
||
187 | 5 | public static function grep($pattern, $subject, $option = '') |
|
203 | |||
204 | /** |
||
205 | * Regular expression filter and return only replaced. |
||
206 | * |
||
207 | * Warning, take care that callback does not trigger any errors or the PHP will just die with some weird exit code. |
||
208 | * |
||
209 | * @param string|string[] $pattern Pattern or array of patterns |
||
210 | * @param callable|string|mixed[] $replacement Replacement (string or callback) or array of replacements |
||
211 | * @param string|string[] $subject Subject or array of subjects |
||
212 | * @param string $option Option |
||
213 | * @return string[] Array of filtered subjects |
||
214 | * @throws MbRegexException When compilation error occurs |
||
215 | * @link http://php.net/function.mb-ereg-search.php |
||
216 | * @link http://php.net/function.mb-ereg-replace.php |
||
217 | * @link http://php.net/function.mb-ereg-replace-callback.php |
||
218 | */ |
||
219 | 15 | View Code Duplication | public static function filter($pattern, $replacement, $subject, $option = '') |
244 | |||
245 | /** |
||
246 | * Prepare error handler for catching compilation errors. |
||
247 | * |
||
248 | * @param string|string[] $pattern Pattern or array of patterns |
||
249 | */ |
||
250 | protected static function setUp($pattern) |
||
257 | |||
258 | /** |
||
259 | * Clean up after setUp(). |
||
260 | */ |
||
261 | 47 | protected static function tearDown() |
|
265 | |||
266 | /** |
||
267 | * Prepare map (pattern, replacement) from arguments. |
||
268 | * |
||
269 | * @param string|string[] $pattern Pattern or array of patterns |
||
270 | * @param callable|string|mixed[] $replacement Replacement (string or callback) or array of replacements |
||
271 | * @return string[][] Array of pattern and replacement |
||
272 | */ |
||
273 | 33 | private static function prepareReplaceMap($pattern, $replacement) |
|
293 | |||
294 | /** |
||
295 | * Replace subject by callback or by string. |
||
296 | * |
||
297 | * @param string $pattern Pattern |
||
298 | * @param callable|string $replacement Replacement |
||
299 | * @param string $subject Subject |
||
300 | * @param string $option Option |
||
301 | * @return string Replaced subject |
||
302 | */ |
||
303 | 25 | private static function execReplace($pattern, $replacement, $subject, $option) |
|
309 | } |
||
310 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.