Passed
Push — develop ( d932b6...c1d6dd )
by Andrew
04:10
created

AutocompleteVariableGenerator::generateInternal()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 29
rs 9.2222
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
                $componentObject = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $componentObject is dead and can be removed.
Loading history...
74
                try {
75
                    $componentObject = $craftVariable->get($key);
76
                } catch (InvalidConfigException $e) {
77
                    // That's okay
78
                }
79
                if ($componentObject) {
80
                    $values[$key] = get_class($componentObject);
81
                }
82
            }
83
        }
84
85
        // Format the line output for each value
86
        foreach ($values as $key => $value) {
87
            $values[$key] = ' * @property \\' . $value . ' $' . $key;
88
        }
89
90
        // Save the template with variable substitution
91
        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...
92
            '{{ properties }}' => implode(PHP_EOL, $values),
93
        ]);
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...
94
    }
95
}
96