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