CraftApiAutocomplete   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
eloc 54
c 4
b 1
f 1
dl 0
loc 142
rs 10
wmc 19

4 Methods

Rating   Name   Duplication   Size   Complexity  
A overrideValues() 0 5 1
B generateCompleteItems() 0 40 11
A getElementRouteGlobals() 0 13 4
A init() 0 7 3
1
<?php
2
/**
3
 * Twigfield for Craft CMS
4
 *
5
 * Provides a twig editor field with Twig & Craft API 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\twigfield\autocompletes;
12
13
use Craft;
14
use craft\base\Element;
15
use nystudio107\twigfield\base\ObjectParserAutocomplete;
16
use nystudio107\twigfield\models\CompleteItem;
17
use nystudio107\twigfield\types\AutocompleteTypes;
18
use nystudio107\twigfield\types\CompleteItemKind;
19
20
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @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...
22
 * @package   twigfield
0 ignored issues
show
Coding Style introduced by
Package name "twigfield" is not valid; consider "Twigfield" instead
Loading history...
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
23
 * @since     1.0.12
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...
24
 */
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...
25
class CraftApiAutocomplete extends ObjectParserAutocomplete
26
{
27
    // Constants
28
    // =========================================================================
29
30
    const ELEMENT_ROUTE_EXCLUDES = [
31
        'matrixblock',
32
        'globalset'
33
    ];
34
35
    // Public Properties
36
    // =========================================================================
37
38
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
39
     * @var string The name of the autocomplete
40
     */
41
    public $name = 'CraftApiAutocomplete';
42
43
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
44
     * @var string The type of the autocomplete
45
     */
46
    public $type = AutocompleteTypes::TwigExpressionAutocomplete;
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties
50
     */
51
    public $hasSubProperties = true;
52
53
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
54
     * @var array A key-value array of the Twig global variables to parse. If left empty, it will
55
     * default to the current Twig context global variables
56
     */
57
    public $twigGlobals = [];
58
59
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
60
     * @var array A key-value array of the Element Route variables (the injected `entry`, etc.
61
     * variable). If left empty, it will default to the current Element Route variables
62
     */
63
    public $elementRouteGlobals = [];
64
65
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
66
     * @var array A key-value array of additional global variables to parse for completions
67
     */
68
    public $additionalGlobals = [];
69
70
    // Public Methods
71
    // =========================================================================
72
73
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
74
     * @inerhitDoc
75
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
76
    public function init(): void
77
    {
78
        if (empty($this->twigGlobals)) {
79
            $this->twigGlobals = Craft::$app->view->getTwig()->getGlobals();
80
        }
81
        if (empty($this->elementRouteGlobals)) {
82
            $this->elementRouteGlobals = $this->getElementRouteGlobals();
83
        }
84
    }
85
86
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
87
     * @inerhitDoc
88
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
89
    public function generateCompleteItems(): void
90
    {
91
        // Gather up all of the globals to parse
92
        $globals = array_merge(
93
            $this->twigGlobals,
94
            $this->elementRouteGlobals,
95
            $this->additionalGlobals,
96
            $this->overrideValues()
97
        );
98
        foreach ($globals as $key => $value) {
99
            if (!in_array($key, parent::EXCLUDED_PROPERTY_NAMES, true)) {
100
                $type = gettype($value);
101
                switch ($type) {
102
                    case 'object':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
103
                        $this->parseObject($key, $value, 0);
104
                        break;
105
106
                    case 'array':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
107
                    case 'boolean':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
108
                    case 'double':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
109
                    case 'integer':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
110
                    case 'string':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
111
                        $kind = CompleteItemKind::VariableKind;
112
                        $path = $key;
113
                        $normalizedKey = preg_replace("/[^A-Za-z]/", '', $key);
114
                        if (ctype_upper($normalizedKey)) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
115
                            $kind = CompleteItemKind::ConstantKind;
116
                        }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
117
                        // If this is an array, JSON-encode the keys. In the future, we could recursively parse the array
118
                        // To allow for nested values
119
                        if (is_array($value)) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
120
                            $value = json_encode(array_keys($value));
121
                        }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
122
                        CompleteItem::create()
123
                            ->detail((string)$value)
124
                            ->kind($kind)
125
                            ->label((string)$key)
126
                            ->insertText((string)$key)
127
                            ->add($this, $path);
128
                        break;
129
                }
130
            }
131
        }
132
    }
133
134
    // Protected Methods
135
    // =========================================================================
136
137
    /**
138
     * Add in the element types that could be injected as route variables
139
     *
140
     * @return array
141
     */
142
    protected function getElementRouteGlobals(): array
143
    {
144
        $routeVariables = [];
145
        $elementTypes = Craft::$app->elements->getAllElementTypes();
0 ignored issues
show
Bug introduced by
The method getAllElementTypes() 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

145
        /** @scrutinizer ignore-call */ 
146
        $elementTypes = Craft::$app->elements->getAllElementTypes();

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...
146
        foreach ($elementTypes as $elementType) {
147
            /* @var Element $elementType */
148
            $key = $elementType::refHandle();
149
            if (!empty($key) && !in_array($key, self::ELEMENT_ROUTE_EXCLUDES)) {
150
                $routeVariables[$key] = new $elementType();
151
            }
152
        }
153
154
        return $routeVariables;
155
    }
156
157
    /**
158
     * Override certain values that we always want hard-coded
159
     *
160
     * @return array
161
     */
162
    protected function overrideValues(): array
163
    {
164
        return [
165
            // Set the nonce to a blank string, as it changes on every request
166
            'nonce' => '',
167
        ];
168
    }
169
}
170