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 |
||
25 | class RelationProcessor |
||
26 | { |
||
27 | /** |
||
28 | * @var \eZ\Publish\SPI\Persistence\Handler |
||
29 | */ |
||
30 | protected $persistenceHandler; |
||
31 | |||
32 | /** |
||
33 | * Setups service with reference to repository object that created it & corresponding handler. |
||
34 | * |
||
35 | * @param \eZ\Publish\SPI\Persistence\Handler $handler |
||
36 | */ |
||
37 | public function __construct(Handler $handler) |
||
38 | { |
||
39 | $this->persistenceHandler = $handler; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Appends destination Content ids of given $fieldValue to the $relation array. |
||
44 | * |
||
45 | * If $fieldValue contains Location ids, the will be converted to the Content id that Location encapsulates. |
||
46 | * |
||
47 | * @param array $relations |
||
48 | * @param array $locationIdToContentIdMapping An array with Location Ids as keys and corresponding Content Id as values |
||
49 | * @param \eZ\Publish\SPI\FieldType\FieldType $fieldType |
||
50 | * @param \eZ\Publish\Core\FieldType\Value $fieldValue Accepted field value. |
||
51 | * @param string $fieldDefinitionId |
||
52 | */ |
||
53 | public function appendFieldRelations( |
||
54 | array &$relations, |
||
55 | array &$locationIdToContentIdMapping, |
||
56 | SPIFieldType $fieldType, |
||
57 | BaseValue $fieldValue, |
||
58 | $fieldDefinitionId |
||
59 | ) { |
||
60 | foreach ($fieldType->getRelations($fieldValue) as $relationType => $destinationIds) { |
||
61 | if ($relationType === Relation::FIELD) { |
||
62 | if (!isset($relations[$relationType][$fieldDefinitionId])) { |
||
63 | $relations[$relationType][$fieldDefinitionId] = array(); |
||
64 | } |
||
65 | $relations[$relationType][$fieldDefinitionId] += array_flip($destinationIds); |
||
66 | } elseif ($relationType & (Relation::LINK | Relation::EMBED)) { |
||
67 | // Using bitwise operators as Legacy Stack stores COMMON, LINK and EMBED relation types |
||
68 | // in the same entry using bitmask |
||
69 | if (!isset($relations[$relationType])) { |
||
70 | $relations[$relationType] = array(); |
||
71 | } |
||
72 | |||
73 | if (isset($destinationIds['locationIds'])) { |
||
74 | foreach ($destinationIds['locationIds'] as $locationId) { |
||
75 | if (!isset($locationIdToContentIdMapping[$locationId])) { |
||
76 | $location = $this->persistenceHandler->locationHandler()->load($locationId); |
||
77 | $locationIdToContentIdMapping[$locationId] = $location->contentId; |
||
78 | } |
||
79 | |||
80 | $relations[$relationType][$locationIdToContentIdMapping[$locationId]] = true; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | if (isset($destinationIds['contentIds'])) { |
||
85 | $relations[$relationType] += array_flip($destinationIds['contentIds']); |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Persists relation data for a content version. |
||
93 | * |
||
94 | * This method creates new relations and deletes removed relations. |
||
95 | * |
||
96 | * @param array $inputRelations |
||
97 | * @param mixed $sourceContentId |
||
98 | * @param mixed $sourceContentVersionNo |
||
99 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
||
100 | * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $existingRelations An array of existing relations for Content version (empty when creating new content) |
||
101 | */ |
||
102 | public function processFieldRelations( |
||
191 | } |
||
192 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.