Complex classes like Properties often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Properties, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Properties extends AbstractIterableValidator |
||
32 | { |
||
33 | const PROPERTIES_INDEX = 'properties'; |
||
34 | const PATTERN_PROPERTIES_INDEX = 'patternProperties'; |
||
35 | const ADDITIONAL_PROPERTIES_INDEX = 'additionalProperties'; |
||
36 | |||
37 | /** |
||
38 | * @var \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface[] |
||
39 | */ |
||
40 | private $properties = []; |
||
41 | |||
42 | /** |
||
43 | * @var bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface |
||
44 | */ |
||
45 | private $additionalProperties = true; |
||
46 | |||
47 | /** |
||
48 | * @var \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface[] |
||
49 | */ |
||
50 | private $patternProperties = []; |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | * |
||
55 | * @see \Mcustiel\SimpleRequest\Validator\AbstractIterableValidator::setSpecification() |
||
56 | */ |
||
57 | 107 | public function setSpecification($specification = null) |
|
65 | |||
66 | /** |
||
67 | * @param array $specification |
||
68 | * |
||
69 | * @throws \Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException |
||
70 | */ |
||
71 | 104 | private function initAdditionalProperties(array $specification) |
|
77 | |||
78 | /** |
||
79 | * @param array $specification |
||
80 | * |
||
81 | * @throws \Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException |
||
82 | */ |
||
83 | 104 | private function initPatternProperties(array $specification) |
|
89 | |||
90 | /** |
||
91 | * @param array $specification |
||
92 | * |
||
93 | * @throws \Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException |
||
94 | */ |
||
95 | 106 | private function initProperties(array $specification) |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | * |
||
105 | * @see \Mcustiel\SimpleRequest\Validator\AbstractAnnotationSpecifiedValidator::validate() |
||
106 | */ |
||
107 | 105 | public function validate($value) |
|
115 | |||
116 | /** |
||
117 | * @param array $value |
||
118 | * |
||
119 | * @return bool |
||
120 | */ |
||
121 | 104 | private function executePropertiesValidation(array $value) |
|
128 | |||
129 | /** |
||
130 | * @param array $rest |
||
131 | * |
||
132 | * @return bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface|bool |
||
133 | */ |
||
134 | 97 | private function validateAdditionalProperties(array $rest) |
|
144 | |||
145 | /** |
||
146 | * @param array $rest |
||
147 | * @return boolean |
||
148 | */ |
||
149 | 88 | private function validateAgainstAdditionalPropertiesValidator(array $rest) |
|
158 | |||
159 | /** |
||
160 | * @param array $value |
||
161 | * @param array $rest |
||
162 | * |
||
163 | * @return bool |
||
164 | */ |
||
165 | 99 | private function validateByPattern(array $value, array &$rest) |
|
181 | |||
182 | /** |
||
183 | * @param array $value |
||
184 | * @param array $rest |
||
185 | * @param string $pattern |
||
186 | * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $validator |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | 12 | private function validateByPatternUsingValidator( |
|
206 | |||
207 | /** |
||
208 | * @param array $value |
||
209 | * @param array $rest |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | 104 | private function validateByProperty(array $value, array &$rest) |
|
227 | |||
228 | /** |
||
229 | * @param \stdClass|array $value |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | 104 | private function convertToArray($value) |
|
240 | |||
241 | /** |
||
242 | * Checks and sets items specification. |
||
243 | * |
||
244 | * @param bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $specification |
||
245 | * |
||
246 | * @throws \Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException |
||
247 | */ |
||
248 | 102 | private function setAdditionalProperties($specification) |
|
256 | |||
257 | /** |
||
258 | * Checks and sets pattern properties specification. |
||
259 | * |
||
260 | * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface[] $specification |
||
261 | * |
||
262 | * @throws \Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException |
||
263 | */ |
||
264 | 99 | private function setProperties($specification) |
|
277 | |||
278 | /** |
||
279 | * Checks and sets pattern properties specification. |
||
280 | * |
||
281 | * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface[] $specification |
||
282 | * |
||
283 | * @throws \Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException |
||
284 | */ |
||
285 | 93 | private function setPatternProperties($specification) |
|
298 | } |
||
299 |