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