| Conditions | 13 |
| Paths | 11 |
| Total Lines | 54 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 68 | public function registerAssetFiles($view) |
||
| 69 | { |
||
| 70 | parent::registerAssetFiles($view); |
||
| 71 | |||
| 72 | $data = Reasons::getInstance()->reasons->getData(); |
||
| 73 | |||
| 74 | // Check if there's a revisionId in the GET params |
||
| 75 | // We have to do this to account for Craft's new draft/revision system, and the new DraftEditor JS component |
||
| 76 | // I'm just gonna add all element types to this, even if it only applies to Entries right now |
||
| 77 | // If I'm lucky, this'll turn out to be future proof |
||
| 78 | $revisionId = (int)Craft::$app->getRequest()->getParam('revisionId'); |
||
| 79 | if ($revisionId) { |
||
| 80 | $sourceElementId = (int)Element::find()->revisionId($revisionId)->scalar(); |
||
| 81 | if ($sourceElementId && $element = Craft::$app->getElements()->getElementById($sourceElementId)) { |
||
| 82 | $elementType = \get_class($element); |
||
| 83 | $renderContext = null; |
||
| 84 | if ($elementType === Entry::class) { |
||
| 85 | /** @var Entry $element */ |
||
| 86 | $renderContext = "entryType:{$element->typeId}"; |
||
| 87 | } else if ($elementType === Category::class) { |
||
| 88 | /** @var Category $element */ |
||
| 89 | $renderContext = "categoryGroup:{$element->groupId}"; |
||
| 90 | } else if ($elementType === Tag::class) { |
||
| 91 | /** @var Tag $element */ |
||
| 92 | $renderContext = "tagGroup:{$element->groupId}"; |
||
| 93 | } else if ($elementType === GlobalSet::class) { |
||
| 94 | /** @var GlobalSet $element */ |
||
| 95 | $renderContext = "globalSet:{$element->id}"; |
||
| 96 | } else if ($elementType === User::class) { |
||
| 97 | $renderContext = 'users'; |
||
| 98 | } else if ($elementType === Asset::class) { |
||
| 99 | /** @var Asset $element */ |
||
| 100 | $renderContext = "assetSource:{$element->volumeId}"; |
||
| 101 | } |
||
| 102 | $data['renderContext'] = $renderContext; |
||
| 103 | } |
||
| 104 | } else { |
||
| 105 | // Sadly, the new Asset edit view doesn't have `<input type="hidden" name="volumeId"/>` input – so we'll need to work around that, too |
||
| 106 | $segments = Craft::$app->getRequest()->getSegments(); |
||
| 107 | if (\count($segments) === 3 && $segments[0] === 'assets') { |
||
| 108 | $volumeHandle = $segments[1]; |
||
| 109 | /** @var Volume $volume */ |
||
| 110 | $volume = Craft::$app->getVolumes()->getVolumeByHandle($volumeHandle); |
||
| 111 | if ($volume !== null) { |
||
| 112 | $data['renderContext'] = "assetSource:{$volume->id}"; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | $json = Json::encode($data, JSON_UNESCAPED_UNICODE); |
||
| 118 | $js = <<<JS |
||
| 119 | Craft.ReasonsPlugin.init({$json}); |
||
| 120 | JS; |
||
| 121 | $view->registerJs($js, View::POS_END); |
||
| 122 | } |
||
| 125 |