| Conditions | 15 |
| Paths | 21 |
| Total Lines | 66 |
| Code Lines | 36 |
| 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 |
||
| 182 | public function getOwnerPage() |
||
| 183 | { |
||
| 184 | // Allow for repeated calls to read from cache |
||
| 185 | if (isset($this->cacheData['owner_page'])) { |
||
| 186 | return $this->cacheData['owner_page']; |
||
| 187 | } |
||
| 188 | |||
| 189 | if ($this->OwnerClassName) { |
||
| 190 | $class = $this->OwnerClassName; |
||
| 191 | $instance = Injector::inst()->get($class); |
||
| 192 | if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) { |
||
| 193 | return null; |
||
| 194 | } |
||
| 195 | $elementalAreaRelations = $instance->getElementalRelations(); |
||
| 196 | |||
| 197 | foreach ($elementalAreaRelations as $eaRelationship) { |
||
| 198 | $areaID = $eaRelationship . 'ID'; |
||
| 199 | |||
| 200 | $currentStage = Versioned::get_stage() ?: Versioned::DRAFT; |
||
| 201 | $page = Versioned::get_by_stage($class, $currentStage)->filter($areaID, $this->ID)->first(); |
||
| 202 | |||
| 203 | if ($page) { |
||
| 204 | $this->setOwnerPageCached($page); |
||
| 205 | return $page; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | foreach ($this->supportedPageTypes() as $class) { |
||
| 211 | $instance = Injector::inst()->get($class); |
||
| 212 | if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) { |
||
| 213 | return null; |
||
| 214 | } |
||
| 215 | $elementalAreaRelations = $instance->getElementalRelations(); |
||
| 216 | |||
| 217 | foreach ($elementalAreaRelations as $eaRelationship) { |
||
| 218 | $areaID = $eaRelationship . 'ID'; |
||
| 219 | try { |
||
| 220 | $page = Versioned::get_by_stage($class, Versioned::DRAFT)->filter($areaID, $this->ID)->first(); |
||
| 221 | } catch (\Exception $ex) { |
||
| 222 | // Usually this is catching cases where test stubs from other modules are trying to be loaded |
||
| 223 | // and failing in unit tests. |
||
| 224 | if (in_array(TestOnly::class, class_implements($class))) { |
||
| 225 | continue; |
||
| 226 | } |
||
| 227 | // Continue as normal... |
||
| 228 | throw $ex; |
||
| 229 | } |
||
| 230 | |||
| 231 | if ($page) { |
||
| 232 | if ($this->OwnerClassName !== $class) { |
||
| 233 | $this->OwnerClassName = $class; |
||
| 234 | |||
| 235 | // Avoid recursion: only write if it's already in the database |
||
| 236 | if ($this->isInDB()) { |
||
| 237 | $this->write(); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | $this->cacheData['area_relation_name'] = $page; |
||
| 242 | return $page; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | return null; |
||
| 248 | } |
||
| 290 |