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 |
||
21 | class Ruleset extends AbstractSet |
||
22 | { |
||
23 | /** |
||
24 | * Class constructor with optional initialization data. |
||
25 | * |
||
26 | * @param DirectiveInterface[] $directives |
||
27 | */ |
||
28 | public function __construct(DirectiveInterface ...$directives) |
||
32 | |||
33 | /** |
||
34 | * Adds a directive. |
||
35 | * |
||
36 | * @param DirectiveInterface $directive |
||
37 | */ |
||
38 | public function add(DirectiveInterface $directive) |
||
42 | |||
43 | /** |
||
44 | * Checks if a directive is contained. |
||
45 | * |
||
46 | * @param DirectiveInterface $directive |
||
47 | * |
||
48 | * @return bool |
||
49 | */ |
||
50 | public function has(DirectiveInterface $directive): bool |
||
54 | |||
55 | /** |
||
56 | * Remove an element. |
||
57 | * |
||
58 | * @param DirectiveInterface $directive |
||
59 | * |
||
60 | * @return bool |
||
61 | */ |
||
62 | View Code Duplication | public function remove(DirectiveInterface $directive): bool |
|
75 | |||
76 | /** |
||
77 | * Checks if a user agent is allowed. |
||
78 | * |
||
79 | * @param string $path |
||
80 | * |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function isUserAgentAllowed( |
||
102 | |||
103 | /** |
||
104 | * Gets ruleset comments for the developer. |
||
105 | * |
||
106 | * @return Generator |
||
107 | */ |
||
108 | public function getComments(): Generator |
||
112 | |||
113 | /** |
||
114 | * Gets directives of a certain type. |
||
115 | * |
||
116 | * @param string|null $class |
||
117 | * |
||
118 | * @return DirectiveInterface[] |
||
119 | */ |
||
120 | public function getDirectives(string $class = null): array |
||
126 | } |
||
127 |
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.