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:
Complex classes like DocumentMap 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 DocumentMap, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class DocumentMap |
||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $mappings = []; |
||
23 | /** |
||
24 | * @var Document[] |
||
25 | */ |
||
26 | private $documents = []; |
||
27 | |||
28 | /** |
||
29 | * Constructor |
||
30 | * |
||
31 | * @param Finder $doctrineFinder Doctrine mapping finder |
||
32 | * @param Finder $serializerFinder Serializer mapping finder |
||
33 | * @param Finder $validationFinder Validation mapping finder |
||
34 | * @param Finder $schemaFinder Schema finder |
||
35 | */ |
||
36 | 10 | public function __construct( |
|
37 | Finder $doctrineFinder, |
||
38 | Finder $serializerFinder, |
||
39 | Finder $validationFinder, |
||
40 | Finder $schemaFinder |
||
41 | ) { |
||
42 | 10 | $doctrineMap = $this->loadDoctrineClassMap($doctrineFinder); |
|
43 | 10 | $serializerMap = $this->loadSerializerClassMap($serializerFinder); |
|
44 | 10 | $validationMap = $this->loadValidationClassMap($validationFinder); |
|
45 | 10 | $schemaMap = $this->loadSchemaClassMap($schemaFinder); |
|
46 | |||
47 | 10 | foreach ($doctrineMap as $className => $doctrineMapping) { |
|
48 | 10 | $this->mappings[$className] = [ |
|
49 | 10 | 'doctrine' => $doctrineMap[$className], |
|
50 | 10 | 'serializer' => isset($serializerMap[$className]) ? $serializerMap[$className] : null, |
|
51 | 10 | 'validation' => isset($validationMap[$className]) ? $validationMap[$className] : null, |
|
52 | 10 | 'schema' => isset($schemaMap[$className]) ? $schemaMap[$className] : null, |
|
53 | ]; |
||
54 | 10 | } |
|
55 | 10 | } |
|
56 | |||
57 | /** |
||
58 | * Get document |
||
59 | * |
||
60 | * @param string $className Document class |
||
61 | * @return Document |
||
62 | */ |
||
63 | 10 | public function getDocument($className) |
|
64 | { |
||
65 | 10 | if (isset($this->documents[$className])) { |
|
66 | 10 | return $this->documents[$className]; |
|
67 | } |
||
68 | 10 | if (!isset($this->mappings[$className])) { |
|
69 | throw new \InvalidArgumentException(sprintf('No XML mapping found for document "%s"', $className)); |
||
70 | } |
||
71 | |||
72 | 10 | return $this->documents[$className] = $this->processDocument( |
|
73 | 10 | $className, |
|
74 | 10 | $this->mappings[$className]['doctrine'], |
|
75 | 10 | $this->mappings[$className]['serializer'], |
|
76 | 10 | $this->mappings[$className]['validation'], |
|
77 | 10 | $this->mappings[$className]['schema'] |
|
78 | 10 | ); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Get all documents |
||
83 | * |
||
84 | * @return Document[] |
||
85 | */ |
||
86 | 6 | public function getDocuments() |
|
90 | |||
91 | /** |
||
92 | * Process document |
||
93 | * |
||
94 | * @param string $className Class name |
||
95 | * @param \DOMElement $doctrineMapping Doctrine XML mapping |
||
96 | * @param \DOMElement $serializerMapping Serializer XML mapping |
||
|
|||
97 | * @param \DOMElement $validationMapping Validation XML mapping |
||
98 | * @param array $schemaMapping Schema mapping |
||
99 | * |
||
100 | * @return Document |
||
101 | */ |
||
102 | 10 | private function processDocument( |
|
103 | $className, |
||
104 | \DOMElement $doctrineMapping, |
||
105 | \DOMElement $serializerMapping = null, |
||
106 | \DOMElement $validationMapping = null, |
||
107 | array $schemaMapping = null |
||
108 | ) { |
||
109 | 10 | if ($serializerMapping === null) { |
|
110 | 2 | $serializerFields = []; |
|
111 | 2 | } else { |
|
112 | 10 | $serializerFields = array_reduce( |
|
113 | 10 | $this->getSerializerFields($serializerMapping), |
|
114 | function (array $fields, array $field) { |
||
115 | 10 | $fields[$field['fieldName']] = $field; |
|
116 | 10 | return $fields; |
|
117 | 10 | }, |
|
118 | 10 | [] |
|
119 | 10 | ); |
|
120 | } |
||
121 | |||
122 | 10 | if ($validationMapping === null) { |
|
123 | 2 | $validationFields = []; |
|
124 | 2 | } else { |
|
125 | 10 | $validationFields = array_reduce( |
|
126 | 10 | $this->getValidationFields($validationMapping), |
|
127 | function (array $fields, array $field) { |
||
128 | 4 | $fields[$field['fieldName']] = $field; |
|
129 | 4 | return $fields; |
|
130 | 10 | }, |
|
131 | 10 | [] |
|
132 | 10 | ); |
|
133 | } |
||
134 | |||
135 | 10 | if ($schemaMapping === null) { |
|
136 | 10 | $schemaFields = []; |
|
137 | 10 | } else { |
|
138 | 2 | $schemaFields = $schemaMapping; |
|
139 | } |
||
140 | |||
141 | 10 | $fields = []; |
|
142 | 10 | foreach ($this->getDoctrineFields($doctrineMapping) as $doctrineField) { |
|
143 | 10 | $serializerField = isset($serializerFields[$doctrineField['name']]) ? |
|
144 | 10 | $serializerFields[$doctrineField['name']] : |
|
145 | 10 | null; |
|
146 | 10 | $validationField = isset($validationFields[$doctrineField['name']]) ? |
|
147 | 10 | $validationFields[$doctrineField['name']] : |
|
148 | 10 | null; |
|
149 | 10 | $schemaField = isset($schemaFields[$doctrineField['name']]) ? |
|
150 | 10 | $schemaFields[$doctrineField['name']] : |
|
151 | 10 | null; |
|
152 | |||
153 | 10 | if ($doctrineField['type'] === 'collection') { |
|
154 | 2 | $fields[] = new ArrayField( |
|
155 | 2 | $serializerField === null ? 'array<string>' : $serializerField['fieldType'], |
|
156 | 2 | $doctrineField['name'], |
|
157 | 2 | $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'], |
|
158 | 2 | !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'], |
|
159 | 2 | $validationField === null ? false : $validationField['required'], |
|
160 | 2 | $serializerField === null ? false : $serializerField['searchable'], |
|
161 | 2 | !isset($schemaField['recordOriginException']) ? false : $schemaField['recordOriginException'] |
|
162 | 2 | ); |
|
163 | 2 | } else { |
|
164 | 10 | $fields[] = new Field( |
|
165 | 10 | $doctrineField['type'], |
|
166 | 10 | $doctrineField['name'], |
|
167 | 10 | $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'], |
|
168 | 10 | !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'], |
|
169 | 10 | $validationField === null ? false : $validationField['required'], |
|
170 | 10 | $serializerField === null ? false : $serializerField['searchable'], |
|
171 | 10 | !isset($schemaField['recordOriginException']) ? false : $schemaField['recordOriginException'] |
|
172 | 10 | ); |
|
173 | } |
||
174 | 10 | } |
|
175 | 10 | foreach ($this->getDoctrineEmbedOneFields($doctrineMapping) as $doctrineField) { |
|
176 | 10 | $serializerField = isset($serializerFields[$doctrineField['name']]) ? |
|
177 | 10 | $serializerFields[$doctrineField['name']] : |
|
178 | 10 | null; |
|
179 | 10 | $validationField = isset($validationFields[$doctrineField['name']]) ? |
|
180 | 10 | $validationFields[$doctrineField['name']] : |
|
181 | 10 | null; |
|
182 | 10 | $schemaField = isset($schemaFields[$doctrineField['name']]) ? |
|
183 | 10 | $schemaFields[$doctrineField['name']] : |
|
184 | 10 | null; |
|
185 | |||
186 | 10 | $fields[] = new EmbedOne( |
|
187 | 10 | $this->getDocument($doctrineField['type']), |
|
188 | 10 | $doctrineField['name'], |
|
189 | 10 | $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'], |
|
190 | 10 | !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'], |
|
191 | 10 | $validationField === null ? false : $validationField['required'], |
|
192 | 10 | $serializerField === null ? false : $serializerField['searchable'], |
|
193 | 10 | !isset($schemaField['recordOriginException']) ? false : $schemaField['recordOriginException'] |
|
194 | 10 | ); |
|
195 | 10 | } |
|
196 | 10 | foreach ($this->getDoctrineEmbedManyFields($doctrineMapping) as $doctrineField) { |
|
197 | 10 | $serializerField = isset($serializerFields[$doctrineField['name']]) ? |
|
198 | 10 | $serializerFields[$doctrineField['name']] : |
|
199 | 10 | null; |
|
200 | 10 | $validationField = isset($validationFields[$doctrineField['name']]) ? |
|
201 | 10 | $validationFields[$doctrineField['name']] : |
|
202 | 10 | null; |
|
203 | 10 | $schemaField = isset($schemaFields[$doctrineField['name']]) ? |
|
204 | 10 | $schemaFields[$doctrineField['name']] : |
|
205 | 10 | null; |
|
206 | |||
207 | 10 | $fields[] = new EmbedMany( |
|
208 | 10 | $this->getDocument($doctrineField['type']), |
|
209 | 10 | $doctrineField['name'], |
|
210 | 10 | $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'], |
|
211 | 10 | !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'], |
|
212 | 10 | $validationField === null ? false : $validationField['required'], |
|
213 | 10 | !isset($schemaField['recordOriginException']) ? false : $schemaField['recordOriginException'] |
|
214 | 10 | ); |
|
215 | 10 | } |
|
216 | |||
217 | 10 | return new Document($className, $fields); |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * Load doctrine class map |
||
222 | * |
||
223 | * @param Finder $finder Mapping finder |
||
224 | * @return array |
||
225 | */ |
||
226 | 10 | View Code Duplication | private function loadDoctrineClassMap(Finder $finder) |
248 | |||
249 | /** |
||
250 | * Load serializer class map |
||
251 | * |
||
252 | * @param Finder $finder Mapping finder |
||
253 | * @return array |
||
254 | */ |
||
255 | 10 | View Code Duplication | private function loadSerializerClassMap(Finder $finder) |
276 | |||
277 | /** |
||
278 | * Load schema class map |
||
279 | * |
||
280 | * @param Finder $finder Mapping finder |
||
281 | * @return array |
||
282 | */ |
||
283 | 10 | private function loadSchemaClassMap(Finder $finder) |
|
284 | { |
||
285 | 10 | $classMap = []; |
|
286 | 10 | foreach ($finder as $file) { |
|
287 | 10 | $schema = json_decode(file_get_contents($file), true); |
|
288 | |||
289 | 10 | if (!isset($schema['x-documentClass'])) { |
|
290 | 10 | continue; |
|
291 | } |
||
292 | |||
293 | 2 | View Code Duplication | foreach ($schema['required'] as $field) { |
294 | 2 | $classMap[$schema['x-documentClass']][$field]['required'] = true; |
|
295 | 2 | } |
|
296 | 2 | foreach ($schema['searchable'] as $field) { |
|
297 | 2 | $classMap[$schema['x-documentClass']][$field]['searchable'] = 1; |
|
298 | 2 | } |
|
299 | 2 | View Code Duplication | foreach ($schema['readOnlyFields'] as $field) { |
300 | 2 | $classMap[$schema['x-documentClass']][$field]['readOnly'] = true; |
|
301 | 2 | } |
|
302 | |||
303 | // flags from fields |
||
304 | 2 | if (is_array($schema['properties'])) { |
|
305 | 2 | foreach ($schema['properties'] as $fieldName => $field) { |
|
306 | 2 | if (isset($field['recordOriginException']) && $field['recordOriginException'] == true) { |
|
307 | 2 | $classMap[$schema['x-documentClass']][$fieldName]['recordOriginException'] = true; |
|
308 | 2 | } |
|
309 | 2 | } |
|
310 | 2 | } |
|
311 | 10 | } |
|
312 | |||
313 | 10 | return $classMap; |
|
314 | } |
||
315 | |||
316 | /** |
||
317 | * Load validation class map |
||
318 | * |
||
319 | * @param Finder $finder Mapping finder |
||
320 | * @return array |
||
321 | */ |
||
322 | 10 | View Code Duplication | private function loadValidationClassMap(Finder $finder) |
344 | |||
345 | /** |
||
346 | * Get serializer fields |
||
347 | * |
||
348 | * @param \DOMElement $mapping Serializer XML mapping |
||
349 | * @return array |
||
350 | */ |
||
351 | 10 | private function getSerializerFields(\DOMElement $mapping) |
|
368 | |||
369 | /** |
||
370 | * Get serializer field type |
||
371 | * |
||
372 | * @param \DOMElement $field Field node |
||
373 | * @return string|null |
||
374 | */ |
||
375 | 10 | private function getSerializerFieldType(\DOMElement $field) |
|
386 | |||
387 | /** |
||
388 | * Get validation fields |
||
389 | * |
||
390 | * @param \DOMElement $mapping Validation XML mapping |
||
391 | * @return array |
||
392 | */ |
||
393 | 10 | private function getValidationFields(\DOMElement $mapping) |
|
409 | |||
410 | /** |
||
411 | * Get doctrine document fields |
||
412 | * |
||
413 | * @param \DOMElement $mapping Doctrine XML mapping |
||
414 | * @return array |
||
415 | */ |
||
416 | 10 | View Code Duplication | private function getDoctrineFields(\DOMElement $mapping) |
431 | |||
432 | /** |
||
433 | * Get doctrine document embed-one fields |
||
434 | * |
||
435 | * @param \DOMElement $mapping Doctrine XML mapping |
||
436 | * @return array |
||
437 | */ |
||
438 | 10 | View Code Duplication | private function getDoctrineEmbedOneFields(\DOMElement $mapping) |
453 | |||
454 | /** |
||
455 | * Get doctrine document embed-many fields |
||
456 | * |
||
457 | * @param \DOMElement $mapping Doctrine XML mapping |
||
458 | * @return array |
||
459 | */ |
||
460 | 10 | View Code Duplication | private function getDoctrineEmbedManyFields(\DOMElement $mapping) |
475 | |||
476 | /** |
||
477 | * Gets an array of all fields, flat with full internal name in dot notation as key and |
||
478 | * the exposed field name as value. You can pass a callable to limit the fields return a subset of fields. |
||
479 | * If the callback returns true, the field will be included in the output. You will get the field definition |
||
480 | * passed to your callback. |
||
481 | * |
||
482 | * @param Document $document The document |
||
483 | * @param string $documentPrefix Document field prefix |
||
484 | * @param string $exposedPrefix Exposed field prefix |
||
485 | * @param callable $callback An optional callback where you can influence the number of fields returned |
||
486 | * |
||
487 | * @return array |
||
488 | */ |
||
489 | 4 | public function getFieldNamesFlat( |
|
535 | |||
536 | /** |
||
537 | * Simple function to check whether a given shall be returned in the output of getFieldNamesFlat |
||
538 | * and the optional given callback there. |
||
539 | * |
||
540 | * @param AbstractField $field field |
||
541 | * @param callable|null $callback optional callback |
||
542 | * |
||
543 | * @return bool|mixed true if field should be returned, false otherwise |
||
544 | */ |
||
545 | 4 | private function getFlatFieldCheckCallback($field, callable $callback = null) |
|
553 | } |
||
554 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.