| Conditions | 30 |
| Paths | 98 |
| Total Lines | 67 |
| Code Lines | 54 |
| 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 |
||
| 58 | public function getSource($fieldType, $source, $indexFrom, $indexTo) |
||
| 59 | { |
||
| 60 | if (strpos($source, ':') === false) { |
||
| 61 | return $source; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** @var Model $sourceObject */ |
||
| 65 | $sourceObject = null; |
||
| 66 | |||
| 67 | list($sourceType, $sourceFrom) = explode(':', $source); |
||
| 68 | switch ($sourceType) { |
||
| 69 | case 'section': |
||
| 70 | case 'createEntries': |
||
| 71 | case 'editPeerEntries': |
||
| 72 | case 'deleteEntries': |
||
| 73 | case 'deletePeerEntries': |
||
| 74 | case 'deletePeerEntryDrafts': |
||
| 75 | case 'editEntries': |
||
| 76 | case 'editPeerEntryDrafts': |
||
| 77 | case 'publishEntries': |
||
| 78 | case 'publishPeerEntries': |
||
| 79 | case 'publishPeerEntryDrafts': |
||
| 80 | $service = Craft::$app->sections; |
||
| 81 | $method = 'getSectionBy'; |
||
| 82 | break; |
||
| 83 | case 'group': |
||
| 84 | case 'editCategories': |
||
| 85 | $service = $fieldType == 'Users' ? Craft::$app->userGroups : Craft::$app->categories; |
||
| 86 | $method = 'getGroupBy'; |
||
| 87 | break; |
||
| 88 | case 'folder': |
||
| 89 | case 'createFoldersInVolume': |
||
| 90 | case 'deleteFilesAndFoldersInVolume': |
||
| 91 | case 'saveAssetInVolume': |
||
| 92 | case 'viewVolume': |
||
| 93 | $service = Craft::$app->volumes; |
||
| 94 | $method = 'getVolumeBy'; |
||
| 95 | break; |
||
| 96 | case 'taggroup': |
||
| 97 | $service = Craft::$app->tags; |
||
| 98 | $method = 'getTagGroupBy'; |
||
| 99 | break; |
||
| 100 | case 'field': |
||
| 101 | $service = Craft::$app->fields; |
||
| 102 | $method = 'getFieldBy'; |
||
| 103 | break; |
||
| 104 | case 'editGlobalSet': |
||
| 105 | $service = Craft::$app->globals; |
||
| 106 | $method = 'getSetBy'; |
||
| 107 | break; |
||
| 108 | case 'utility': |
||
| 109 | return $source; |
||
| 110 | } |
||
| 111 | |||
| 112 | if (isset($service) && isset($method) && isset($sourceFrom)) { |
||
| 113 | $method = $method.ucfirst($indexFrom); |
||
| 114 | $sourceObject = $service->$method($sourceFrom); |
||
| 115 | } |
||
| 116 | |||
| 117 | if ($sourceObject && isset($sourceType)) { |
||
| 118 | return $sourceType.':'.$sourceObject->$indexTo; |
||
| 119 | } |
||
| 120 | |||
| 121 | Schematic::warning('No mapping found for source '.$source); |
||
| 122 | |||
| 123 | return $source; |
||
| 124 | } |
||
| 125 | } |
||
| 126 |