| Conditions | 29 |
| Paths | 90 |
| Total Lines | 65 |
| Code Lines | 53 |
| 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 | $service = $fieldType == 'Users' ? Craft::$app->userGroups : Craft::$app->categories; |
||
| 85 | $method = 'getGroupBy'; |
||
| 86 | break; |
||
| 87 | case 'folder': |
||
| 88 | case 'createFoldersInVolume': |
||
| 89 | case 'deleteFilesAndFoldersInVolume': |
||
| 90 | case 'saveAssetInVolume': |
||
| 91 | case 'viewVolume': |
||
| 92 | $service = Craft::$app->volumes; |
||
| 93 | $method = 'getVolumeBy'; |
||
| 94 | break; |
||
| 95 | case 'taggroup': |
||
| 96 | $service = Craft::$app->tags; |
||
| 97 | $method = 'getTagGroupBy'; |
||
| 98 | break; |
||
| 99 | case 'field': |
||
| 100 | $service = Craft::$app->fields; |
||
| 101 | $method = 'getFieldBy'; |
||
| 102 | break; |
||
| 103 | case 'editGlobalSet': |
||
| 104 | $service = Craft::$app->globals; |
||
| 105 | $method = 'getSetBy'; |
||
| 106 | break; |
||
| 107 | case 'utility': |
||
| 108 | return $source; |
||
| 109 | } |
||
| 110 | |||
| 111 | if (isset($service) && isset($method) && isset($sourceFrom)) { |
||
| 112 | $method = $method.ucfirst($indexFrom); |
||
| 113 | $sourceObject = $service->$method($sourceFrom); |
||
| 114 | } |
||
| 115 | |||
| 116 | if ($sourceObject && isset($sourceType)) { |
||
| 117 | return $sourceType.':'.$sourceObject->$indexTo; |
||
| 118 | } |
||
| 119 | |||
| 120 | Schematic::warning('No mapping found for source'.$source); |
||
| 121 | return $source; |
||
| 122 | } |
||
| 123 | } |
||
| 124 |