Passed
Push — develop ( 35f566...26e711 )
by Andrew
04:22
created

AutocompleteTwigExtensionGenerator::setCachedValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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
19
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
20
 * @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...
21
 * @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...
22
 * @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...
23
 */
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...
24
class AutocompleteTwigExtensionGenerator extends Generator
25
{
26
    // Public Static Methods
27
    // =========================================================================
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @inheritDoc
31
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
32
    public static function getGeneratorName(): string
33
    {
34
        return 'AutocompleteTwigExtension';
35
    }
36
37
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
38
     * @inheritDoc
39
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
40
    public static function generate()
41
    {
42
        // We always regenerate, to be context-sensitive based on the last page that was loaded/rendered
43
        self::generateInternal();
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 regenerate()
50
    {
51
        self::generateInternal();
52
    }
53
54
    // Private Static Methods
55
    // =========================================================================
56
57
    /**
58
     * Core function that generates the autocomplete class
59
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
60
    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...
61
    {
62
        $values = [];
63
        // Iterate through the globals in the Twig context
64
        /* @noinspection PhpInternalEntityUsedInspection */
65
        $globals = Craft::$app->view->getTwig()->getGlobals();
66
        foreach ($globals as $key => $value) {
67
            $type = gettype($value);
68
            switch ($type) {
69
                case 'object':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
70
                    $values[$key] = 'new \\' . get_class($value) . '()';
71
                    break;
72
73
                case 'boolean':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
74
                    $values[$key] = $value ? 'true' : 'false';
75
                    break;
76
77
                case 'integer':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
78
                case 'double':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
79
                    $values[$key] = $value;
80
                    break;
81
82
                case 'string':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
83
                    $values[$key] = "'" . addslashes($value) . "'";
84
                    break;
85
86
                case 'array':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
87
                    $values[$key] = '[]';
88
                    break;
89
90
                case 'NULL':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
91
                    $values[$key] = 'null';
92
                    break;
93
            }
94
        }
95
        // Override values that should be used for autocompletion
96
        $values = array_merge(
97
            $values,
98
            static::overrideValues()
99
        );
100
        // Format the line output for each value
101
        foreach ($values as $key => $value) {
102
            $values[$key] = "            '" . $key . "' => " . $value . ",";
103
        }
104
        // Save the template with variable substitution
105
        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...
106
            '{{ globals }}' => implode(PHP_EOL, $values),
107
        ]);
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...
108
    }
109
110
    /**
111
     * Override certain values that we always want hard-coded
112
     *
113
     * @return array
114
     */
115
    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...
116
    {
117
        return [
118
            // Swap in our variable in place of the `craft` variable
119
            'craft' => 'new \nystudio107\autocomplete\variables\AutocompleteVariable()',
120
            // Set the current user to a new user, so it is never `null`
121
            'currentUser' => 'new \craft\elements\User()',
122
            // Set the nonce to a blank string, as it changes on every request
123
            'nonce' => "''",
124
        ];
125
    }
126
}
127