Conditions | 9 |
Paths | 1 |
Total Lines | 69 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Tests | 28 |
CRAP Score | 9 |
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 |
||
19 | 17 | public static function bootCascadesDeletes() |
|
20 | { |
||
21 | // Setup the 'deleting' event listener. |
||
22 | static::deleting(function ($model) { |
||
1 ignored issue
–
show
|
|||
23 | |||
24 | // Wrap all of the cascading deletes inside of a transaction to make this an |
||
25 | // all or nothing operation. Any exceptions thrown inside the transaction |
||
26 | // need to bubble up to make sure all transactions will be rolled back. |
||
27 | $model->getConnectionResolver()->transaction(function () use ($model) { |
||
28 | |||
29 | 16 | $relations = $model->getCascadeDeletesRelations(); |
|
30 | |||
31 | 16 | if ($invalidRelations = $model->getInvalidCascadeDeletesRelations($relations)) { |
|
32 | 1 | throw new LogicException(sprintf('[%s]: invalid relationship(s) for cascading deletes. Relationship method(s) [%s] must return an object of type Illuminate\Database\Eloquent\Relations\Relation.', static::class, implode(', ', $invalidRelations))); |
|
33 | } |
||
34 | |||
35 | 15 | $deleteMethod = $model->isCascadeDeletesForceDeleting() ? 'forceDelete' : 'delete'; |
|
36 | |||
37 | 15 | foreach ($relations as $relationName => $relation) { |
|
38 | 15 | $expected = 0; |
|
1 ignored issue
–
show
|
|||
39 | 15 | $deleted = 0; |
|
40 | |||
41 | 15 | if ($relation instanceof BelongsToMany) { |
|
42 | // Process the many-to-many relationships on the model. |
||
43 | // These relationships should not delete the related |
||
44 | // record, but should just detach from each other. |
||
45 | |||
46 | 13 | $expected = $model->getCascadeDeletesRelationQuery($relationName)->count(); |
|
47 | |||
48 | 13 | $deleted = $model->getCascadeDeletesRelationQuery($relationName)->detach(); |
|
49 | 15 | } elseif ($relation instanceof HasOneOrMany) { |
|
50 | // Process the one-to-one and one-to-many relationships |
||
51 | // on the model. These relationships should actually |
||
52 | // delete the related records from the database. |
||
53 | |||
54 | 14 | $children = $model->getCascadeDeletesRelationQuery($relationName)->get(); |
|
55 | |||
56 | // To protect against potential relationship defaults, |
||
57 | // filter out any children that may not actually be |
||
58 | // Model instances, or that don't actually exist. |
||
59 | $children = $children->filter(function ($child) { |
||
60 | 14 | return $child instanceof Model && $child->exists; |
|
61 | 14 | })->all(); |
|
62 | |||
63 | 14 | $expected = count($children); |
|
64 | |||
65 | 14 | foreach ($children as $child) { |
|
66 | // Delete the record using the proper method. |
||
67 | 14 | $child->$deleteMethod(); |
|
68 | |||
69 | // forceDelete doesn't return anything until Laravel 5.2. Check |
||
70 | // exists property to determine if the delete was successful |
||
71 | // since that is the last thing set before delete returns. |
||
72 | 14 | $deleted += !$child->exists; |
|
73 | 14 | } |
|
74 | 14 | } else { |
|
75 | // Not all relationship types make sense for cascading. As an |
||
76 | // example, for a BelongsTo relationship, it does not make |
||
77 | // sense to delete the parent when the child is deleted. |
||
78 | 4 | throw new LogicException(sprintf('[%s]: error occurred deleting [%s]. Relation type [%s] not handled.', static::class, $relationName, get_class($relation))); |
|
79 | } |
||
80 | |||
81 | 14 | if ($deleted < $expected) { |
|
82 | 1 | throw new LogicException(sprintf('[%s]: error occurred deleting [%s]. Only deleted [%d] out of [%d] records.', static::class, $relationName, $deleted, $expected)); |
|
83 | } |
||
84 | 13 | } |
|
85 | 16 | }); |
|
86 | 17 | }); |
|
87 | 17 | } |
|
88 | |||
196 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.