Conditions | 5 |
Paths | 12 |
Total Lines | 59 |
Code Lines | 20 |
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 |
||
56 | public function delete(Model $model): bool |
||
57 | { |
||
58 | |||
59 | // The event to trigger |
||
60 | $event = $this->createEvent(); |
||
61 | |||
62 | // Db transaction |
||
63 | $transaction = RecordHelper::beginTransaction(); |
||
64 | |||
65 | try { |
||
66 | |||
67 | // The 'before' event |
||
68 | if (!$model->beforeDelete($event)) { |
||
69 | |||
70 | $transaction->rollBack(); |
||
71 | |||
72 | return false; |
||
73 | } |
||
74 | |||
75 | // Get record |
||
76 | $record = $this->getRecordById($model->id); |
||
77 | |||
78 | // Insert record |
||
79 | if (!$record->delete()) { |
||
80 | |||
81 | // Transfer errors to model |
||
82 | $model->addErrors($record->getErrors()); |
||
83 | |||
84 | // Roll back db transaction |
||
85 | $transaction->rollBack(); |
||
86 | |||
87 | return false; |
||
88 | |||
89 | } |
||
90 | |||
91 | // The 'after' event |
||
92 | if (!$model->afterDelete($event)) { |
||
93 | |||
94 | // Roll back db transaction |
||
95 | $transaction->rollBack(); |
||
96 | |||
97 | return false; |
||
98 | |||
99 | } |
||
100 | |||
101 | } catch (\Exception $e) { |
||
102 | |||
103 | // Roll back all db actions (fail) |
||
104 | $transaction->rollback(); |
||
105 | |||
106 | throw $e; |
||
107 | |||
108 | } |
||
109 | |||
110 | $transaction->commit(); |
||
111 | |||
112 | return true; |
||
113 | |||
114 | } |
||
115 | |||
117 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.