| Conditions | 14 |
| Paths | 267 |
| Total Lines | 87 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 210 |
| Changes | 0 | ||
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 |
||
| 266 | public function afterElementSave(MetaField $field, ElementInterface $owner) |
||
| 267 | { |
||
| 268 | /** @var Element $owner */ |
||
| 269 | |||
| 270 | /** @var MetaQuery $query */ |
||
| 271 | $query = $owner->getFieldValue($field->handle); |
||
| 272 | |||
| 273 | // Skip if the query's site ID is different than the element's |
||
| 274 | // (Indicates that the value as copied from another site for element propagation) |
||
| 275 | if ($query->siteId != $owner->siteId) { |
||
|
1 ignored issue
–
show
|
|||
| 276 | return; |
||
| 277 | } |
||
| 278 | |||
| 279 | if (null === ($elements = $query->getCachedResult())) { |
||
| 280 | $query = clone $query; |
||
| 281 | $query->status = null; |
||
| 282 | $query->enabledForSite = false; |
||
| 283 | $elements = $query->all(); // existing meta |
||
| 284 | } |
||
| 285 | |||
| 286 | |||
| 287 | $transaction = Craft::$app->getDb()->beginTransaction(); |
||
| 288 | try { |
||
| 289 | // If this is a preexisting element, make sure that the blocks for this field/owner respect the field's translation setting |
||
| 290 | if ($query->ownerId) { |
||
| 291 | $this->applyFieldTranslationSetting($query->ownerId, $query->siteId, $field); |
||
| 292 | } |
||
| 293 | |||
| 294 | // If the query is set to fetch blocks of a different owner, we're probably duplicating an element |
||
| 295 | if ($query->ownerId && $query->ownerId != $owner->id) { |
||
|
1 ignored issue
–
show
|
|||
| 296 | // Make sure this owner doesn't already have meta |
||
| 297 | $newQuery = clone $query; |
||
| 298 | $newQuery->ownerId = $owner->id; |
||
|
1 ignored issue
–
show
|
|||
| 299 | if (!$newQuery->exists()) { |
||
| 300 | // Duplicate the blocks for the new owner |
||
| 301 | $elementsService = Craft::$app->getElements(); |
||
| 302 | foreach ($elements as $element) { |
||
| 303 | $elementsService->duplicateElement($element, [ |
||
| 304 | 'ownerId' => $owner->id, |
||
|
1 ignored issue
–
show
|
|||
| 305 | 'ownerSiteId' => $field->localize ? $owner->siteId : null |
||
|
1 ignored issue
–
show
|
|||
| 306 | ]); |
||
| 307 | } |
||
| 308 | } |
||
| 309 | } else { |
||
| 310 | $elementIds = []; |
||
| 311 | |||
| 312 | // Only propagate the blocks if the owner isn't being propagated |
||
| 313 | $propagate = !$owner->propagating; |
||
| 314 | |||
| 315 | /** @var MetaElement $element */ |
||
| 316 | foreach ($elements as $element) { |
||
| 317 | $element->setOwnerId($owner->id); |
||
|
1 ignored issue
–
show
|
|||
| 318 | $element->ownerSiteId = ($field->localize ? $owner->siteId : null); |
||
|
1 ignored issue
–
show
|
|||
| 319 | $element->propagating = $owner->propagating; |
||
| 320 | |||
| 321 | Craft::$app->getElements()->saveElement($element, false, $propagate); |
||
| 322 | |||
| 323 | $elementIds[] = $element->id; |
||
| 324 | } |
||
| 325 | |||
| 326 | // Delete any elements that shouldn't be there anymore |
||
| 327 | $deleteElementsQuery = MetaElement::find() |
||
| 328 | ->status(null) |
||
| 329 | ->enabledForSite(false) |
||
| 330 | ->ownerId($owner->id) |
||
|
1 ignored issue
–
show
|
|||
| 331 | ->fieldId($field->id) |
||
| 332 | ->where(['not', ['elements.id' => $elementIds]]); |
||
| 333 | |||
| 334 | if ($field->localize) { |
||
| 335 | $deleteElementsQuery->ownerSiteId($owner->siteId); |
||
|
1 ignored issue
–
show
|
|||
| 336 | } else { |
||
| 337 | $deleteElementsQuery->siteId($owner->siteId); |
||
|
1 ignored issue
–
show
|
|||
| 338 | } |
||
| 339 | |||
| 340 | foreach ($deleteElementsQuery->all() as $deleteElement) { |
||
| 341 | Craft::$app->getElements()->deleteElement($deleteElement); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | $transaction->commit(); |
||
| 346 | } catch (\Exception $e) { |
||
| 347 | $transaction->rollback(); |
||
| 348 | throw $e; |
||
| 349 | } |
||
| 350 | |||
| 351 | return; |
||
| 352 | } |
||
| 353 | |||
| 466 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: