| Conditions | 10 |
| Paths | 22 |
| Total Lines | 93 |
| Code Lines | 37 |
| 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 | $isNew = (null === $model->id); |
||
| 59 | |||
| 60 | // a 'beforeSave' event |
||
| 61 | if(!$this->beforeSave($model, $isNew)) { |
||
| 62 | return false; |
||
| 63 | } |
||
| 64 | |||
| 65 | // Create event |
||
| 66 | $event = new ModelEvent([ |
||
| 67 | 'isNew' => $isNew |
||
| 68 | ]); |
||
| 69 | |||
| 70 | // Db transaction |
||
| 71 | $transaction = RecordHelper::beginTransaction(); |
||
| 72 | |||
| 73 | try { |
||
| 74 | |||
| 75 | // The 'before' event |
||
| 76 | if (!$model->beforeSave($event)) { |
||
| 77 | |||
| 78 | $transaction->rollBack(); |
||
| 79 | |||
| 80 | return false; |
||
| 81 | } |
||
| 82 | |||
| 83 | $record = $this->toRecord($model, $mirrorScenario); |
||
| 84 | |||
| 85 | // Validate |
||
| 86 | if (!$record->validate($attributes)) { |
||
| 87 | |||
| 88 | $model->addErrors($record->getErrors()); |
||
| 89 | |||
| 90 | $transaction->rollBack(); |
||
| 91 | |||
| 92 | return false; |
||
| 93 | |||
| 94 | } |
||
| 95 | |||
| 96 | // Insert record |
||
| 97 | if (!$record->save($attributes)) { |
||
|
|
|||
| 98 | |||
| 99 | // Transfer errors to model |
||
| 100 | $model->addErrors($record->getErrors()); |
||
| 101 | |||
| 102 | $transaction->rollBack(); |
||
| 103 | |||
| 104 | return false; |
||
| 105 | |||
| 106 | } |
||
| 107 | |||
| 108 | // Transfer record to model |
||
| 109 | if ($event->isNew) { |
||
| 110 | $model->id = $record->id; |
||
| 111 | $model->dateCreated = $record->dateCreated; |
||
| 112 | $model->uid = $record->uid; |
||
| 113 | } |
||
| 114 | $model->dateUpdated = $record->dateUpdated; |
||
| 115 | |||
| 116 | |||
| 117 | // The 'after' event |
||
| 118 | if (!$model->afterSave($event)) { |
||
| 119 | |||
| 120 | $transaction->rollBack(); |
||
| 121 | |||
| 122 | return false; |
||
| 123 | |||
| 124 | } |
||
| 125 | |||
| 126 | } catch (\Exception $e) { |
||
| 127 | |||
| 128 | $transaction->rollBack(); |
||
| 129 | |||
| 130 | throw $e; |
||
| 131 | |||
| 132 | } |
||
| 133 | |||
| 134 | $transaction->commit(); |
||
| 135 | |||
| 136 | // an 'afterSave' event |
||
| 137 | $this->afterSave($model, $isNew); |
||
| 138 | |||
| 139 | return true; |
||
| 140 | |||
| 141 | } |
||
| 142 | |||
| 169 |
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: