Passed
Push — develop ( 6c6638...e34a78 )
by Andrew
03:52
created

overrideValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 10
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
        static::regenerate();
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
        $values = [];
52
        // Route variables are not merged in until the template is rendered, so do it here manually
53
        /* @noinspection PhpInternalEntityUsedInspection */
54
        $globals = array_merge(
55
            Craft::$app->view->getTwig()->getGlobals(),
56
            Craft::$app->controller->actionParams['variables'] ?? []
57
        );
58
        foreach ($globals as $key => $value) {
59
            $type = gettype($value);
60
            switch ($type) {
61
                case 'object':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
62
                    $values[$key] = 'new \\' . get_class($value) . '()';
63
                    break;
64
65
                case 'boolean':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
66
                    $values[$key] = $value ? 'true' : 'false';
67
                    break;
68
69
                case 'integer':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
70
                case 'double':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
71
                    $values[$key] = $value;
72
                    break;
73
74
                case 'string':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
75
                    $values[$key] = "'" . addslashes($value) . "'";
76
                    break;
77
78
                case 'array':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
79
                    $values[$key] = '[]';
80
                    break;
81
82
                case 'NULL':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
83
                    $values[$key] = 'null';
84
                    break;
85
            }
86
        }
87
88
        // Override values that should be used for autocompletion
89
        static::overrideValues($values);
90
91
        // Format the line output for each value
92
        foreach ($values as $key => $value) {
93
            $values[$key] = "            '" . $key . "' => " . $value . ",";
94
        }
95
96
        // Save the template with variable substitution
97
        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...
98
            '{{ globals }}' => implode(PHP_EOL, $values),
99
        ]);
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...
100
    }
101
102
    // Public Static Methods
103
    // =========================================================================
104
105
    /**
106
     * Override certain values that we always want hard-coded
107
     *
108
     * @param array $values
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
109
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
110
    private static function overrideValues(array &$values)
0 ignored issues
show
Coding Style introduced by
Private method name "AutocompleteTwigExtensionGenerator::overrideValues" must be prefixed with an underscore
Loading history...
111
    {
112
        // Swap in our variable in place of the `craft` variable
113
        $values['craft'] = 'new \nystudio107\autocomplete\variables\AutocompleteVariable()';
114
115
        // Set the current user to a new user, so it is never `null`
116
        $values['currentUser'] = 'new \craft\elements\User()';
117
118
        // Set the nonce to a blank string, as it changes on every request
119
        $values['nonce'] = "''";
120
    }
121
}
122