Conditions | 7 |
Paths | 14 |
Total Lines | 66 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 1 |
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 |
||
65 | public function reorder($elementToBeAfterID = 0) |
||
66 | { |
||
67 | $element = $this->element; |
||
68 | $parentId = $element->ParentID; |
||
|
|||
69 | $currentPosition = (int) $element->Sort; |
||
70 | $sortAfterPosition = 0; |
||
71 | |||
72 | if ($elementToBeAfterID) { |
||
73 | /** @var BaseElement $afterBlock */ |
||
74 | $afterElement = BaseElement::get()->byID($elementToBeAfterID); |
||
75 | |||
76 | if (!$afterElement) { |
||
77 | throw new InvalidArgumentException(sprintf( |
||
78 | '%s#%s not found', |
||
79 | BaseElement::class, |
||
80 | $elementToBeAfterID |
||
81 | )); |
||
82 | } |
||
83 | |||
84 | // Must be weak comparison as sometimes integers are returned from the DB as strings |
||
85 | if ($afterElement->ParentID != $parentId) { |
||
86 | throw new InvalidArgumentException( |
||
87 | 'Trying to sort element to be placed after an element from a different elemental area' |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | $sortAfterPosition = (int) $afterElement->Sort; |
||
92 | } |
||
93 | |||
94 | // We are updating records with SQL queries to avoid the ORM triggering the creation of new versions |
||
95 | // for each element that is affected by this reordering. |
||
96 | $baseTableName = Convert::raw2sql(DataObject::getSchema()->tableName(BaseElement::class)); |
||
97 | |||
98 | // Update both the draft and live versions of the records |
||
99 | $tableNames = [$baseTableName]; |
||
100 | if (BaseElement::has_extension(Versioned::class)) { |
||
101 | /** @var BaseElement&Versioned $element */ |
||
102 | $tableNames[] = $element->stageTable($baseTableName, Versioned::LIVE); |
||
103 | } |
||
104 | |||
105 | foreach ($tableNames as $tableName) { |
||
106 | $tableName = sprintf('"%s"', $tableName); |
||
107 | |||
108 | if ($sortAfterPosition < $currentPosition) { |
||
109 | $operator = '+'; |
||
110 | $filter = "$tableName.\"Sort\" > $sortAfterPosition AND $tableName.\"Sort\" < $currentPosition"; |
||
111 | $newBlockPosition = $sortAfterPosition + 1; |
||
112 | } else { |
||
113 | $operator = '-'; |
||
114 | $filter = "$tableName.\"Sort\" <= $sortAfterPosition AND $tableName.\"Sort\" > $currentPosition"; |
||
115 | $newBlockPosition = $sortAfterPosition; |
||
116 | } |
||
117 | |||
118 | $query = SQLUpdate::create() |
||
119 | ->setTable("$tableName") |
||
120 | ->assignSQL('"Sort"', "$tableName.\"Sort\" $operator 1") |
||
121 | ->addWhere([$filter, "$tableName.\"ParentID\"" => $parentId]); |
||
122 | |||
123 | $query->execute(); |
||
124 | } |
||
125 | |||
126 | // Now use the ORM to write a new version of the record that we are directly reordering |
||
127 | $element->Sort = $newBlockPosition; |
||
128 | $element->write(); |
||
129 | |||
130 | return $element; |
||
131 | } |
||
133 |