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 FieldHandler 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 FieldHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class FieldHandler |
||
24 | { |
||
25 | /** |
||
26 | * Content Gateway. |
||
27 | * |
||
28 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Gateway |
||
29 | */ |
||
30 | protected $contentGateway; |
||
31 | |||
32 | /** |
||
33 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler |
||
34 | */ |
||
35 | protected $languageHandler; |
||
36 | |||
37 | /** |
||
38 | * Content Mapper. |
||
39 | * |
||
40 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Mapper |
||
41 | */ |
||
42 | protected $mapper; |
||
43 | |||
44 | /** |
||
45 | * Storage Handler. |
||
46 | * |
||
47 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\StorageHandler |
||
48 | */ |
||
49 | protected $storageHandler; |
||
50 | |||
51 | /** |
||
52 | * FieldType registry. |
||
53 | * |
||
54 | * @var \eZ\Publish\Core\Persistence\FieldTypeRegistry |
||
55 | */ |
||
56 | protected $fieldTypeRegistry; |
||
57 | |||
58 | /** |
||
59 | * Hash of SPI FieldTypes or callable callbacks to generate one. |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $fieldTypes; |
||
64 | |||
65 | /** |
||
66 | * Creates a new Field Handler. |
||
67 | * |
||
68 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Gateway $contentGateway |
||
69 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Mapper $mapper |
||
70 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageHandler $storageHandler |
||
71 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
72 | * @param \eZ\Publish\Core\Persistence\FieldTypeRegistry $fieldTypeRegistry |
||
73 | */ |
||
74 | public function __construct( |
||
87 | |||
88 | /** |
||
89 | * Creates new fields in the database from $content of $contentType. |
||
90 | * |
||
91 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
92 | * @param \eZ\Publish\SPI\Persistence\Content\Type $contentType |
||
93 | */ |
||
94 | public function createNewFields(Content $content, Type $contentType) |
||
125 | |||
126 | /** |
||
127 | * Returns empty Field object for given field definition and language code. |
||
128 | * |
||
129 | * Uses FieldType to create empty field value. |
||
130 | * |
||
131 | * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDefinition |
||
132 | * @param string $languageCode |
||
133 | * |
||
134 | * @return \eZ\Publish\SPI\Persistence\Content\Field |
||
135 | */ |
||
136 | protected function getEmptyField(FieldDefinition $fieldDefinition, $languageCode) |
||
149 | |||
150 | /** |
||
151 | * Creates existing fields in a new version for $content. |
||
152 | * |
||
153 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
154 | */ |
||
155 | public function createExistingFieldsInNewVersion(Content $content) |
||
161 | |||
162 | /** |
||
163 | * Creates a new field in the database. |
||
164 | * |
||
165 | * Used by self::createNewFields() and self::updateFields() |
||
166 | * |
||
167 | * @param \eZ\Publish\SPI\Persistence\Content\Field $field |
||
168 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
169 | */ |
||
170 | View Code Duplication | protected function createNewField(Field $field, Content $content) |
|
190 | |||
191 | /** |
||
192 | * @param array $fields |
||
193 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
194 | */ |
||
195 | protected function copyFields(array $fields, Content $content) |
||
203 | |||
204 | /** |
||
205 | * Copies existing field to new field for given $languageCode. |
||
206 | * |
||
207 | * Used by self::createNewFields() and self::updateFields() |
||
208 | * |
||
209 | * @param \eZ\Publish\SPI\Persistence\Content\Field $originalField |
||
210 | * @param string $languageCode |
||
211 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
212 | */ |
||
213 | protected function copyField(Field $originalField, $languageCode, Content $content) |
||
237 | |||
238 | /** |
||
239 | * Updates an existing field in the database. |
||
240 | * |
||
241 | * Used by self::createNewFields() and self::updateFields() |
||
242 | * |
||
243 | * @param \eZ\Publish\SPI\Persistence\Content\Field $field |
||
244 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
245 | */ |
||
246 | protected function updateField(Field $field, Content $content) |
||
263 | |||
264 | /** |
||
265 | * Creates an existing field in a new version, no new ID is generated. |
||
266 | * |
||
267 | * Used to insert a field with an existing ID but a new version number. |
||
268 | * $content is used for new version data, needed by Content gateway and external storage. |
||
269 | * |
||
270 | * External data is being copied here as some FieldTypes require original field external data. |
||
271 | * By default copying falls back to storing, it is upon external storage implementation to override |
||
272 | * the behaviour as needed. |
||
273 | * |
||
274 | * @param Field $field |
||
275 | * @param Content $content |
||
276 | */ |
||
277 | View Code Duplication | protected function createExistingFieldInNewVersion(Field $field, Content $content) |
|
298 | |||
299 | /** |
||
300 | * Performs external loads for the fields in $content. |
||
301 | * |
||
302 | * @param Content $content |
||
303 | */ |
||
304 | public function loadExternalFieldData(Content $content) |
||
310 | |||
311 | /** |
||
312 | * Updates the fields in for content identified by $contentId and $versionNo in the database in respect to $updateStruct. |
||
313 | * |
||
314 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
315 | * @param \eZ\Publish\SPI\Persistence\Content\UpdateStruct $updateStruct |
||
316 | * @param \eZ\Publish\SPI\Persistence\Content\Type $contentType |
||
317 | */ |
||
318 | public function updateFields(Content $content, UpdateStruct $updateStruct, Type $contentType) |
||
380 | |||
381 | /** |
||
382 | * Updates a language copy of a non-translatable field. |
||
383 | * |
||
384 | * External data is being copied here as some FieldTypes require original field external data. |
||
385 | * By default copying falls back to storing, it is upon external storage implementation to override |
||
386 | * the behaviour as needed. |
||
387 | * |
||
388 | * @param \eZ\Publish\SPI\Persistence\Content\Field $field |
||
389 | * @param \eZ\Publish\SPI\Persistence\Content\Field $updateField |
||
390 | * @param \eZ\Publish\SPI\Persistence\Content\Field $originalField |
||
391 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
392 | */ |
||
393 | View Code Duplication | protected function updateCopiedField(Field $field, Field $updateField, Field $originalField, Content $content) |
|
413 | |||
414 | /** |
||
415 | * Returns given $fields structured in hash array with field definition ids and language codes as keys. |
||
416 | * |
||
417 | * @param \eZ\Publish\SPI\Persistence\Content\Field[] $fields |
||
418 | * @param array $languageCodes |
||
419 | * |
||
420 | * @return \eZ\Publish\SPI\Persistence\Content\Field[][] |
||
421 | */ |
||
422 | protected function getFieldMap(array $fields, &$languageCodes = null) |
||
434 | |||
435 | /** |
||
436 | * Deletes the fields for $contentId in $versionInfo from the database. |
||
437 | * |
||
438 | * @param int $contentId |
||
439 | * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo |
||
440 | */ |
||
441 | public function deleteFields($contentId, VersionInfo $versionInfo) |
||
448 | } |
||
449 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.