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 MapperTest 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 MapperTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class MapperTest extends LanguageAwareTestCase |
||
29 | { |
||
30 | /** |
||
31 | * Value converter registry mock. |
||
32 | * |
||
33 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry |
||
34 | */ |
||
35 | protected $valueConverterRegistryMock; |
||
36 | |||
37 | /** |
||
38 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::__construct |
||
39 | */ |
||
40 | public function testCtor() |
||
52 | |||
53 | /** |
||
54 | * Returns a eZ\Publish\SPI\Persistence\Content\CreateStruct fixture. |
||
55 | * |
||
56 | * @return \eZ\Publish\SPI\Persistence\Content\CreateStruct |
||
57 | */ |
||
58 | protected function getCreateStructFixture() |
||
82 | |||
83 | /** |
||
84 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createVersionInfoForContent |
||
85 | */ |
||
86 | public function testCreateVersionInfoForContent() |
||
113 | |||
114 | /** |
||
115 | * Returns a Content fixture. |
||
116 | * |
||
117 | * @return Content |
||
118 | */ |
||
119 | protected function getFullContentFixture() |
||
142 | |||
143 | /** |
||
144 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::convertToStorageValue |
||
145 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue |
||
146 | */ |
||
147 | public function testConvertToStorageValue() |
||
177 | |||
178 | /** |
||
179 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::extractContentFromRows |
||
180 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::extractFieldFromRow |
||
181 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::extractFieldValueFromRow |
||
182 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue |
||
183 | */ |
||
184 | public function testExtractContentFromRows() |
||
227 | |||
228 | /** |
||
229 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::extractContentFromRows |
||
230 | */ |
||
231 | public function testExtractContentFromRowsMultipleVersions() |
||
277 | |||
278 | /** |
||
279 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createCreateStructFromContent |
||
280 | */ |
||
281 | public function testCreateCreateStructFromContent() |
||
304 | |||
305 | /** |
||
306 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createCreateStructFromContent |
||
307 | * @depends testCreateCreateStructFromContent |
||
308 | */ |
||
309 | public function testCreateCreateStructFromContentBasicProperties($data) |
||
325 | |||
326 | /** |
||
327 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createCreateStructFromContent |
||
328 | * @depends testCreateCreateStructFromContent |
||
329 | */ |
||
330 | public function testCreateCreateStructFromContentParentLocationsEmpty($data) |
||
337 | |||
338 | /** |
||
339 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createCreateStructFromContent |
||
340 | * @depends testCreateCreateStructFromContent |
||
341 | */ |
||
342 | public function testCreateCreateStructFromContentFieldCount($data) |
||
349 | |||
350 | /** |
||
351 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createCreateStructFromContent |
||
352 | * @depends testCreateCreateStructFromContent |
||
353 | */ |
||
354 | public function testCreateCreateStructFromContentFieldsNoId($data) |
||
360 | |||
361 | public function testExtractRelationsFromRows() |
||
374 | |||
375 | /** |
||
376 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createCreateStructFromContent |
||
377 | */ |
||
378 | public function testCreateCreateStructFromContentWithPreserveOriginalLanguage() |
||
397 | |||
398 | /** |
||
399 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::extractContentInfoFromRow |
||
400 | * @dataProvider extractContentInfoFromRowProvider |
||
401 | * |
||
402 | * @param array $fixtures |
||
403 | * @param string $prefix |
||
404 | */ |
||
405 | public function testExtractContentInfoFromRow(array $fixtures, $prefix) |
||
414 | |||
415 | /** |
||
416 | * Returns test data for {@link testExtractContentInfoFromRow()}. |
||
417 | * |
||
418 | * @return array |
||
419 | */ |
||
420 | public function extractContentInfoFromRowProvider() |
||
436 | |||
437 | /** |
||
438 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createRelationFromCreateStruct |
||
439 | */ |
||
440 | public function testCreateRelationFromCreateStruct() |
||
452 | |||
453 | /** |
||
454 | * Returns test data for {@link testExtractVersionInfoFromRow()}. |
||
455 | * |
||
456 | * @return array |
||
457 | */ |
||
458 | public function extractVersionInfoFromRowProvider() |
||
478 | |||
479 | /** |
||
480 | * Returns a fixture of database rows for content extraction. |
||
481 | * |
||
482 | * Fixture is stored in _fixtures/extract_content_from_rows.php |
||
483 | * |
||
484 | * @return array |
||
485 | */ |
||
486 | protected function getContentExtractFixture() |
||
490 | |||
491 | /** |
||
492 | * Returns a fixture of database rows for content names extraction. |
||
493 | * |
||
494 | * Fixture is stored in _fixtures/extract_names_from_rows.php |
||
495 | * |
||
496 | * @return array |
||
497 | */ |
||
498 | protected function getNamesExtractFixture() |
||
502 | |||
503 | /** |
||
504 | * Returns a reference result for content extraction. |
||
505 | * |
||
506 | * Fixture is stored in _fixtures/extract_content_from_rows_result.php |
||
507 | * |
||
508 | * @return Content |
||
509 | */ |
||
510 | protected function getContentExtractReference() |
||
514 | |||
515 | /** |
||
516 | * Returns a fixture for mapping multiple versions of a content object. |
||
517 | * |
||
518 | * @return string[][] |
||
519 | */ |
||
520 | protected function getMultipleVersionsExtractFixture() |
||
524 | |||
525 | /** |
||
526 | * Returns a fixture of database rows for content names extraction across multiple versions. |
||
527 | * |
||
528 | * Fixture is stored in _fixtures/extract_names_from_rows_multiple_versions.php |
||
529 | * |
||
530 | * @return array |
||
531 | */ |
||
532 | protected function getMultipleVersionsNamesExtractFixture() |
||
536 | |||
537 | /** |
||
538 | * Returns a fixture of database rows for relations extraction. |
||
539 | * |
||
540 | * Fixture is stored in _fixtures/relations.php |
||
541 | * |
||
542 | * @return array |
||
543 | */ |
||
544 | protected function getRelationExtractFixture() |
||
548 | |||
549 | /** |
||
550 | * Returns a reference result for content extraction. |
||
551 | * |
||
552 | * Fixture is stored in _fixtures/relations_results.php |
||
553 | * |
||
554 | * @return Content |
||
555 | */ |
||
556 | protected function getRelationExtractReference() |
||
560 | |||
561 | /** |
||
562 | * Returns a Mapper. |
||
563 | * |
||
564 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\Mapper |
||
565 | */ |
||
566 | protected function getMapper($valueConverter = null) |
||
573 | |||
574 | /** |
||
575 | * Returns a FieldValue converter registry mock. |
||
576 | * |
||
577 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry |
||
578 | */ |
||
579 | protected function getValueConverterRegistryMock() |
||
591 | |||
592 | /** |
||
593 | * Returns a eZ\Publish\SPI\Persistence\Content\CreateStruct fixture. |
||
594 | * |
||
595 | * @return \eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct |
||
596 | */ |
||
597 | View Code Duplication | protected function getRelationCreateStructFixture() |
|
609 | |||
610 | /** |
||
611 | * Returns a language handler mock. |
||
612 | * |
||
613 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler |
||
614 | */ |
||
615 | protected function getLanguageHandler() |
||
666 | } |
||
667 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..