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 |
||
20 | final class HeadersConstraint extends Constraint |
||
21 | { |
||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private $errors = []; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $expectedHeadersSchemas; |
||
31 | |||
32 | /** |
||
33 | * @var Validator |
||
34 | */ |
||
35 | private $validator; |
||
36 | |||
37 | /** |
||
38 | * @param array $expectedHeadersSchemas |
||
39 | */ |
||
40 | 4 | public function __construct(array $expectedHeadersSchemas) { |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | 4 | protected function matches($actualHeaders) |
|
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | 2 | protected function failureDescription($other) |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 2 | protected function additionalFailureDescription($other) |
|
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | 2 | public function toString() |
|
98 | |||
99 | /** |
||
100 | * Ensure schema is object. |
||
101 | * |
||
102 | * @param object|array $schema |
||
103 | * |
||
104 | * @return object |
||
105 | */ |
||
106 | 4 | private static function normalizeJsonSchema($schema) |
|
110 | |||
111 | /** |
||
112 | * Ensure header value is array. |
||
113 | * |
||
114 | * @param mixed $value |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | 2 | private static function normalizeHeaderValue($value) |
|
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.