| Conditions | 17 | 
| Paths | 351 | 
| Total Lines | 82 | 
| Code Lines | 53 | 
| 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 | ||
| 61 | public function onAfterWrite() | ||
| 62 |     { | ||
| 63 |         if (Security::database_is_ready()  && $this->owner->HasPageRows()) { | ||
| 64 |             // debug::Log('-------------------------'); | ||
| 65 | $currentPageRows = []; | ||
| 66 |             if ($this->owner->PageRows()->count() > 0) { | ||
| 67 | $sortOrder = 1; | ||
| 68 |                 foreach ($this->owner->PageRows() as $pageRow) { | ||
| 69 | $currentPageRows[$sortOrder] = $pageRow; | ||
| 70 | $sortOrder++; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | $rowClassNames = $this->owner->DefaultPageRows(); | ||
| 74 | $sortOrder = 1; | ||
| 75 |             foreach ($rowClassNames as $className) { | ||
| 76 | $childClassName = null; | ||
| 77 |                 if (is_array($className)) { | ||
| 78 | $childClassName = $className['Child']; | ||
| 79 | // debug::log($childClassName); | ||
| 80 | $className = $className['Parent']; | ||
| 81 | } | ||
| 82 |                 if (isset($currentPageRows[$sortOrder])) { | ||
| 83 | $row = $currentPageRows[$sortOrder]; | ||
| 84 |                     if ($row->ClassName === $className) { | ||
| 85 | //all OK! | ||
| 86 |                     } else { | ||
| 87 | // $this->owner->removePageRowFromThisPage($row); | ||
| 88 | //we do not delete the row as it may be used somewhere else ... | ||
| 89 | // $this->owner->deletePageRowFromMe($row); | ||
| 90 | } | ||
| 91 |                 } else { | ||
| 92 | $row = $className::create(); | ||
| 93 | $row->Title = 'Title '.$row->singular_name().' #'.$className::get()->count().' for '.$this->owner->MenuTitle; | ||
| 94 | $row->write(); | ||
| 95 | $this->owner->PageRows()->add($row, ['SortOrder' => $sortOrder]); | ||
| 96 | $currentPageRows[$sortOrder] = $row; | ||
| 97 | } | ||
| 98 |                 DB::query(' | ||
| 99 | UPDATE "Page_PageRows" | ||
| 100 | SET "SortOrder" = '.$sortOrder.' | ||
| 101 | WHERE | ||
| 102 | "PageID" ='.$this->owner->ID.' AND | ||
| 103 | "PageRowID" = '.$row->ID.' | ||
| 104 | LIMIT 1; | ||
| 105 | '); | ||
| 106 |                 if ($childClassName) { | ||
| 107 | $childClassMethod = $row->ChildClassMethodName(); | ||
| 108 |                     if ($childClassMethod) { | ||
| 109 | $child = $row->$childClassMethod(); | ||
| 110 |                         if ($child && $child->exists()) { | ||
| 111 |                         } else { | ||
| 112 | $childClassMethodFieldName = $childClassMethod.'ID'; | ||
| 113 | $child = $childClassName::create(); | ||
| 114 | $child->Title = 'New '.$child->singular_name().' for '.$row->getTitle(); | ||
| 115 | $child->write(); | ||
| 116 | // debug::log($childClassMethodFieldName); | ||
| 117 | // debug::log($child->ID); | ||
| 118 | // debug::log($child->Title); | ||
| 119 | $row->$childClassMethodFieldName = $child->ID; | ||
| 120 | $row->write(); | ||
| 121 | } | ||
| 122 |                     } else { | ||
| 123 |                         user_error('no childclass method set in '.$row->ClassName); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | $sortOrder++; | ||
| 127 | } | ||
| 128 | $sortOrder = 1; | ||
| 129 |             foreach ($this->owner->PageRows() as $pageRow) { | ||
| 130 | $sortOrder++; | ||
| 131 | $delete = false; | ||
| 132 |                 if (!isset($rowClassNames[$sortOrder])) { | ||
| 133 | $delete = true; | ||
| 134 |                 } elseif ($rowClassNames[$sortOrder] !== $pageRow->ClassName) { | ||
| 135 | $delete = true; | ||
| 136 | } | ||
| 137 |                 if ($delete) { | ||
| 138 | // $this->owner->removePageRowFromThisPage($pageRow); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 249 | 
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.