Passed
Push — develop ( 26e711...f1c6fb )
by Andrew
04:14
created

AutocompleteTwigExtensionGenerator   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 11
Bugs 0 Features 2
Metric Value
eloc 50
c 11
b 0
f 2
dl 0
loc 129
rs 10
wmc 19

6 Methods

Rating   Name   Duplication   Size   Complexity  
B generateInternal() 0 48 11
A generate() 0 4 1
A overrideValues() 0 9 1
A regenerate() 0 3 1
A getGeneratorName() 0 3 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
17
use Craft;
18
use craft\base\Element;
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   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...
23
 * @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...
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 AutocompleteTwigExtensionGenerator extends Generator
26
{
27
    // const
28
    // =========================================================================
29
30
    const ELEMENT_ROUTE_EXCLUDES = [
31
        'matrixblock',
32
        'globalset'
33
    ];
34
35
    // Public Static Methods
36
    // =========================================================================
37
38
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
39
     * @inheritDoc
40
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
41
    public static function getGeneratorName(): string
42
    {
43
        return 'AutocompleteTwigExtension';
44
    }
45
46
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
47
     * @inheritDoc
48
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
49
    public static function generate()
50
    {
51
        // We always regenerate, to be context-sensitive based on the last page that was loaded/rendered
52
        self::generateInternal();
53
    }
54
55
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
56
     * @inheritDoc
57
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
58
    public static function regenerate()
59
    {
60
        self::generateInternal();
61
    }
62
63
    // Private Static Methods
64
    // =========================================================================
65
66
    /**
67
     * Core function that generates the autocomplete class
68
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
69
    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...
70
    {
71
        $values = [];
72
        // Iterate through the globals in the Twig context
73
        /* @noinspection PhpInternalEntityUsedInspection */
74
        $globals = Craft::$app->view->getTwig()->getGlobals();
75
        foreach ($globals as $key => $value) {
76
            $type = gettype($value);
77
            switch ($type) {
78
                case 'object':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
79
                    $values[$key] = 'new \\' . get_class($value) . '()';
80
                    break;
81
82
                case 'boolean':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
83
                    $values[$key] = $value ? 'true' : 'false';
84
                    break;
85
86
                case 'integer':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
87
                case 'double':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
88
                    $values[$key] = $value;
89
                    break;
90
91
                case 'string':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
92
                    $values[$key] = "'" . addslashes($value) . "'";
93
                    break;
94
95
                case 'array':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
96
                    $values[$key] = '[]';
97
                    break;
98
99
                case 'NULL':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
100
                    $values[$key] = 'null';
101
                    break;
102
            }
103
        }
104
        // Mix in element route variables, and override values that should be used for autocompletion
105
        $values = array_merge(
106
            $values,
107
            static::elementRouteVariables(),
108
            static::overrideValues()
109
        );
110
        // Format the line output for each value
111
        foreach ($values as $key => $value) {
112
            $values[$key] = "            '" . $key . "' => " . $value . ",";
113
        }
114
        // Save the template with variable substitution
115
        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...
116
            '{{ globals }}' => implode(PHP_EOL, $values),
117
        ]);
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...
118
    }
119
120
    /**
121
     * Add in the element types that could be injected as route variables
122
     *
123
     * @return array
124
     */
125
    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...
126
    {
127
        $routeVariables = [];
128
        $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

128
        /** @scrutinizer ignore-call */ 
129
        $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...
129
        foreach ($elementTypes as $elementType) {
130
            /* @var Element $elementType */
131
            $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...
132
            if (!empty($key) && !in_array($key, static::ELEMENT_ROUTE_EXCLUDES)) {
133
                $routeVariables[$key] = 'new \\' . $elementType . '()';
134
            }
135
        }
136
137
        return $routeVariables;
138
    }
139
140
    /**
141
     * Override certain values that we always want hard-coded
142
     *
143
     * @return array
144
     */
145
    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...
146
    {
147
        return [
148
            // Swap in our variable in place of the `craft` variable
149
            'craft' => 'new \nystudio107\autocomplete\variables\AutocompleteVariable()',
150
            // Set the current user to a new user, so it is never `null`
151
            'currentUser' => 'new \craft\elements\User()',
152
            // Set the nonce to a blank string, as it changes on every request
153
            'nonce' => "''",
154
        ];
155
    }
156
}
157