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 HostParser implements ParserInterface, RobotsTxtInterface |
||
14 | { |
||
15 | use UrlParser; |
||
16 | |||
17 | /** |
||
18 | * Directive |
||
19 | */ |
||
20 | const DIRECTIVE = self::DIRECTIVE_HOST; |
||
21 | |||
22 | /** |
||
23 | * Base Uri |
||
24 | * @var string |
||
25 | */ |
||
26 | private $base; |
||
27 | |||
28 | /** |
||
29 | * Host array |
||
30 | * @var string[] |
||
31 | */ |
||
32 | private $array = []; |
||
33 | |||
34 | /** |
||
35 | * Host constructor. |
||
36 | * |
||
37 | * @param string $base |
||
38 | */ |
||
39 | public function __construct($base) |
||
43 | |||
44 | /** |
||
45 | * Add |
||
46 | * |
||
47 | * @param string $line |
||
48 | * @return bool |
||
49 | */ |
||
50 | public function add($line) |
||
63 | |||
64 | /** |
||
65 | * Client |
||
66 | * |
||
67 | * @param string $line |
||
68 | * @return string|false |
||
69 | */ |
||
70 | private function parse($line) |
||
89 | |||
90 | /** |
||
91 | * Client |
||
92 | * |
||
93 | * @return HostClient |
||
94 | */ |
||
95 | public function client() |
||
99 | |||
100 | /** |
||
101 | * Rule array |
||
102 | * |
||
103 | * @return string[][] |
||
104 | */ |
||
105 | public function getRules() |
||
109 | |||
110 | /** |
||
111 | * Render |
||
112 | * |
||
113 | * @return string[] |
||
114 | */ |
||
115 | View Code Duplication | public function render() |
|
123 | } |
||
124 |
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.