Passed
Push — v1 ( fe944c...52c974 )
by Andrew
14:42 queued 09:08
created

AutocompleteTwigExtensionGenerator   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 16
Bugs 1 Features 2
Metric Value
eloc 61
c 16
b 1
f 2
dl 0
loc 158
rs 10
wmc 22

7 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 4 2
A regenerate() 0 3 1
A getGeneratorName() 0 3 1
B generateInternal() 0 59 11
A globalVariables() 0 9 2
A overrideValues() 0 9 1
A elementRouteVariables() 0 13 4
1
<?php
2
/**
3
 * Autocomplete plugin for Craft CMS 3.x
4
 *
5
 * Provides Twig template IDE autocomplete of Craft CMS & plugin variables
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
 * @link      https://putyourlightson.com
9
 * @copyright Copyright (c) 2021 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
 * @copyright Copyright (c) 2021 PutYourLightsOn
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
11
 */
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...
12
13
namespace nystudio107\autocomplete\generators;
14
15
use nystudio107\autocomplete\base\Generator;
16
use nystudio107\autocomplete\events\DefineGeneratorValuesEvent;
17
18
use Craft;
19
use craft\base\Element;
20
use craft\web\twig\GlobalsExtension;
21
22
use yii\base\Event;
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   autocomplete
0 ignored issues
show
Coding Style introduced by
Package name "autocomplete" is not valid; consider "Autocomplete" instead
Loading history...
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
27
 * @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...
28
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @category tag in class comment
Loading history...
29
class AutocompleteTwigExtensionGenerator extends Generator
30
{
31
    // Constants
32
    // =========================================================================
33
34
    const ELEMENT_ROUTE_EXCLUDES = [
35
        'matrixblock',
36
        'globalset'
37
    ];
38
39
    // Public Static Methods
40
    // =========================================================================
41
42
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
43
     * @inheritDoc
44
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
45
    public static function getGeneratorName(): string
46
    {
47
        return 'AutocompleteTwigExtension';
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 generate()
54
    {
55
        if (self::shouldRegenerateFile()) {
56
            static::generateInternal();
57
        }
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 static function regenerate()
64
    {
65
        self::generateInternal();
66
    }
67
68
    // Private Static Methods
69
    // =========================================================================
70
71
    /**
72
     * Core function that generates the autocomplete class
73
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
74
    private static function generateInternal()
0 ignored issues
show
Coding Style introduced by
Private method name "AutocompleteTwigExtensionGenerator::generateInternal" must be prefixed with an underscore
Loading history...
75
    {
76
        $values = [];
77
        // Iterate through the globals in the Twig context
78
        /* @noinspection PhpInternalEntityUsedInspection */
79
        $globals = Craft::$app->view->getTwig()->getGlobals();
80
        foreach ($globals as $key => $value) {
81
            $type = gettype($value);
82
            switch ($type) {
83
                case 'object':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
84
                    $values[$key] = 'new \\' . get_class($value) . '()';
85
                    break;
86
87
                case 'boolean':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
88
                    $values[$key] = $value ? 'true' : 'false';
89
                    break;
90
91
                case 'integer':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
92
                case 'double':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
93
                    $values[$key] = $value;
94
                    break;
95
96
                case 'string':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
97
                    $values[$key] = "'" . addslashes($value) . "'";
98
                    break;
99
100
                case 'array':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
101
                    $values[$key] = '[]';
102
                    break;
103
104
                case 'NULL':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
105
                    $values[$key] = 'null';
106
                    break;
107
            }
108
        }
109
110
        // Mix in element route variables, and override values that should be used for autocompletion
111
        $values = array_merge(
112
            $values,
113
            static::elementRouteVariables(),
114
            static::globalVariables(),
115
            static::overrideValues()
116
        );
117
118
        // Allow plugins to modify the values
119
        $event = new DefineGeneratorValuesEvent([
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...
120
            'values' => $values,
121
        ]);
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...
122
        Event::trigger(self::class, self::EVENT_BEFORE_GENERATE, $event);
123
        $values = $event->values;
124
125
        // Format the line output for each value
126
        foreach ($values as $key => $value) {
127
            $values[$key] = "            '" . $key . "' => " . $value . ",";
128
        }
129
130
        // Save the template with variable substitution
131
        self::saveTemplate([
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...
132
            '{{ globals }}' => implode(PHP_EOL, $values),
133
        ]);
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...
134
    }
135
136
    /**
137
     * Add in the element types that could be injected as route variables
138
     *
139
     * @return array
140
     */
141
    private static function elementRouteVariables(): array
0 ignored issues
show
Coding Style introduced by
Private method name "AutocompleteTwigExtensionGenerator::elementRouteVariables" must be prefixed with an underscore
Loading history...
142
    {
143
        $routeVariables = [];
144
        $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

144
        /** @scrutinizer ignore-call */ 
145
        $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...
145
        foreach ($elementTypes as $elementType) {
146
            /* @var Element $elementType */
147
            $key = $elementType::refHandle();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $key is correct as $elementType::refHandle() targeting craft\base\Element::refHandle() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
148
            if (!empty($key) && !in_array($key, static::ELEMENT_ROUTE_EXCLUDES, true)) {
149
                $routeVariables[$key] = 'new \\' . $elementType . '()';
150
            }
151
        }
152
153
        return $routeVariables;
154
    }
155
156
    /**
157
     * Add in the global variables manually, because Craft conditionally loads the GlobalsExtension as of
158
     * Craft CMS 3.7.8 only for frontend routes
159
     *
160
     * @return array
161
     */
162
    private static function globalVariables(): array
0 ignored issues
show
Coding Style introduced by
Private method name "AutocompleteTwigExtensionGenerator::globalVariables" must be prefixed with an underscore
Loading history...
163
    {
164
        $globalVariables = [];
165
        $globalsExtension = new GlobalsExtension();
166
        foreach ($globalsExtension->getGlobals() as $key => $value) {
167
            $globalVariables[$key] = 'new \\' . get_class($value) . '()';
168
        }
169
170
        return $globalVariables;
171
    }
172
173
    /**
174
     * Override certain values that we always want hard-coded
175
     *
176
     * @return array
177
     */
178
    private static function overrideValues(): array
0 ignored issues
show
Coding Style introduced by
Private method name "AutocompleteTwigExtensionGenerator::overrideValues" must be prefixed with an underscore
Loading history...
179
    {
180
        return [
181
            // Swap in our variable in place of the `craft` variable
182
            'craft' => 'new \nystudio107\autocomplete\variables\AutocompleteVariable()',
183
            // Set the current user to a new user, so it is never `null`
184
            'currentUser' => 'new \craft\elements\User()',
185
            // Set the nonce to a blank string, as it changes on every request
186
            'nonce' => "''",
187
        ];
188
    }
189
}
190