Conditions | 20 |
Paths | 378 |
Total Lines | 89 |
Code Lines | 57 |
Lines | 32 |
Ratio | 35.96 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
117 | public function processFieldRelations( |
||
118 | array $inputRelations, |
||
119 | $sourceContentId, |
||
120 | $sourceContentVersionNo, |
||
121 | ContentType $contentType, |
||
122 | array $existingRelations = array() |
||
123 | ) { |
||
124 | // Map existing relations for easier handling |
||
125 | $mappedRelations = array(); |
||
126 | foreach ($existingRelations as $relation) { |
||
127 | if ($relation->type === Relation::FIELD) { |
||
128 | $fieldDefinitionId = $contentType->getFieldDefinition($relation->sourceFieldDefinitionIdentifier)->id; |
||
129 | $mappedRelations[$relation->type][$fieldDefinitionId][$relation->destinationContentInfo->id] = $relation; |
||
130 | } |
||
131 | // Using bitwise AND as Legacy Stack stores COMMON, LINK and EMBED relation types |
||
132 | // in the same entry using bitmask |
||
133 | View Code Duplication | if ($relation->type & Relation::LINK) { |
|
|
|||
134 | $mappedRelations[Relation::LINK][$relation->destinationContentInfo->id] = $relation; |
||
135 | } |
||
136 | View Code Duplication | if ($relation->type & Relation::EMBED) { |
|
137 | $mappedRelations[Relation::EMBED][$relation->destinationContentInfo->id] = $relation; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | // Add new relations |
||
142 | foreach ($inputRelations as $relationType => $relationData) { |
||
143 | if ($relationType === Relation::FIELD) { |
||
144 | foreach ($relationData as $fieldDefinitionId => $contentIds) { |
||
145 | foreach (array_keys($contentIds) as $destinationContentId) { |
||
146 | if (isset($mappedRelations[$relationType][$fieldDefinitionId][$destinationContentId])) { |
||
147 | unset($mappedRelations[$relationType][$fieldDefinitionId][$destinationContentId]); |
||
148 | View Code Duplication | } else { |
|
149 | $this->persistenceHandler->contentHandler()->addRelation( |
||
150 | new SPIRelationCreateStruct( |
||
151 | array( |
||
152 | 'sourceContentId' => $sourceContentId, |
||
153 | 'sourceContentVersionNo' => $sourceContentVersionNo, |
||
154 | 'sourceFieldDefinitionId' => $fieldDefinitionId, |
||
155 | 'destinationContentId' => $destinationContentId, |
||
156 | 'type' => $relationType, |
||
157 | ) |
||
158 | ) |
||
159 | ); |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | } elseif ($relationType === Relation::LINK || $relationType === Relation::EMBED) { |
||
164 | foreach (array_keys($relationData) as $destinationContentId) { |
||
165 | if (isset($mappedRelations[$relationType][$destinationContentId])) { |
||
166 | unset($mappedRelations[$relationType][$destinationContentId]); |
||
167 | View Code Duplication | } else { |
|
168 | $this->persistenceHandler->contentHandler()->addRelation( |
||
169 | new SPIRelationCreateStruct( |
||
170 | array( |
||
171 | 'sourceContentId' => $sourceContentId, |
||
172 | 'sourceContentVersionNo' => $sourceContentVersionNo, |
||
173 | 'sourceFieldDefinitionId' => null, |
||
174 | 'destinationContentId' => $destinationContentId, |
||
175 | 'type' => $relationType, |
||
176 | ) |
||
177 | ) |
||
178 | ); |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | // Remove relations not present in input set |
||
185 | foreach ($mappedRelations as $relationType => $relationData) { |
||
186 | foreach ($relationData as $relationEntry) { |
||
187 | switch ($relationType) { |
||
188 | case Relation::FIELD: |
||
189 | foreach ($relationEntry as $relation) { |
||
190 | $this->persistenceHandler->contentHandler()->removeRelation( |
||
191 | $relation->id, |
||
192 | $relationType |
||
193 | ); |
||
194 | } |
||
195 | break; |
||
196 | case Relation::LINK: |
||
197 | case Relation::EMBED: |
||
198 | $this->persistenceHandler->contentHandler()->removeRelation( |
||
199 | $relationEntry->id, |
||
200 | $relationType |
||
201 | ); |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | |||
264 |
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.