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 |
||
11 | final class AdditionalItems implements ConstraintInterface |
||
12 | { |
||
13 | const KEYWORD = 'additionalItems'; |
||
14 | |||
15 | /** |
||
16 | * {@inheritdoc} |
||
17 | */ |
||
18 | 4 | public function validate($value, $parameter, Validator $validator) |
|
38 | |||
39 | /** |
||
40 | * @param object $schema |
||
41 | * |
||
42 | * @return mixed |
||
43 | */ |
||
44 | 2 | private static function getItems($schema) |
|
48 | |||
49 | /** |
||
50 | * @param array $items |
||
51 | * @param object $schema |
||
52 | * @param Validator $validator |
||
53 | * |
||
54 | * @return array |
||
55 | */ |
||
56 | 2 | private static function validateAdditionalItemsAgainstSchema($items, $schema, Validator $validator) |
|
57 | { |
||
58 | 2 | $errors = []; |
|
59 | 2 | View Code Duplication | foreach ($items as $key => $item) { |
|
|||
60 | 2 | $subValidator = $validator->makeSubSchemaValidator( |
|
61 | 2 | $item, |
|
62 | 2 | $schema, |
|
63 | 2 | pointer_push($validator->getDataPath(), $key) |
|
64 | ); |
||
65 | 2 | $errors = array_merge($errors, $subValidator->errors()); |
|
66 | } |
||
67 | |||
68 | 2 | return $errors; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param array $value |
||
73 | * @param array $items |
||
74 | * @param \League\JsonGuard\Validator $validator |
||
75 | * |
||
76 | * @return \League\JsonGuard\ValidationError |
||
77 | */ |
||
78 | 2 | private static function validateAdditionalItemsWhenNotAllowed($value, $items, Validator $validator) |
|
84 | } |
||
85 |
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.