Passed
Push — develop ( e34a78...4b00a7 )
by Andrew
04:04
created

AutocompleteVariableGenerator::regenerate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 6
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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
use craft\web\twig\variables\CraftVariable;
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 AutocompleteVariableGenerator extends Generator
26
{
27
    // Public Static Methods
28
    // =========================================================================
29
30
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
     * @inheritDoc
32
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
33
    public static function getGeneratorName(): string
34
    {
35
        return 'AutocompleteVariable';
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 generate()
42
    {
43
        if (self::shouldRegenerateFile()) {
44
            static::generateInternal();
45
        }
46
    }
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @inheritDoc
50
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
51
    public static function regenerate()
52
    {
53
        static::generateInternal();
54
    }
55
56
    // Private Static Methods
57
    // =========================================================================
58
59
    /**
60
     * Core function that generates the autocomplete class
61
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
62
    private static function generateInternal()
0 ignored issues
show
Coding Style introduced by
Private method name "AutocompleteVariableGenerator::generateInternal" must be prefixed with an underscore
Loading history...
63
    {
64
        $values = [];
65
        /* @noinspection PhpInternalEntityUsedInspection */
66
        $globals = Craft::$app->view->getTwig()->getGlobals();
67
        /* @var CraftVariable $craftVariable */
68
        if (isset($globals['craft'])) {
69
            $craftVariable = $globals['craft'];
70
            foreach ($craftVariable->getComponents() as $key => $value) {
71
                $type = gettype($value);
72
                switch ($type) {
73
                    case 'object':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
74
                        $className = get_class($value);
75
                        $values[$key] = $className;
76
                        break;
77
78
                    case 'array':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
79
                        if (isset($value['class'])) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
80
                            $values[$key] = $value['class'];
81
                        }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
82
                        break;
83
84
                    case 'string':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
85
                        $values[$key] = $value;
86
                        break;
87
                }
88
            }
89
        }
90
91
        // Format the line output for each value
92
        foreach ($values as $key => $value) {
93
            $values[$key] = ' * @property \\' . $value . ' $' . $key;
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
            '{{ properties }}' => 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