Issues (446)

src/fields/Recipe.php (50 issues)

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
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\recipe\fields;
13
14
use Craft;
15
use craft\base\ElementInterface;
0 ignored issues
show
The type craft\base\ElementInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use craft\base\Field;
17
use craft\base\PreviewableFieldInterface;
18
use craft\elements\Asset;
0 ignored issues
show
The type craft\elements\Asset was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use craft\helpers\Html;
20
use craft\helpers\Json;
21
use nystudio107\recipe\assetbundles\recipefield\RecipeFieldAsset;
22
use nystudio107\recipe\models\Recipe as RecipeModel;
23
use nystudio107\recipe\models\Settings;
24
use nystudio107\recipe\Recipe as RecipePlugin;
25
use Throwable;
26
use yii\base\InvalidConfigException;
27
use yii\db\Schema;
28
29
/**
0 ignored issues
show
Missing short description in doc comment
Loading history...
30
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
31
 * @package   Recipe
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
32
 * @since     1.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
33
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
34
class Recipe extends Field implements PreviewableFieldInterface
35
{
36
    // Public Properties
37
    // =========================================================================
38
39
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
40
     * @var array
41
     */
42
    public array $assetSources = [];
43
44
    // Static Methods
45
    // =========================================================================
46
47
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
48
     * @inheritdoc
49
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
50
    public static function dbType(): array|string|null
51
    {
52
        return Schema::TYPE_TEXT;
53
    }
54
55
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
56
     * @inheritdoc
57
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
58
    public static function displayName(): string
59
    {
60
        return Craft::t('recipe', 'Recipe');
61
    }
62
63
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
64
     * @inheritdoc
65
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
66
    public static function icon(): string
67
    {
68
        return 'hat-chef';
69
    }
70
71
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
72
     * @inheritdoc
73
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
74
    public static function phpType(): string
75
    {
76
        return sprintf('\\%s', RecipeModel::class);
77
    }
78
79
    // Public Methods
80
    // =========================================================================
81
82
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
83
     * @inheritdoc
84
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
85
    public function rules(): array
86
    {
87
        $rules = parent::rules();
88
89
        return array_merge($rules, [
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
90
        ]);
0 ignored issues
show
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...
91
    }
92
93
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
94
     * @inheritdoc
95
     * @since 1.2.1
0 ignored issues
show
Tag value for @since tag indented incorrectly; expected 6 spaces but found 1
Loading history...
96
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
97
    public function useFieldset(): bool
98
    {
99
        return true;
100
    }
101
102
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Parameter $element should have a doc-comment as per coding-style.
Loading history...
103
     * @inheritdoc
104
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
105
    public function normalizeValue(mixed $value, ?ElementInterface $element = null): RecipeModel
106
    {
107
        $config = [];
108
        // If we already have a model, just return it
109
        if ($value instanceof RecipeModel) {
110
            return $value;
111
        }
112
        // If we have a non-empty string, try to JSON-decode it
113
        if (is_string($value)) {
114
            $value = Json::decodeIfJson($value);
115
        }
116
        // If we have an array, use that for our config data
117
        if (is_array($value)) {
118
            $config = $value;
119
        }
120
        // Ensure we save our asset ids as integers, not arrays
121
        if (isset($config['imageId'])) {
122
            if (is_array($config['imageId'])) {
123
                $config['imageId'] = $config['imageId'][0];
124
            }
125
        }
126
        if (isset($config['videoId'])) {
127
            if (is_array($config['videoId'])) {
128
                $config['videoId'] = $config['videoId'][0];
129
            }
130
        }
131
132
        return new RecipeModel($config);
133
    }
134
135
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
136
     * @inheritdoc
137
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
138
    public function getSettingsHtml(): ?string
139
    {
140
        // Render the settings template
141
        return Craft::$app->getView()->renderTemplate(
142
            'recipe/_components/fields/Recipe_settings',
143
            [
144
                'field' => $this,
145
                'assetSources' => $this->getSourceOptions(),
146
            ]
147
        );
148
    }
149
150
    /**
0 ignored issues
show
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Parameter $element should have a doc-comment as per coding-style.
Loading history...
Missing short description in doc comment
Loading history...
151
     * @inheritdoc
152
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
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 */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
The close comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
196
        $settings = RecipePlugin::$plugin->getSettings();
0 ignored issues
show
The method getSettings() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

196
        /** @scrutinizer ignore-call */ 
197
        $settings = RecipePlugin::$plugin->getSettings();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
        }
220
    }
221
222
    /**
223
     * Get the asset sources
224
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
225
    public function getSourceOptions(): array
226
    {
227
        $sourceOptions = [];
228
229
        foreach (Asset::sources('settings') as $volume) {
230
            if (!isset($volume['heading'])) {
231
                $sourceOptions[] = [
232
                    'label' => Html::encode($volume['label']),
233
                    'value' => $volume['key'],
234
                ];
235
            }
236
        }
237
238
        return $sourceOptions;
239
    }
240
}
241