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