| Conditions | 8 |
| Paths | 30 |
| Total Lines | 57 |
| Code Lines | 39 |
| 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 |
||
| 120 | public function getInputHtml($value, ElementInterface $element = null): string |
||
| 121 | { |
||
| 122 | // Register our asset bundle |
||
| 123 | try { |
||
| 124 | Craft::$app->getView()->registerAssetBundle(RecipeFieldAsset::class); |
||
| 125 | } catch (InvalidConfigException $e) { |
||
| 126 | Craft::error($e->getMessage(), __METHOD__); |
||
| 127 | } |
||
| 128 | |||
| 129 | // Get our id and namespace |
||
| 130 | $id = Craft::$app->getView()->formatInputId($this->handle); |
||
| 131 | $nameSpacedId = Craft::$app->getView()->namespaceInputId($id); |
||
| 132 | |||
| 133 | // Variables to pass down to our field JavaScript to let it namespace properly |
||
| 134 | $jsonVars = [ |
||
| 135 | 'id' => $id, |
||
| 136 | 'name' => $this->handle, |
||
| 137 | 'namespace' => $nameSpacedId, |
||
| 138 | 'prefix' => Craft::$app->getView()->namespaceInputId(''), |
||
| 139 | ]; |
||
| 140 | $jsonVars = Json::encode($jsonVars); |
||
| 141 | Craft::$app->getView()->registerJs("$('#{$nameSpacedId}-field').RecipeRecipe(".$jsonVars.");"); |
||
| 142 | |||
| 143 | // Set asset elements |
||
| 144 | $elements = []; |
||
| 145 | if ($value->imageId) { |
||
| 146 | if (is_array($value->imageId)) { |
||
| 147 | $value->imageId = $value->imageId[0]; |
||
| 148 | } |
||
| 149 | $elements = [Craft::$app->getAssets()->getAssetById($value->imageId)]; |
||
| 150 | } |
||
| 151 | |||
| 152 | // Render the input template |
||
| 153 | try { |
||
| 154 | return Craft::$app->getView()->renderTemplate( |
||
| 155 | 'recipe/_components/fields/Recipe_input', |
||
| 156 | [ |
||
| 157 | 'name' => $this->handle, |
||
| 158 | 'value' => $value, |
||
| 159 | 'field' => $this, |
||
| 160 | 'id' => $id, |
||
| 161 | 'nameSpacedId' => $nameSpacedId, |
||
| 162 | 'prefix' => Craft::$app->getView()->namespaceInputId(''), |
||
| 163 | 'assetsSourceExists' => count(Craft::$app->getAssets()->findFolders()), |
||
| 164 | 'elements' => $elements, |
||
| 165 | 'elementType' => Asset::class, |
||
| 166 | 'assetSources' => $this->assetSources, |
||
| 167 | ] |
||
| 168 | ); |
||
| 169 | } catch (LoaderError $e) { |
||
| 170 | Craft::error($e->getMessage(), __METHOD__); |
||
| 171 | } catch (RuntimeError $e) { |
||
| 172 | Craft::error($e->getMessage(), __METHOD__); |
||
| 173 | } catch (SyntaxError $e) { |
||
| 174 | Craft::error($e->getMessage(), __METHOD__); |
||
| 175 | } catch (Exception $e) { |
||
| 176 | Craft::error($e->getMessage(), __METHOD__); |
||
| 177 | } |
||
| 199 |