| Conditions | 14 |
| Paths | 16 |
| Total Lines | 49 |
| Code Lines | 27 |
| 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 |
||
| 137 | public function getOwnerPage() |
||
| 138 | { |
||
| 139 | if ($this->OwnerClassName) { |
||
| 140 | $class = $this->OwnerClassName; |
||
| 141 | $instance = Injector::inst()->get($class); |
||
| 142 | if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) { |
||
| 143 | return null; |
||
| 144 | } |
||
| 145 | $elementalAreaRelations = $instance->getElementalRelations(); |
||
| 146 | |||
| 147 | foreach ($elementalAreaRelations as $eaRelationship) { |
||
| 148 | $areaID = $eaRelationship . 'ID'; |
||
| 149 | |||
| 150 | $currentStage = Versioned::get_stage() ?: Versioned::DRAFT; |
||
| 151 | $page = Versioned::get_by_stage($class, $currentStage)->filter($areaID, $this->ID); |
||
| 152 | |||
| 153 | |||
| 154 | if ($page && $page->exists()) { |
||
| 155 | return $page->first(); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | foreach ($this->supportedPageTypes() as $class) { |
||
| 161 | $instance = Injector::inst()->get($class); |
||
| 162 | if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) { |
||
| 163 | return null; |
||
| 164 | } |
||
| 165 | $elementalAreaRelations = $instance->getElementalRelations(); |
||
| 166 | |||
| 167 | foreach ($elementalAreaRelations as $eaRelationship) { |
||
| 168 | $areaID = $eaRelationship . 'ID'; |
||
| 169 | $page = Versioned::get_by_stage($class, Versioned::DRAFT)->filter($areaID, $this->ID); |
||
| 170 | |||
| 171 | if ($page && $page->exists()) { |
||
| 172 | if ($this->OwnerClassName !== $class) { |
||
| 173 | $this->OwnerClassName = $class; |
||
| 174 | |||
| 175 | // Avoid recursion: only write if it's already in the database |
||
| 176 | if ($this->isInDB()) { |
||
| 177 | $this->write(); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | return $page->first(); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | return null; |
||
| 186 | } |
||
| 228 |