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