Conditions | 16 |
Paths | 37 |
Total Lines | 52 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
56 | public function getSource($fieldType, $source, $indexFrom, $indexTo) |
||
57 | { |
||
58 | if ($source == 'singles' || $source == '*') { |
||
59 | return $source; |
||
60 | } |
||
61 | |||
62 | /** @var BaseElementModel $sourceObject */ |
||
63 | $sourceObject = null; |
||
64 | |||
65 | if (strpos($source, ':') > -1) { |
||
66 | list($sourceType, $sourceFrom) = explode(':', $source); |
||
67 | switch ($sourceType) { |
||
68 | case 'section': |
||
69 | $service = Craft::app()->sections; |
||
70 | $method = 'getSectionBy'; |
||
71 | break; |
||
72 | case 'group': |
||
73 | $service = $fieldType == 'Users' ? Craft::app()->userGroups : Craft::app()->categories; |
||
74 | $method = 'getGroupBy'; |
||
75 | break; |
||
76 | case 'folder': |
||
77 | $service = Craft::app()->schematic_assetSources; |
||
78 | $method = 'getSourceBy'; |
||
79 | break; |
||
80 | case 'taggroup': |
||
81 | $service = Craft::app()->tags; |
||
82 | $method = 'getTagGroupBy'; |
||
83 | break; |
||
84 | case 'field': |
||
85 | $service = Craft::app()->fields; |
||
86 | $method = 'getFieldBy'; |
||
87 | break; |
||
88 | } |
||
89 | } elseif ($source !== 'singles') { |
||
90 | //Backwards compatibility |
||
91 | $sourceType = 'section'; |
||
92 | $sourceFrom = $source; |
||
93 | $service = Craft::app()->sections; |
||
94 | $method = 'getSectionBy'; |
||
95 | } |
||
96 | |||
97 | if (isset($service) && isset($method) && isset($sourceFrom)) { |
||
98 | $method = $method.$indexFrom; |
||
99 | $sourceObject = $service->$method($sourceFrom); |
||
100 | } |
||
101 | |||
102 | if ($sourceObject && isset($sourceType)) { |
||
103 | return $sourceType.':'.$sourceObject->$indexTo; |
||
104 | } |
||
105 | |||
106 | return ''; |
||
107 | } |
||
108 | } |
||
109 |