Passed
Push — develop ( 62364d...29a54d )
by Andrew
06:16 queued 01:53
created

Recipe   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 62
c 6
b 0
f 0
dl 0
loc 161
rs 10
wmc 19

8 Methods

Rating   Name   Duplication   Size   Complexity  
A displayName() 0 3 1
A rules() 0 7 1
A getContentColumnType() 0 3 1
A getSourceOptions() 0 14 3
A getSettingsHtml() 0 8 1
A useFieldset() 0 3 1
A normalizeValue() 0 8 3
B getInputHtml() 0 57 8
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 Twig\Error\LoaderError;
25
use Twig\Error\RuntimeError;
26
use Twig\Error\SyntaxError;
27
use yii\base\Exception;
28
use yii\base\InvalidConfigException;
29
use yii\db\Schema;
30
31
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
32
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
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
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
33
 * @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...
34
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
35
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
36
class Recipe extends Field
37
{
38
    // Public Properties
39
    // =========================================================================
40
41
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
42
     * @var array
43
     */
44
    public $assetSources = [];
45
46
    // Static Methods
47
    // =========================================================================
48
49
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
50
     * @inheritdoc
51
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
52
    public static function displayName(): string
53
    {
54
        return Craft::t('recipe', 'Recipe');
55
    }
56
57
    // Public Methods
58
    // =========================================================================
59
60
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
61
     * @inheritdoc
62
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
63
    public function rules()
64
    {
65
        $rules = parent::rules();
66
        $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...
67
        ]);
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...
68
69
        return $rules;
70
    }
71
72
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
73
     * @inheritdoc
74
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
75
    public function getContentColumnType(): string
76
    {
77
        return Schema::TYPE_TEXT;
78
    }
79
80
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
81
     * @inheritdoc
82
     * @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...
83
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
84
    public function useFieldset(): bool
85
    {
86
        return true;
87
    }
88
89
    /**
0 ignored issues
show
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...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
90
     * @inheritdoc
91
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
92
    public function normalizeValue($value, ElementInterface $element = null)
93
    {
94
        if (is_string($value) && !empty($value)) {
95
            $value = Json::decode($value);
96
        }
97
        $model = new RecipeModel($value);
98
99
        return $model;
100
    }
101
102
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
103
     * @inheritdoc
104
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
105
    public function getSettingsHtml()
106
    {
107
        // Render the settings template
108
        return Craft::$app->getView()->renderTemplate(
109
            'recipe/_components/fields/Recipe_settings',
110
            [
111
                'field' => $this,
112
                'assetSources' => $this->getSourceOptions(),
113
            ]
114
        );
115
    }
116
117
    /**
0 ignored issues
show
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...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
118
     * @inheritdoc
119
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
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__);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
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
        }
178
    }
179
180
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
181
     * @inheritdoc
182
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
183
    public function getSourceOptions(): array
184
    {
185
        $sourceOptions = [];
186
187
        foreach (Asset::sources('settings') as $key => $volume) {
188
            if (!isset($volume['heading'])) {
189
                $sourceOptions[] = [
190
                    'label' => Html::encode($volume['label']),
191
                    'value' => $volume['key']
192
                ];
193
            }
194
        }
195
196
        return $sourceOptions;
197
    }
198
}
199