Conditions | 13 |
Paths | 2 |
Total Lines | 44 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 182 |
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 |
||
35 | /** |
||
36 | * Set Position Behavior |
||
37 | */ |
||
38 | public function setPositionBehavior(PositionBehavior $positionBehavior): void |
||
39 | { |
||
40 | $this->positionBehavior = $positionBehavior; |
||
41 | $this->addBehavior($this->positionBehavior); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Get Position Behavior |
||
46 | */ |
||
47 | public function getPositionBehavior(): PositionBehavior |
||
48 | { |
||
49 | return $this->positionBehavior; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Re-ordering an entity |
||
54 | * - Update position+1 done using afterSave event |
||
55 | * |
||
56 | * @return mixed |
||
57 | * @throws Exception |
||
58 | */ |
||
59 | public function reorder(?int $position = null, ?string $positionField = null) |
||
60 | { |
||
61 | $positionField ??= $this->getPositionBehavior()->getField(); |
||
62 | |||
63 | if ($this->fireEventCancel('beforeReorder') === false) { |
||
|
|||
64 | return false; |
||
65 | } |
||
66 | |||
67 | $this->assign([$positionField => $position], [$positionField]); |
||
68 | $saved = $this->save() && (!$this->hasSnapshotData() || $this->hasUpdated($positionField)); |
||
69 | |||
70 | if ($saved) { |
||
71 | $this->fireEvent('afterReorder'); |
||
72 | } |
||
73 | |||
74 | return $saved; |
||
75 | } |
||
76 | } |
||
77 |