Passed
Push — v1 ( d932b6...e18b22 )
by Andrew
13:20 queued 07:25
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
use yii\base\InvalidConfigException;
21
22
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
 * @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...
24
 * @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...
25
 * @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...
26
 */
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...
27
class AutocompleteVariableGenerator extends Generator
28
{
29
    // Public Static Methods
30
    // =========================================================================
31
32
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
33
     * @inheritDoc
34
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
35
    public static function getGeneratorName(): string
36
    {
37
        return 'AutocompleteVariable';
38
    }
39
40
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
41
     * @inheritDoc
42
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
43
    public static function generate()
44
    {
45
        if (self::shouldRegenerateFile()) {
46
            static::generateInternal();
47
        }
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 regenerate()
54
    {
55
        static::generateInternal();
56
    }
57
58
    // Private Static Methods
59
    // =========================================================================
60
61
    /**
62
     * Core function that generates the autocomplete class
63
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
64
    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...
65
    {
66
        $values = [];
67
        /* @noinspection PhpInternalEntityUsedInspection */
68
        $globals = Craft::$app->view->getTwig()->getGlobals();
69
        /* @var CraftVariable $craftVariable */
70
        if (isset($globals['craft'])) {
71
            $craftVariable = $globals['craft'];
72
            foreach ($craftVariable->getComponents() as $key => $value) {
73
                try {
74
                    $values[$key] = get_class($craftVariable->get($key));
75
                } catch (\Throwable $e) {
76
                    // That's okay
77
                }
78
            }
79
        }
80
81
        // Format the line output for each value
82
        foreach ($values as $key => $value) {
83
            $values[$key] = ' * @property \\' . $value . ' $' . $key;
84
        }
85
86
        // Save the template with variable substitution
87
        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...
88
            '{{ properties }}' => implode(PHP_EOL, $values),
89
        ]);
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...
90
    }
91
}
92