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 |
||
45 | public function delete(Model $model): bool |
||
46 | { |
||
47 | |||
48 | // The event to trigger |
||
49 | $event = new ModelEvent(); |
||
50 | |||
51 | // Db transaction |
||
52 | $transaction = RecordHelper::beginTransaction(); |
||
53 | |||
54 | try { |
||
55 | |||
56 | // The 'before' event |
||
57 | if (!$model->beforeDelete($event)) { |
||
58 | |||
59 | $transaction->rollBack(); |
||
60 | |||
61 | return false; |
||
62 | } |
||
63 | |||
64 | // Get record |
||
65 | $record = $this->getRecordById($model->id); |
||
66 | |||
67 | // Insert record |
||
68 | if (!$record->delete()) { |
||
|
|||
69 | |||
70 | // Transfer errors to model |
||
71 | $model->addErrors($record->getErrors()); |
||
72 | |||
73 | // Roll back db transaction |
||
74 | $transaction->rollBack(); |
||
75 | |||
76 | return false; |
||
77 | |||
78 | } |
||
79 | |||
80 | // The 'after' event |
||
81 | if (!$model->afterDelete($event)) { |
||
82 | |||
83 | // Roll back db transaction |
||
84 | $transaction->rollBack(); |
||
85 | |||
86 | return false; |
||
87 | |||
88 | } |
||
89 | |||
90 | } catch (\Exception $e) { |
||
91 | |||
92 | // Roll back all db actions (fail) |
||
93 | $transaction->rollback(); |
||
94 | |||
95 | throw $e; |
||
96 | |||
97 | } |
||
98 | |||
99 | $transaction->commit(); |
||
100 | |||
101 | return true; |
||
102 | |||
103 | } |
||
104 | |||
106 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: