Passed
Push — develop ( e8f94c...f7bbc0 )
by Andrew
04:36
created

Recipe::getInputHtml()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 61
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 41
c 3
b 0
f 0
nc 36
nop 2
dl 0
loc 61
rs 8.3306

How to fix   Long Method   

Long Method

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:

1
<?php
2
/**
3
 * Recipe plugin for Craft CMS 3.x
4
 *
5
 * A comprehensive recipe FieldType for Craft CMS that includes metric/imperial
6
 * conversion, portion calculation, and JSON-LD microdata support
7
 *
8
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\recipe\fields;
13
14
use nystudio107\recipe\assetbundles\recipefield\RecipeFieldAsset;
15
use nystudio107\recipe\models\Recipe as RecipeModel;
16
17
use Craft;
18
use craft\base\ElementInterface;
19
use craft\base\Field;
20
use craft\elements\Asset;
21
use craft\helpers\Html;
22
use craft\helpers\Json;
23
24
use nystudio107\recipe\Recipe as RecipePlugin;
25
use Twig\Error\LoaderError;
26
use Twig\Error\RuntimeError;
27
use Twig\Error\SyntaxError;
28
use yii\base\Exception;
29
use yii\base\InvalidConfigException;
30
use yii\db\Schema;
31
32
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
33
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
34
 * @package   Recipe
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
35
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
36
 */
0 ignored issues
show
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
37
class Recipe extends Field
38
{
39
    // Public Properties
40
    // =========================================================================
41
42
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
43
     * @var array
44
     */
45
    public $assetSources = [];
46
47
    // Static Methods
48
    // =========================================================================
49
50
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
51
     * @inheritdoc
52
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
53
    public static function displayName(): string
54
    {
55
        return Craft::t('recipe', 'Recipe');
56
    }
57
58
    // Public Methods
59
    // =========================================================================
60
61
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
62
     * @inheritdoc
63
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
64
    public function rules()
65
    {
66
        $rules = parent::rules();
67
        $rules = array_merge($rules, [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
68
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
69
70
        return $rules;
71
    }
72
73
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
74
     * @inheritdoc
75
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
76
    public function getContentColumnType(): string
77
    {
78
        return Schema::TYPE_TEXT;
79
    }
80
81
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
82
     * @inheritdoc
83
     * @since 1.2.1
0 ignored issues
show
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 6 spaces but found 1
Loading history...
84
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
85
    public function useFieldset(): bool
86
    {
87
        return true;
88
    }
89
90
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
91
     * @inheritdoc
92
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
93
    public function normalizeValue($value, ElementInterface $element = null)
94
    {
95
        if (is_string($value) && !empty($value)) {
96
            $value = Json::decode($value);
97
        }
98
        $model = new RecipeModel($value);
99
100
        return $model;
101
    }
102
103
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
104
     * @inheritdoc
105
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
106
    public function getSettingsHtml()
107
    {
108
        // Render the settings template
109
        return Craft::$app->getView()->renderTemplate(
110
            'recipe/_components/fields/Recipe_settings',
111
            [
112
                'field' => $this,
113
                'assetSources' => $this->getSourceOptions(),
114
            ]
115
        );
116
    }
117
118
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
119
     * @inheritdoc
120
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
121
    public function getInputHtml($value, ElementInterface $element = null): string
122
    {
123
        // Register our asset bundle
124
        try {
125
            Craft::$app->getView()->registerAssetBundle(RecipeFieldAsset::class);
126
        } catch (InvalidConfigException $e) {
127
            Craft::error($e->getMessage(), __METHOD__);
128
        }
129
130
        // Get our id and namespace
131
        $id = Craft::$app->getView()->formatInputId($this->handle);
132
        $nameSpacedId = Craft::$app->getView()->namespaceInputId($id);
133
134
        // Variables to pass down to our field JavaScript to let it namespace properly
135
        $jsonVars = [
136
            'id' => $id,
137
            'name' => $this->handle,
138
            'namespace' => $nameSpacedId,
139
            'prefix' => Craft::$app->getView()->namespaceInputId(''),
140
        ];
141
        $jsonVars = Json::encode($jsonVars);
142
        Craft::$app->getView()->registerJs("$('#{$nameSpacedId}-field').RecipeRecipe(".$jsonVars.");");
143
144
        // Set asset elements
145
        $elements = [];
146
        if ($value->imageId) {
147
            if (is_array($value->imageId)) {
148
                $value->imageId = $value->imageId[0];
149
            }
150
            $elements = [Craft::$app->getAssets()->getAssetById($value->imageId)];
151
        }
152
        $videoElements = [];
153
        if ($value->videoId) {
154
            if (is_array($value->videoId)) {
155
                $value->videoId = $value->videoId[0];
156
            }
157
            $videoElements = [Craft::$app->getAssets()->getAssetById($value->videoId)];
158
        }
159
160
        // Render the input template
161
        try {
162
            return Craft::$app->getView()->renderTemplate(
163
                'recipe/_components/fields/Recipe_input',
164
                [
165
                    'name' => $this->handle,
166
                    'value' => $value,
167
                    'field' => $this,
168
                    'id' => $id,
169
                    'nameSpacedId' => $nameSpacedId,
170
                    'prefix' => Craft::$app->getView()->namespaceInputId(''),
171
                    'assetsSourceExists' => count(Craft::$app->getAssets()->findFolders()),
172
                    'elements' => $elements,
173
                    'videoElements' => $videoElements,
174
                    'elementType' => Asset::class,
175
                    'assetSources' => $this->assetSources,
176
                    'hasApiCredentials' => RecipePlugin::$plugin->settings->hasApiCredentials(),
177
                ]
178
            );
179
        } catch (\Throwable $e) {
180
            Craft::error($e->getMessage(), __METHOD__);
181
            return '';
182
        }
183
    }
184
185
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
186
     * @inheritdoc
187
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
188
    public function getSourceOptions(): array
189
    {
190
        $sourceOptions = [];
191
192
        foreach (Asset::sources('settings') as $key => $volume) {
193
            if (!isset($volume['heading'])) {
194
                $sourceOptions[] = [
195
                    'label' => Html::encode($volume['label']),
196
                    'value' => $volume['key']
197
                ];
198
            }
199
        }
200
201
        return $sourceOptions;
202
    }
203
}
204