Conditions | 11 |
Paths | 39 |
Total Lines | 44 |
Code Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
34 | public function handleWidget() |
||
35 | { |
||
36 | $SQL_id = $this->owner->getRequest()->param('ID'); |
||
37 | if (!$SQL_id) { |
||
38 | return false; |
||
39 | } |
||
40 | /** @var SiteTree $widgetOwner */ |
||
41 | $widgetOwner = $this->owner->data(); |
||
42 | while ($widgetOwner->InheritSideBar && $widgetOwner->Parent()->exists()) { |
||
43 | $widgetOwner = $widgetOwner->Parent(); |
||
44 | } |
||
45 | |||
46 | // find WidgetArea relations |
||
47 | $widgetAreaRelations = array(); |
||
48 | $hasOnes = $widgetOwner->hasOne(); |
||
49 | |||
50 | if (!$hasOnes) { |
||
51 | return false; |
||
52 | } |
||
53 | |||
54 | foreach ($hasOnes as $hasOneName => $hasOneClass) { |
||
55 | if ($hasOneClass == WidgetArea::class || is_subclass_of($hasOneClass, WidgetArea::class)) { |
||
56 | $widgetAreaRelations[] = $hasOneName; |
||
57 | } |
||
58 | } |
||
59 | |||
60 | // find widget |
||
61 | $widget = null; |
||
62 | |||
63 | foreach ($widgetAreaRelations as $widgetAreaRelation) { |
||
64 | if ($widget) { |
||
65 | break; |
||
66 | } |
||
67 | |||
68 | $widget = $widgetOwner->$widgetAreaRelation()->Widgets() |
||
69 | ->filter('ID', $SQL_id) |
||
70 | ->First(); |
||
71 | } |
||
72 | |||
73 | if (!$widget) { |
||
74 | user_error('No widget found', E_USER_ERROR); |
||
75 | } |
||
76 | |||
77 | return $widget->getController(); |
||
78 | } |
||
80 |