| Conditions | 9 |
| Paths | 21 |
| Total Lines | 83 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 49 | public function save(Model $model, bool $runValidation = true, $attributes = null, bool $mirrorScenario = true) |
||
| 50 | { |
||
| 51 | |||
| 52 | // Validate |
||
| 53 | if ($runValidation && !$model->validate($attributes)) { |
||
| 54 | Craft::info('Model not saved due to validation error.', __METHOD__); |
||
| 55 | return false; |
||
| 56 | } |
||
| 57 | |||
| 58 | // Create event |
||
| 59 | $event = new ModelEvent([ |
||
| 60 | 'isNew' => null !== $model->id |
||
| 61 | ]); |
||
| 62 | |||
| 63 | // Db transaction |
||
| 64 | $transaction = RecordHelper::beginTransaction(); |
||
| 65 | |||
| 66 | try { |
||
| 67 | |||
| 68 | // The 'before' event |
||
| 69 | if (!$model->beforeSave($event)) { |
||
| 70 | |||
| 71 | $transaction->rollBack(); |
||
| 72 | |||
| 73 | return false; |
||
| 74 | } |
||
| 75 | |||
| 76 | $record = $this->toRecord($model, $mirrorScenario); |
||
| 77 | |||
| 78 | // Validate |
||
| 79 | if (!$record->validate($attributes)) { |
||
| 80 | |||
| 81 | $model->addErrors($record->getErrors()); |
||
| 82 | |||
| 83 | $transaction->rollBack(); |
||
| 84 | |||
| 85 | return false; |
||
| 86 | |||
| 87 | } |
||
| 88 | |||
| 89 | // Insert record |
||
| 90 | if (!$record->save($attributes)) { |
||
|
|
|||
| 91 | |||
| 92 | // Transfer errors to model |
||
| 93 | $model->addErrors($record->getErrors()); |
||
| 94 | |||
| 95 | $transaction->rollBack(); |
||
| 96 | |||
| 97 | return false; |
||
| 98 | |||
| 99 | } |
||
| 100 | |||
| 101 | // Transfer record to model |
||
| 102 | if ($event->isNew) { |
||
| 103 | $model->id = $record->id; |
||
| 104 | $model->dateCreated = $record->dateCreated; |
||
| 105 | $model->uid = $record->uid; |
||
| 106 | } |
||
| 107 | $model->dateUpdated = $record->dateUpdated; |
||
| 108 | |||
| 109 | |||
| 110 | // The 'after' event |
||
| 111 | if (!$model->afterSave($event)) { |
||
| 112 | |||
| 113 | $transaction->rollBack(); |
||
| 114 | |||
| 115 | return false; |
||
| 116 | |||
| 117 | } |
||
| 118 | |||
| 119 | } catch (\Exception $e) { |
||
| 120 | |||
| 121 | $transaction->rollBack(); |
||
| 122 | |||
| 123 | throw $e; |
||
| 124 | |||
| 125 | } |
||
| 126 | |||
| 127 | $transaction->commit(); |
||
| 128 | |||
| 129 | return true; |
||
| 130 | |||
| 131 | } |
||
| 132 | |||
| 134 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: