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 |
||
6 | trait ReplaceFunctions |
||
7 | { |
||
8 | /** |
||
9 | * Replaces all occurrences of $pattern with $replacement in $subject and returns the result and number |
||
10 | * of replacements done. |
||
11 | * See @link http://php.net/manual/en/function.preg-replace.php |
||
12 | * |
||
13 | * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression |
||
14 | * $pattern |
||
15 | * @param string |
||
16 | * $replacement |
||
17 | * @param string|array |
||
18 | * $subject |
||
19 | * @param number |
||
20 | * $limit |
||
21 | * |
||
22 | * @throws \RuntimeException |
||
23 | * @throws \InvalidArgumentException |
||
24 | * |
||
25 | * @return \Mcustiel\PhpSimpleRegex\ReplaceResult |
||
26 | */ |
||
27 | 3 | View Code Duplication | public function replaceAndCount($pattern, $replacement, $subject, $limit = -1) |
|
|||
28 | { |
||
29 | 3 | $count = 0; |
|
30 | 3 | $replaced = @preg_replace( |
|
31 | 3 | $this->getPatternForReplace($pattern), |
|
32 | 3 | $replacement, |
|
33 | 3 | $subject, |
|
34 | 3 | $limit, |
|
35 | 3 | $count |
|
36 | ); |
||
37 | 3 | $this->checkResultIsOkForReplaceOrThrowException($replaced, $pattern); |
|
38 | |||
39 | 3 | return new ReplaceResult($replaced, $count); |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Replaces all occurrences of $pattern with $replacement in $subject and returns the result and number |
||
44 | * of replacements done. Result will return only the modified subjects. |
||
45 | * See @link http://php.net/manual/en/function.preg-filter.php |
||
46 | * |
||
47 | * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression |
||
48 | * $pattern |
||
49 | * @param string |
||
50 | * $replacement |
||
51 | * @param string|array |
||
52 | * $subject |
||
53 | * @param number |
||
54 | * $limit |
||
55 | * |
||
56 | * @throws \RuntimeException |
||
57 | * @throws \InvalidArgumentException |
||
58 | * |
||
59 | * @return \Mcustiel\PhpSimpleRegex\ReplaceResult |
||
60 | */ |
||
61 | 4 | View Code Duplication | public function replaceAndCountAndOnlyGetChanged($pattern, $replacement, $subject, $limit = -1) |
62 | { |
||
63 | 4 | $count = 0; |
|
64 | // I must display error here, since I couldn't differentiate if error happened or not replace done |
||
65 | 4 | $replaced = preg_filter( |
|
66 | 4 | $this->getPatternForReplace($pattern), |
|
67 | 4 | $replacement, |
|
68 | 4 | $subject, |
|
69 | 4 | $limit, |
|
70 | 4 | $count |
|
71 | ); |
||
72 | |||
73 | 4 | return new ReplaceResult($replaced, $count); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * Replaces all occurrences of $pattern with $replacement in $subject and returns the replaced subject. |
||
78 | * |
||
79 | * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression |
||
80 | * $pattern |
||
81 | * @param string |
||
82 | * $replacement |
||
83 | * @param string|array |
||
84 | * $subject |
||
85 | * @param number |
||
86 | * $limit |
||
87 | * |
||
88 | * @throws \RuntimeException |
||
89 | * @throws \InvalidArgumentException |
||
90 | * |
||
91 | * @return string|array |
||
92 | */ |
||
93 | 5 | public function replace($pattern, $replacement, $subject, $limit = -1) |
|
94 | { |
||
95 | 5 | $replaced = @preg_replace( |
|
96 | 5 | $this->getPatternForReplace($pattern), |
|
97 | 5 | $replacement, |
|
98 | 5 | $subject, |
|
99 | 5 | $limit |
|
100 | ); |
||
101 | 5 | $this->checkResultIsOkForReplaceOrThrowException($replaced, $pattern); |
|
102 | |||
103 | 4 | return $replaced; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * Replaces all occurrences of $pattern with $replacement in $subject and returns the replaced subject. |
||
108 | * |
||
109 | * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression |
||
110 | * $pattern |
||
111 | * @param string |
||
112 | * $replacement |
||
113 | * @param string|array |
||
114 | * $subject |
||
115 | * @param number |
||
116 | * $limit |
||
117 | * |
||
118 | * @throws \RuntimeException |
||
119 | * @throws \InvalidArgumentException |
||
120 | * |
||
121 | * @return string|array |
||
122 | */ |
||
123 | public function replaceAndOnlyGetChanged($pattern, $replacement, $subject, $limit = -1) |
||
124 | { |
||
125 | // I must display error here, since I couldn't differentiate if error happened or not replace done |
||
126 | return preg_filter( |
||
127 | $this->getPatternForReplace($pattern), |
||
128 | $replacement, |
||
129 | $subject, |
||
130 | $limit |
||
131 | ); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Replaces all occurrences of $pattern using $callback function in $subject |
||
136 | * and returns the replaced subject. |
||
137 | * |
||
138 | * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression |
||
139 | * $pattern |
||
140 | * @param callable |
||
141 | * $callback |
||
142 | * @param string|array |
||
143 | * $subject |
||
144 | * @param number |
||
145 | * $limit |
||
146 | * |
||
147 | * @throws \RuntimeException |
||
148 | * @throws \InvalidArgumentException |
||
149 | * |
||
150 | * @return string|array |
||
151 | */ |
||
152 | 2 | public function replaceCallback($pattern, callable $callback, $subject, $limit = -1) |
|
153 | { |
||
154 | 2 | $replaced = @preg_replace_callback( |
|
155 | 2 | $this->getPatternForReplace($pattern), |
|
156 | 2 | $callback, |
|
157 | 2 | $subject, |
|
158 | 2 | $limit |
|
159 | ); |
||
160 | 2 | $this->checkResultIsOkForReplaceOrThrowException($replaced, $pattern); |
|
161 | |||
162 | 2 | return $replaced; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Replaces all occurrences of $pattern using $callback function in $subject |
||
167 | * and returns the replaced subject and the number of replacements done. |
||
168 | * |
||
169 | * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression |
||
170 | * $pattern |
||
171 | * @param callable |
||
172 | * $callback |
||
173 | * @param string|array |
||
174 | * $subject |
||
175 | * @param number |
||
176 | * $limit |
||
177 | * @throws \RuntimeException |
||
178 | * @throws \InvalidArgumentException |
||
179 | * |
||
180 | * @return \Mcustiel\PhpSimpleRegex\ReplaceResult |
||
181 | */ |
||
182 | 1 | View Code Duplication | public function replaceCallbackAndCount($pattern, callable $callback, $subject, $limit = -1) |
183 | { |
||
184 | 1 | $count = 0; |
|
185 | 1 | $result = preg_replace_callback( |
|
186 | 1 | $this->getPatternForReplace($pattern), |
|
187 | 1 | $callback, |
|
188 | 1 | $subject, |
|
189 | 1 | $limit, |
|
190 | 1 | $count |
|
191 | ); |
||
192 | 1 | $this->checkResultIsOkForReplaceOrThrowException($result, $pattern); |
|
193 | |||
194 | 1 | return new ReplaceResult($result, $count); |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param mixed $pattern |
||
199 | * @throws \InvalidArgumentException |
||
200 | * @return string|array |
||
201 | */ |
||
202 | abstract protected function getPatternForReplace($pattern); |
||
203 | |||
204 | /** |
||
205 | * @param mixed $result |
||
206 | * @param string $pattern |
||
207 | * |
||
208 | * @throws \RuntimeException |
||
209 | */ |
||
210 | abstract protected function checkResultIsOkForReplaceOrThrowException($result, $pattern); |
||
211 | } |
||
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.