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 |
||
14 | class Htaccess |
||
15 | { |
||
16 | /** |
||
17 | * @var Filesystem |
||
18 | */ |
||
19 | private $fs; |
||
20 | |||
21 | /** |
||
22 | * @var Parser |
||
23 | */ |
||
24 | private $parser; |
||
25 | |||
26 | /** |
||
27 | * @var array|\ArrayAccess|HtaccessContainer |
||
28 | */ |
||
29 | private $contents; |
||
30 | |||
31 | /** |
||
32 | * @var bool |
||
33 | */ |
||
34 | private $redirectHttps = false; |
||
35 | |||
36 | /** |
||
37 | * @var bool |
||
38 | */ |
||
39 | private $redirectWww = false; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $redirects = []; |
||
45 | |||
46 | /** |
||
47 | * Htaccess constructor. |
||
48 | * |
||
49 | * @param string $path |
||
50 | * |
||
51 | * @throws ConfigurationException |
||
52 | */ |
||
53 | public function __construct(string $path) { |
||
64 | |||
65 | /** |
||
66 | * @param bool $redirectHttps |
||
67 | * |
||
68 | * @return Htaccess |
||
69 | */ |
||
70 | public function setRedirectHttps(bool $redirectHttps = false) : Htaccess { |
||
75 | |||
76 | /** |
||
77 | * @param bool $redirectWww |
||
78 | * |
||
79 | * @return Htaccess |
||
80 | */ |
||
81 | public function setRedirectWww(bool $redirectWww = false) : Htaccess { |
||
86 | |||
87 | /** |
||
88 | * Add custom redirects handles by htaccess |
||
89 | * |
||
90 | * @param string $from |
||
91 | * @param string $to |
||
92 | * |
||
93 | * @return Htaccess |
||
94 | */ |
||
95 | public function addRedirect(string $from, string $to) : Htaccess { |
||
100 | |||
101 | /** |
||
102 | * Parse the modified .htaccess |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | public function parse() : string { |
||
122 | |||
123 | /** |
||
124 | * Get or create the headers block |
||
125 | * |
||
126 | * @return Block |
||
127 | */ |
||
128 | View Code Duplication | public function &getHeaderBlock() : Block { |
|
142 | |||
143 | /** |
||
144 | * Get or create a page block within the headers block |
||
145 | * |
||
146 | * @param Page $page |
||
147 | * |
||
148 | * @return Block |
||
149 | */ |
||
150 | public function &getPageBlock(Page $page) : Block { |
||
166 | |||
167 | /** |
||
168 | * Clear all page header blocks |
||
169 | */ |
||
170 | public function clearPageBlocks() { |
||
179 | |||
180 | /** |
||
181 | * Clear all rewrites |
||
182 | */ |
||
183 | public function clearRewriteBlock() { |
||
195 | |||
196 | /** |
||
197 | * Get or create the rewrite block |
||
198 | * |
||
199 | * @return Block |
||
200 | */ |
||
201 | View Code Duplication | public function &getRewriteBlock() : Block { |
|
215 | |||
216 | /** |
||
217 | * @param Block $headerBlock |
||
218 | * @param string $pageName |
||
219 | * |
||
220 | * @return null|Block |
||
221 | */ |
||
222 | private function findPageBlockByParentAndName(Block $headerBlock, string $pageName) { |
||
233 | |||
234 | /** |
||
235 | * Add www rewrite |
||
236 | */ |
||
237 | private function rewriteWww() { |
||
242 | |||
243 | /** |
||
244 | * Add https rewrite |
||
245 | */ |
||
246 | private function rewriteHttps() { |
||
251 | |||
252 | /** |
||
253 | * Add custom rewrites |
||
254 | */ |
||
255 | private function rewriteCustomRedirects() { |
||
265 | |||
266 | /** |
||
267 | * Add .html rewrite |
||
268 | */ |
||
269 | private function rewriteHtml() { |
||
274 | |||
275 | /** |
||
276 | * @param Block $rewriteBlock |
||
277 | * @param string $condition |
||
278 | * @param string $rule |
||
279 | */ |
||
280 | private function createConditionalRewrite(Block &$rewriteBlock, string $condition, string $rule) { |
||
291 | |||
292 | /** |
||
293 | * @param string $modName |
||
294 | * |
||
295 | * @return null|Block |
||
296 | */ |
||
297 | private function findHeaderBlockByModName(string $modName) { |
||
307 | } |
||
308 |
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.