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 |
||
23 | class Rulesets extends AbstractSet |
||
24 | { |
||
25 | /** |
||
26 | * Class constructor with optional initialization data. |
||
27 | * |
||
28 | * @param Ruleset[] $rulesets |
||
29 | */ |
||
30 | public function __construct(Ruleset ...$rulesets) |
||
34 | |||
35 | /** |
||
36 | * Adds a ruleset. |
||
37 | * |
||
38 | * @param Ruleset $ruleset |
||
39 | */ |
||
40 | public function add(Ruleset $ruleset) |
||
44 | |||
45 | /** |
||
46 | * Checks if a ruleset is contained. |
||
47 | * |
||
48 | * @param Ruleset $ruleset |
||
49 | * |
||
50 | * @return bool |
||
51 | */ |
||
52 | public function has(Ruleset $ruleset): bool |
||
56 | |||
57 | /** |
||
58 | * Remove an element. |
||
59 | * |
||
60 | * @param Ruleset $ruleset |
||
61 | * |
||
62 | * @return bool |
||
63 | */ |
||
64 | View Code Duplication | public function remove(Ruleset $ruleset): bool |
|
77 | |||
78 | /** |
||
79 | * Checks if a user agent is allowed. |
||
80 | * |
||
81 | * @param string $userAgent |
||
82 | * @param string $path |
||
83 | * |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function isUserAgentAllowed( |
||
100 | |||
101 | /** |
||
102 | * Gets roles for a specified user-agent. |
||
103 | * |
||
104 | * @param string $userAgent Default "*" |
||
105 | * |
||
106 | * @return Ruleset |
||
107 | */ |
||
108 | public function getUserAgentRules(string $userAgent = UserAgent::ALL_AGENTS): Ruleset |
||
125 | |||
126 | /** |
||
127 | * Extract sitemap directives. |
||
128 | * |
||
129 | * @return Generator |
||
130 | */ |
||
131 | public function getSitemaps(): Generator |
||
137 | |||
138 | /** |
||
139 | * Checks if the host directive is defined. |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function hasHost(): bool |
||
153 | |||
154 | /** |
||
155 | * Gets the host directive. |
||
156 | * |
||
157 | * @return Host |
||
158 | */ |
||
159 | public function getHost(): Host |
||
167 | |||
168 | /** |
||
169 | * Gets top User-Agent directive. |
||
170 | * |
||
171 | * @param string $userAgent |
||
172 | * |
||
173 | * @return UserAgent |
||
174 | */ |
||
175 | private function getTopUserAgent(string $userAgent): UserAgent |
||
206 | } |
||
207 |
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.