Passed
Push — v1 ( d932b6...e18b22 )
by Andrew
13:20 queued 07:25
created

Autocomplete::regenerateAutocompleteClasses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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;
14
15
use nystudio107\autocomplete\base\Generator;
16
use nystudio107\autocomplete\console\controllers\AutocompleteController;
17
use nystudio107\autocomplete\generators\AutocompleteTwigExtensionGenerator;
18
use nystudio107\autocomplete\generators\AutocompleteVariableGenerator;
19
20
use Craft;
21
use craft\console\Application as CraftConsoleApp;
22
use craft\events\RegisterComponentTypesEvent;
23
use craft\services\Plugins;
24
use craft\web\Application as CraftWebApp;
25
26
use yii\base\Application as YiiApp;
27
use yii\base\BootstrapInterface;
28
use yii\base\Event;
29
use yii\base\Module;
30
31
/**
32
 * Class Autocomplete
33
 *
34
 * @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...
35
 * @package   Autocomplete
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
36
 * @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...
37
 *
38
 * @property-read string[] $allAutocompleteGenerators
39
 */
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...
40
class Autocomplete extends Module implements BootstrapInterface
41
{
42
    // Constants
43
    // =========================================================================
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46
     * @event RegisterComponentTypesEvent The event that is triggered when registering
47
     *        Autocomplete Generator types
48
     *
49
     * Autocomplete Generator types must implement [[GeneratorInterface]]. [[Generator]]
50
     * provides a base implementation.
51
     *
52
     * ```php
53
     * use nystudio107\autocomplete\Autocomplete;
54
     * use craft\events\RegisterComponentTypesEvent;
55
     * use yii\base\Event;
56
     *
57
     * Event::on(Autocomplete::class,
58
     *     Autocomplete::EVENT_REGISTER_AUTOCOMPLETE_GENERATORS,
59
     *     function(RegisterComponentTypesEvent $event) {
60
     *         $event->types[] = MyAutocompleteGenerator::class;
61
     *     }
62
     * );
63
     * ```
64
     */
65
    const EVENT_REGISTER_AUTOCOMPLETE_GENERATORS = 'registerAutocompleteGenerators';
66
67
    const DEFAULT_AUTOCOMPLETE_GENERATORS = [
68
        AutocompleteVariableGenerator::class,
69
        AutocompleteTwigExtensionGenerator::class,
70
    ];
71
72
    // Private Properties
73
    // =========================================================================
74
75
    private $allAutocompleteGenerators;
0 ignored issues
show
Coding Style introduced by
Private member variable "allAutocompleteGenerators" must be prefixed with an underscore
Loading history...
76
77
    // Public Methods
78
    // =========================================================================
79
80
    /**
81
     * Bootstraps the extension
82
     *
83
     * @param YiiApp $app
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
84
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
85
    public function bootstrap($app)
86
    {
87
        // Set the currently requested instance of this module class,
88
        // so we can later access it with `Autocomplete::getInstance()`
89
        static::setInstance($this);
90
91
        // Make sure it's Craft
92
        if (!($app instanceof CraftWebApp || $app instanceof CraftConsoleApp)) {
93
            return;
94
        }
95
        // Make sure we're in devMode
96
        if (!Craft::$app->config->general->devMode) {
97
            return;
98
        }
99
100
        // Register our event handlers
101
        $this->registerEventHandlers();
102
103
        // Add our console controller
104
        if (Craft::$app->request->isConsoleRequest) {
105
            Craft::$app->controllerMap['autocomplete'] = AutocompleteController::class;
106
        }
107
    }
108
109
    /**
110
     * Registers our event handlers
111
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
112
    public function registerEventHandlers()
113
    {
114
        Event::on(Plugins::class,Plugins::EVENT_AFTER_INSTALL_PLUGIN, [$this, 'regenerateAutocompleteClasses']);
115
        Event::on(Plugins::class,Plugins::EVENT_AFTER_UNINSTALL_PLUGIN, [$this, 'deleteAutocompleteClasses']);
116
        Event::on(Plugins::class,Plugins::EVENT_AFTER_LOAD_PLUGINS, [$this, 'generateAutocompleteClasses']);
117
        Craft::info('Event Handlers installed',__METHOD__);
118
    }
119
120
    /**
121
     * Call each of the autocomplete generator classes to tell them to generate their classes if they don't exist already
122
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
123
    public function generateAutocompleteClasses()
124
    {
125
        $autocompleteGenerators = $this->getAllAutocompleteGenerators();
126
        foreach($autocompleteGenerators as $generatorClass) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
127
            /* @var Generator $generatorClass */
128
            $generatorClass::generate();
129
        }
130
        Craft::info('Autocomplete classes generated',__METHOD__);
131
    }
132
133
    /**
134
     * Call each of the autocomplete generator classes to tell them to regenerate their classes from scratch
135
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
136
    public function regenerateAutocompleteClasses()
137
    {
138
        $autocompleteGenerators = $this->getAllAutocompleteGenerators();
139
        foreach($autocompleteGenerators as $generatorClass) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
140
            /* @var Generator $generatorClass */
141
            $generatorClass::regenerate();
142
        }
143
        Craft::info('Autocomplete classes regenerated',__METHOD__);
144
    }
145
146
    /**
147
     * Call each of the autocomplete generator classes to tell them to delete their classes
148
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
149
    public function deleteAutocompleteClasses()
150
    {
151
        $autocompleteGenerators = $this->getAllAutocompleteGenerators();
152
        foreach($autocompleteGenerators as $generatorClass) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
153
            /* @var Generator $generatorClass */
154
            $generatorClass::delete();
155
        }
156
        Craft::info('Autocomplete classes deleted',__METHOD__);
157
    }
158
159
    // Protected Methods
160
    // =========================================================================
161
162
    /**
163
     * Returns all available autocomplete generator classes.
164
     *
165
     * @return string[] The available autocomplete generator classes
166
     */
167
    public function getAllAutocompleteGenerators(): array
168
    {
169
        if ($this->allAutocompleteGenerators) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->allAutocompleteGenerators of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
170
            return $this->allAutocompleteGenerators;
171
        }
172
173
        $event = new RegisterComponentTypesEvent([
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...
174
            'types' => self::DEFAULT_AUTOCOMPLETE_GENERATORS
175
        ]);
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...
176
        $this->trigger(self::EVENT_REGISTER_AUTOCOMPLETE_GENERATORS, $event);
177
        $this->allAutocompleteGenerators = $event->types;
0 ignored issues
show
Bug introduced by
The property allAutocompleteGenerators is declared read-only in nystudio107\autocomplete\Autocomplete.
Loading history...
178
179
        return $this->allAutocompleteGenerators;
180
    }
181
}
182