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