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 namespace Gears\String\Methods; |
||
17 | trait Regx |
||
18 | { |
||
19 | /** |
||
20 | * The delimiter we will use for all regular expressions. |
||
21 | * |
||
22 | * @link http://php.net/manual/en/regexp.reference.delimiters.php |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $regexDelimiter = '/'; |
||
27 | |||
28 | /** |
||
29 | * Allows you to change the the default regular expression delimiter. |
||
30 | * |
||
31 | * @param string $value The delimiter to use for all future expressions. |
||
32 | * |
||
33 | * @return static |
||
34 | */ |
||
35 | public function setRegexDelimiter($value) |
||
41 | |||
42 | /** |
||
43 | * Returns true if the string matches the supplied pattern, false otherwise. |
||
44 | * |
||
45 | * @param string $pattern Regex pattern to match against. |
||
46 | * |
||
47 | * @param string $options Matching conditions to be used. |
||
48 | * |
||
49 | * @return bool |
||
50 | * |
||
51 | * @throws PcreException When PCRE Error occurs. |
||
52 | */ |
||
53 | public function regexMatch($pattern, $options = '') |
||
75 | |||
76 | /** |
||
77 | * Given an expression with capture groups, this will return those captures. |
||
78 | * |
||
79 | * Basically this is the same as `regexMatch()` but returns the array |
||
80 | * of matches from `preg_match()` where as `regexMatch()` just returns |
||
81 | * a boolean result. |
||
82 | * |
||
83 | * @param string $pattern Regex pattern to match against. |
||
84 | * |
||
85 | * @param string $options Matching conditions to be used. |
||
86 | * |
||
87 | * @return array The matches discovered by `preg_match()`. |
||
88 | * |
||
89 | * @throws PcreException When PCRE Error occurs. |
||
90 | */ |
||
91 | View Code Duplication | public function regexExtract($pattern, $options = '') |
|
116 | |||
117 | /** |
||
118 | * Replaces all occurrences of $pattern in $str by $replacement. |
||
119 | * |
||
120 | * @param string $pattern The regular expression pattern. |
||
121 | * |
||
122 | * @param string $replacement The string to replace with. |
||
123 | * |
||
124 | * @param string $options Matching conditions to be used. |
||
125 | * |
||
126 | * @return static Resulting string after the replacements |
||
127 | * |
||
128 | * @throws PcreException When PCRE Error occurs. |
||
129 | */ |
||
130 | View Code Duplication | public function regexReplace($pattern, $replacement, $options = '') |
|
156 | |||
157 | /** |
||
158 | * Splits the string with the provided regular expression. |
||
159 | * |
||
160 | * @param string $pattern The regex with which to split the string. |
||
161 | * |
||
162 | * @param int|null $limit Optional maximum number of results to return. |
||
163 | * |
||
164 | * @param bool $quote By default this method will run the provided |
||
165 | * $pattern through preg_quote(), this allows the |
||
166 | * method to be used to split on simple substrings. |
||
167 | * |
||
168 | * @return static[] An array of Str objects. |
||
169 | */ |
||
170 | public function split($pattern, $limit = null, $quote = true) |
||
215 | } |
||
216 |
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.