| Conditions | 7 |
| Paths | 36 |
| Total Lines | 66 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 153 | public function getInputHtml(mixed $value, ?ElementInterface $element = null): string |
||
| 154 | { |
||
| 155 | // Register our asset bundle |
||
| 156 | try { |
||
| 157 | Craft::$app->getView()->registerAssetBundle(RecipeFieldAsset::class); |
||
| 158 | } catch (InvalidConfigException $invalidConfigException) { |
||
| 159 | Craft::error($invalidConfigException->getMessage(), __METHOD__); |
||
| 160 | } |
||
| 161 | |||
| 162 | // Get our id and namespace |
||
| 163 | $id = Html::id($this->handle); |
||
| 164 | $nameSpacedId = Craft::$app->getView()->namespaceInputId($id); |
||
| 165 | |||
| 166 | // Variables to pass down to our field JavaScript to let it namespace properly |
||
| 167 | $jsonVars = [ |
||
| 168 | 'id' => $id, |
||
| 169 | 'name' => $this->handle, |
||
| 170 | 'namespace' => $nameSpacedId, |
||
| 171 | 'prefix' => Craft::$app->getView()->namespaceInputId(''), |
||
| 172 | ]; |
||
| 173 | $jsonVars = Json::encode($jsonVars); |
||
| 174 | Craft::$app->getView()->registerJs(sprintf('$(\'#%s-field\').RecipeRecipe(', $nameSpacedId) . $jsonVars . ");"); |
||
| 175 | |||
| 176 | // Set asset elements |
||
| 177 | $elements = []; |
||
| 178 | if ($value->imageId) { |
||
| 179 | if (is_array($value->imageId)) { |
||
| 180 | $value->imageId = $value->imageId[0]; |
||
| 181 | } |
||
| 182 | |||
| 183 | $elements = [Craft::$app->getAssets()->getAssetById($value->imageId)]; |
||
| 184 | } |
||
| 185 | |||
| 186 | $videoElements = []; |
||
| 187 | if ($value->videoId) { |
||
| 188 | if (is_array($value->videoId)) { |
||
| 189 | $value->videoId = $value->videoId[0]; |
||
| 190 | } |
||
| 191 | |||
| 192 | $videoElements = [Craft::$app->getAssets()->getAssetById($value->videoId)]; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** @var Settings $settings */ |
||
| 196 | $settings = RecipePlugin::$plugin->getSettings(); |
||
| 197 | // Render the input template |
||
| 198 | try { |
||
| 199 | return Craft::$app->getView()->renderTemplate( |
||
| 200 | 'recipe/_components/fields/Recipe_input', |
||
| 201 | [ |
||
| 202 | 'name' => $this->handle, |
||
| 203 | 'value' => $value, |
||
| 204 | 'field' => $this, |
||
| 205 | 'id' => $id, |
||
| 206 | 'nameSpacedId' => $nameSpacedId, |
||
| 207 | 'prefix' => Craft::$app->getView()->namespaceInputId(''), |
||
| 208 | 'assetsSourceExists' => count(Craft::$app->getAssets()->findFolders()), |
||
| 209 | 'elements' => $elements, |
||
| 210 | 'videoElements' => $videoElements, |
||
| 211 | 'elementType' => Asset::class, |
||
| 212 | 'assetSources' => $this->assetSources, |
||
| 213 | 'hasApiCredentials' => $settings->hasApiCredentials(), |
||
| 214 | ] |
||
| 215 | ); |
||
| 216 | } catch (Throwable $throwable) { |
||
| 217 | Craft::error($throwable->getMessage(), __METHOD__); |
||
| 218 | return ''; |
||
| 219 | } |
||
| 241 |