Passed
Push — develop-v3 ( 9e872f...6bb9a5 )
by Andrew
15:35
created

Code::getContentGqlType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * Code Field plugin for Craft CMS
4
 *
5
 * Provides a Code Field that has a full-featured code editor with syntax highlighting & autocomplete
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2022 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
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...
10
11
namespace nystudio107\codefield\fields;
12
13
use Craft;
14
use craft\base\ElementInterface;
15
use craft\base\Field;
16
use craft\base\PreviewableFieldInterface;
17
use craft\helpers\Json;
18
use craft\validators\ArrayValidator;
19
use nystudio107\codefield\gql\types\generators\CodeDataGenerator;
20
use nystudio107\codefield\models\CodeData;
21
use nystudio107\codefield\validators\JsonValidator;
22
use yii\db\Schema;
23
24
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
25
 * @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...
26
 * @package   CodeField
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
27
 * @since     3.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...
28
 */
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...
29
class Code extends Field implements PreviewableFieldInterface
30
{
31
    // Public Properties
32
    // =========================================================================
33
34
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
35
     * @var string The theme to use for the Code Editor field.
36
     */
37
    public $theme = 'auto';
38
39
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
40
     * @var string The language to use for the Code Editor field.
41
     */
42
    public $language = 'javascript';
43
44
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @var bool Whether the Code Editor field display as a single line
46
     */
47
    public $singleLineEditor = false;
48
49
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
50
     * @var int The font size to use for the Code Editor field
51
     */
52
    public $fontSize = 14;
53
54
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
55
     * @var bool Whether line numbers should be displayed in the Code Editor field
56
     */
57
    public $lineNumbers = false;
58
59
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
60
     * @var bool Whether code folding should be used in the Code Editor field
61
     */
62
    public $codeFolding = false;
63
64
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
65
     * @var string The text that will be shown if the code field is empty.
66
     */
67
    public $placeholder = '';
68
69
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
70
     * @var bool Whether the language selector dropdown menu should be displayed.
71
     */
72
    public $showLanguageDropdown = true;
73
74
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
75
     * @var array The languages that should be listed in the language selector dropdown menu.
76
     */
77
    public $availableLanguages = [
78
        'css',
79
        'graphql',
80
        'html',
81
        'javascript',
82
        'json',
83
        'markdown',
84
        'mysql',
85
        'php',
86
        'shell',
87
        'twig',
88
        'typescript',
89
        'yaml',
90
    ];
91
92
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
93
     * @var string JSON blob of Monaco [EditorOptions](https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.IEditorOptions.html) that will override the default settings
94
     */
95
    public $monacoEditorOptions = '';
96
97
    // Static Methods
98
    // =========================================================================
99
100
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
101
     * @inheritdoc
102
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
103
    public static function displayName(): string
104
    {
105
        return Craft::t('codefield', 'Code');
106
    }
107
108
    // Public Methods
109
    // =========================================================================
110
111
    /**
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...
112
     * @inheritdoc
113
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
114
    public function normalizeValue($value, ElementInterface $element = null)
115
    {
116
        if ($value instanceof CodeData) {
117
            return $value;
118
        }
119
        // Default config
120
        $config = [
121
            'value' => '',
122
            'language' => $this->language,
123
        ];
124
        // Handle incoming values potentially being JSON or an array
125
        if (!empty($value)) {
126
            // Handle JSON-encoded values coming in
127
            if (\is_string($value)) {
128
                $jsonValue = Json::decodeIfJson($value);
129
                // If this is still a string (meaning it's not valid JSON), treat it as the value
130
                if (\is_string($jsonValue)) {
131
                    $config['value'] = $jsonValue;
132
                } else {
133
                    $value = $jsonValue;
134
                }
135
            }
136
            if (\is_array($value)) {
137
                $config = array_merge($config, array_filter($value));
138
            }
139
        }
140
        // Create and validate the model
141
        $codeData = new CodeData($config);
142
        if (!$codeData->validate()) {
143
            Craft::error(
144
                Craft::t('codefield', 'CodeData failed validation: ')
145
                . print_r($codeData->getErrors(), true),
0 ignored issues
show
Bug introduced by
Are you sure print_r($codeData->getErrors(), true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

145
                . /** @scrutinizer ignore-type */ print_r($codeData->getErrors(), true),
Loading history...
146
                __METHOD__
147
            );
148
        }
149
150
        return $codeData;
151
    }
152
153
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
154
     * @inheritdoc
155
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
156
    public function getSettingsHtml()
157
    {
158
        $monacoLanguages = require(__DIR__ . '/MonacoLanguages.php');
0 ignored issues
show
Coding Style introduced by
"require" is a statement not a function; no parentheses are required
Loading history...
Coding Style introduced by
File is being conditionally included; use "include" instead
Loading history...
159
        // Render the settings template
160
        return Craft::$app->getView()->renderTemplate(
161
            'codefield/_components/fields/Code_settings',
162
            [
163
                'field' => $this,
164
                'monacoLanguages' => $monacoLanguages,
165
            ]
166
        );
167
    }
168
169
    /**
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...
170
     * @inheritdoc
171
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
172
    public function getInputHtml($value, ElementInterface $element = null): string
173
    {
174
        // Get our id and namespace
175
        $id = Craft::$app->getView()->formatInputId($this->handle);
176
        $namespacedId = Craft::$app->getView()->namespaceInputId($id);
177
178
        // Extract just the languages that have been selected for display
179
        $displayLanguages = [];
180
        if ($this->showLanguageDropdown) {
181
            $monacoLanguages = require(__DIR__ . '/MonacoLanguages.php');
0 ignored issues
show
Coding Style introduced by
"require" is a statement not a function; no parentheses are required
Loading history...
Coding Style introduced by
File is being conditionally included; use "include" instead
Loading history...
182
            $decomposedLanguages = array_column($monacoLanguages, 'label', 'value');
183
            $displayLanguages = array_intersect_key($decomposedLanguages, array_flip($this->availableLanguages));
184
            $displayLanguages = array_map(function ($k, $v) {
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...
185
                return ['value' => $k, 'label' => $v];
186
            }, array_keys($displayLanguages), array_values($displayLanguages));
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...
187
        }
188
        $monacoOptionsOverride = Json::decodeIfJson($this->monacoEditorOptions);
189
        if ($monacoOptionsOverride === null || is_string($monacoOptionsOverride)) {
190
            $monacoOptionsOverride = [];
191
        }
192
        // Render the input template
193
        return Craft::$app->getView()->renderTemplate(
194
            'codefield/_components/fields/Code_input',
195
            [
196
                'name' => $this->handle,
197
                'value' => $value,
198
                'field' => $this,
199
                'orientation' => $this->getOrientation($element),
200
                'id' => $id,
201
                'namespacedId' => $namespacedId,
202
                'displayLanguages' => $displayLanguages,
203
                'monacoOptionsOverride' => $monacoOptionsOverride,
204
            ]
205
        );
206
    }
207
208
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
209
     * @inheritdoc
210
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
211
    public function getContentGqlType()
212
    {
213
        $typeArray = CodeDataGenerator::generateTypes($this);
214
215
        return [
216
            'name' => $this->handle,
217
            'description' => 'Code Editor field',
218
            'type' => array_shift($typeArray),
219
        ];
220
    }
221
222
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
223
     * @inheritdoc
224
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
225
    public function rules()
226
    {
227
        $rules = parent::rules();
228
        $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...
229
            ['theme', 'in', 'range' => ['auto', 'vs', 'vs-dark', 'hc-black']],
230
            ['theme', 'default', 'value' => 'auto'],
231
            ['language', 'string'],
232
            ['language', 'default', 'value' => 'javascript'],
233
            [['singleLineEditor', 'showLanguageDropdown', 'lineNumbers', 'codeFolding'], 'boolean'],
234
            ['placeholder', 'string'],
235
            ['placeholder', 'default', 'value' => ''],
236
            ['fontSize', 'integer'],
237
            ['fontSize', 'default', 'value' => 14],
238
            ['availableLanguages', ArrayValidator::class],
239
            ['monacoEditorOptions', JsonValidator::class],
240
        ]);
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...
241
        return $rules;
242
    }
243
244
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
245
     * @inheritdoc
246
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
247
    public function getContentColumnType(): string
248
    {
249
        return Schema::TYPE_TEXT;
250
    }
251
}
252