Conditions | 15 |
Paths | 32 |
Total Lines | 72 |
Code Lines | 38 |
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 |
||
187 | public function getOwnerPage() |
||
188 | { |
||
189 | // You can't find the owner page of a area that hasn't been save yet |
||
190 | if (!$this->isInDB()) { |
||
191 | return null; |
||
192 | } |
||
193 | |||
194 | // Allow for repeated calls to read from cache |
||
195 | if (isset($this->cacheData['owner_page'])) { |
||
196 | return $this->cacheData['owner_page']; |
||
197 | } |
||
198 | |||
199 | if ($this->OwnerClassName) { |
||
200 | $class = $this->OwnerClassName; |
||
201 | $instance = Injector::inst()->get($class); |
||
202 | if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) { |
||
203 | return null; |
||
204 | } |
||
205 | $elementalAreaRelations = $instance->getElementalRelations(); |
||
206 | |||
207 | foreach ($elementalAreaRelations as $eaRelationship) { |
||
208 | $areaID = $eaRelationship . 'ID'; |
||
209 | |||
210 | $table = DataObject::getSchema()->tableForField($class, $areaID); |
||
211 | $page = DataObject::get_one($class, ["\"{$table}\".\"$areaID\" = ?" => $this->ID]); |
||
212 | |||
213 | if ($page) { |
||
214 | $this->setOwnerPageCached($page); |
||
215 | return $page; |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | |||
220 | foreach ($this->supportedPageTypes() as $class) { |
||
221 | $instance = Injector::inst()->get($class); |
||
222 | if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) { |
||
223 | return null; |
||
224 | } |
||
225 | |||
226 | $areaIDFilters = []; |
||
227 | foreach ($instance->getElementalRelations() as $eaRelationship) { |
||
228 | $areaIDFilters[$eaRelationship . 'ID'] = $this->ID; |
||
229 | } |
||
230 | |||
231 | try { |
||
232 | $page = Versioned::get_by_stage($class, Versioned::DRAFT)->filterAny($areaIDFilters)->first(); |
||
233 | } catch (\Exception $ex) { |
||
234 | // Usually this is catching cases where test stubs from other modules are trying to be loaded |
||
235 | // and failing in unit tests. |
||
236 | if (in_array(TestOnly::class, class_implements($class))) { |
||
237 | continue; |
||
238 | } |
||
239 | // Continue as normal... |
||
240 | throw $ex; |
||
241 | } |
||
242 | |||
243 | if ($page) { |
||
244 | if ($this->OwnerClassName !== $class) { |
||
245 | $this->OwnerClassName = $class; |
||
246 | |||
247 | // Avoid recursion: only write if it's already in the database |
||
248 | if ($this->isInDB()) { |
||
249 | $this->write(); |
||
250 | } |
||
251 | } |
||
252 | |||
253 | $this->cacheData['area_relation_name'] = $page; |
||
254 | return $page; |
||
255 | } |
||
256 | } |
||
257 | |||
258 | return null; |
||
259 | } |
||
301 |