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 |
||
17 | class Registry |
||
18 | { |
||
19 | const VERSION_CURRENT = 'http://json-schema.org/schema#'; |
||
20 | const VERSION_DRAFT_3 = 'http://json-schema.org/draft-03/schema#'; |
||
21 | const VERSION_DRAFT_4 = 'http://json-schema.org/draft-04/schema#'; |
||
22 | |||
23 | private static $commonConstraints = [ |
||
24 | 'Maximum', |
||
25 | 'Minimum', |
||
26 | 'MaxLength', |
||
27 | 'MinLength', |
||
28 | 'Pattern', |
||
29 | 'Items', |
||
30 | 'MaxItems', |
||
31 | 'MinItems', |
||
32 | 'UniqueItems', |
||
33 | 'Required', |
||
34 | 'Properties', |
||
35 | 'Dependencies', |
||
36 | 'Enum', |
||
37 | 'Type', |
||
38 | 'Format', |
||
39 | ]; |
||
40 | |||
41 | private static $draft4Constraints = [ |
||
42 | 'MultipleOf', |
||
43 | 'MinProperties', |
||
44 | 'MaxProperties', |
||
45 | 'AllOf', |
||
46 | 'AnyOf', |
||
47 | 'OneOf', |
||
48 | 'Not', |
||
49 | ]; |
||
50 | |||
51 | /** |
||
52 | * @var Constraint[][] |
||
53 | */ |
||
54 | private $constraints = []; |
||
55 | |||
56 | /** |
||
57 | * @var Constraint[][] |
||
58 | */ |
||
59 | private $constraintsForTypeCache = []; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | private $keywordsCache = []; |
||
65 | |||
66 | /** |
||
67 | * Returns the constraints associated with a given JSON Schema version. |
||
68 | * |
||
69 | * @param string $version |
||
70 | * |
||
71 | * @return Constraint[] |
||
72 | * |
||
73 | * @throws UnsupportedVersionException if the version is not supported |
||
74 | */ |
||
75 | 371 | public function getConstraints($version) |
|
83 | |||
84 | /** |
||
85 | * Returns the constraints associated with a given JSON Schema version |
||
86 | * supporting a given primitive type. |
||
87 | * |
||
88 | * @param string $version |
||
89 | * @param string $type |
||
90 | * |
||
91 | * @return Constraint[] |
||
92 | * |
||
93 | * @throws UnsupportedVersionException if the version is not supported |
||
94 | */ |
||
95 | 354 | View Code Duplication | public function getConstraintsForType($version, $type) |
111 | |||
112 | /** |
||
113 | * Returns whether a keyword is supported in a given JSON Schema version. |
||
114 | * |
||
115 | * @param string $version |
||
116 | * @param string $keyword |
||
117 | * |
||
118 | * @return bool |
||
119 | */ |
||
120 | 306 | View Code Duplication | public function hasKeyword($version, $keyword) |
136 | |||
137 | /** |
||
138 | * Loads the constraints associated with a given JSON Schema version. |
||
139 | * |
||
140 | * @param string $version |
||
141 | * |
||
142 | * @return Constraint[] |
||
143 | * |
||
144 | * @throws UnsupportedVersionException if the version is not supported |
||
145 | */ |
||
146 | 371 | protected function createConstraints($version) |
|
163 | |||
164 | private function createBuiltInConstraints(array $constraintNames) |
||
172 | } |
||
173 |
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.