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 |
||
25 | class PropertiesConstraint implements Constraint |
||
26 | { |
||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | 357 | public function keywords() |
|
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | 348 | public function supports($type) |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 121 | public function normalize(stdClass $schema, Context $context, Walker $walker) |
|
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 99 | public function apply($instance, stdClass $schema, Context $context, Walker $walker) |
|
65 | { |
||
66 | // implementation of the algorithms described in 5.4.4.4 and in 8.3 |
||
67 | 99 | foreach ($instance as $property => $value) { |
|
68 | 92 | $schemas = []; |
|
69 | |||
70 | 92 | if (isset($schema->properties->{$property})) { |
|
71 | 65 | $schemas[] = $schema->properties->{$property}; |
|
72 | 65 | } |
|
73 | |||
74 | 92 | foreach ($schema->patternProperties as $regex => $propertySchema) { |
|
75 | 34 | if (Utils::matchesRegex($property, $regex)) { |
|
76 | 23 | $schemas[] = $propertySchema; |
|
77 | 23 | } |
|
78 | 92 | } |
|
79 | |||
80 | 92 | if (empty($schemas)) { |
|
81 | 36 | if (is_object($schema->additionalProperties)) { |
|
82 | 27 | $schemas[] = $schema->additionalProperties; |
|
83 | 36 | } elseif ($schema->additionalProperties === false) { |
|
84 | 9 | $context->addViolation('additional property "%s" is not allowed', [$property]); |
|
85 | 9 | } |
|
86 | 36 | } |
|
87 | |||
88 | 92 | $context->enterNode($property); |
|
89 | |||
90 | 92 | foreach ($schemas as $childSchema) { |
|
91 | 90 | $walker->applyConstraints($value, $childSchema, $context); |
|
92 | 92 | } |
|
93 | |||
94 | 92 | $context->leaveNode(); |
|
95 | 99 | } |
|
96 | 99 | } |
|
97 | |||
98 | 121 | private function createDefaults(stdClass $schema) |
|
99 | { |
||
100 | 121 | if (!property_exists($schema, 'properties')) { |
|
101 | 17 | $schema->properties = new stdClass(); |
|
102 | 17 | } |
|
103 | |||
104 | 121 | if (!property_exists($schema, 'additionalProperties') |
|
105 | 121 | || $schema->additionalProperties === true) { |
|
106 | 33 | $schema->additionalProperties = new stdClass(); |
|
107 | 33 | } |
|
108 | |||
109 | 121 | if (!property_exists($schema, 'patternProperties')) { |
|
110 | 33 | $schema->patternProperties = new stdClass(); |
|
111 | 33 | } |
|
112 | 121 | } |
|
113 | |||
114 | 121 | private function parsePropertiesProperty(stdClass $schema, Context $context, Walker $walker) |
|
131 | |||
132 | 119 | View Code Duplication | private function parseAdditionalPropertiesProperty(stdClass $schema, Context $context, Walker $walker) |
140 | |||
141 | 118 | private function parsePatternPropertiesProperty(stdClass $schema, Context $context, Walker $walker) |
|
162 | } |
||
163 |