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 | */ |
||
121 | 306 | View Code Duplication | public function hasKeyword($version, $keyword) |
137 | |||
138 | /** |
||
139 | * Loads the constraints associated with a given JSON Schema version. |
||
140 | * |
||
141 | * @param string $version |
||
142 | * |
||
143 | * @return Constraint[] |
||
144 | * |
||
145 | * @throws UnsupportedVersionException if the version is not supported |
||
146 | */ |
||
147 | 371 | protected function createConstraints($version) |
|
164 | |||
165 | private function createBuiltInConstraints(array $constraintNames) |
||
173 | } |
||
174 |