Conditions | 6 |
Paths | 13 |
Total Lines | 67 |
Code Lines | 23 |
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 |
||
46 | public function delete(Model $model): bool |
||
47 | { |
||
48 | |||
49 | // a 'beforeSave' event |
||
50 | if(!$this->beforeDelete($model)) { |
||
51 | return false; |
||
52 | } |
||
53 | |||
54 | // The event to trigger |
||
55 | $event = new ModelEvent(); |
||
56 | |||
57 | // Db transaction |
||
58 | $transaction = RecordHelper::beginTransaction(); |
||
59 | |||
60 | try { |
||
61 | |||
62 | // The 'before' event |
||
63 | if (!$model->beforeDelete($event)) { |
||
64 | |||
65 | $transaction->rollBack(); |
||
66 | |||
67 | return false; |
||
68 | } |
||
69 | |||
70 | // Get record |
||
71 | $record = $this->getRecordById($model->id); |
||
72 | |||
73 | // Insert record |
||
74 | if (!$record->delete()) { |
||
|
|||
75 | |||
76 | // Transfer errors to model |
||
77 | $model->addErrors($record->getErrors()); |
||
78 | |||
79 | // Roll back db transaction |
||
80 | $transaction->rollBack(); |
||
81 | |||
82 | return false; |
||
83 | |||
84 | } |
||
85 | |||
86 | // The 'after' event |
||
87 | if (!$model->afterDelete($event)) { |
||
88 | |||
89 | // Roll back db transaction |
||
90 | $transaction->rollBack(); |
||
91 | |||
92 | return false; |
||
93 | |||
94 | } |
||
95 | |||
96 | } catch (\Exception $e) { |
||
97 | |||
98 | // Roll back all db actions (fail) |
||
99 | $transaction->rollback(); |
||
100 | |||
101 | throw $e; |
||
102 | |||
103 | } |
||
104 | |||
105 | $transaction->commit(); |
||
106 | |||
107 | // an 'afterDelete' event |
||
108 | $this->afterDelete($model); |
||
109 | |||
110 | return true; |
||
111 | |||
112 | } |
||
113 | |||
138 |
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: