Autocomplete   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 7
Bugs 1 Features 0
Metric Value
eloc 41
c 7
b 1
f 0
dl 0
loc 158
rs 10
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A registerEventHandlers() 0 7 1
A bootstrap() 0 21 5
A deleteAutocompleteClasses() 0 8 2
A generateAutocompleteClasses() 0 9 3
A regenerateAutocompleteClasses() 0 8 2
A getAllAutocompleteGenerators() 0 13 2
1
<?php
2
/**
3
 * Autocomplete module for Craft CMS
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) 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) 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 Craft;
16
use craft\console\Application as CraftConsoleApp;
17
use craft\events\RegisterComponentTypesEvent;
18
use craft\services\Globals;
19
use craft\services\Plugins;
20
use craft\web\Application as CraftWebApp;
21
use nystudio107\autocomplete\base\Generator;
22
use nystudio107\autocomplete\console\controllers\AutocompleteController;
23
use nystudio107\autocomplete\generators\AutocompleteTwigExtensionGenerator;
24
use nystudio107\autocomplete\generators\AutocompleteVariableGenerator;
25
use yii\base\Application as YiiApp;
26
use yii\base\BootstrapInterface;
27
use yii\base\Event;
28
use yii\base\Module;
29
30
/**
31
 * Class Autocomplete
32
 *
33
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
34
 * @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...
35
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
36
 *
37
 * @property-read string[] $allAutocompleteGenerators
38
 */
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...
39
class Autocomplete extends Module implements BootstrapInterface
40
{
41
    // Constants
42
    // =========================================================================
43
44
    public const ID = 'craft-autocomplete';
45
46
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
47
     * @event RegisterComponentTypesEvent The event that is triggered when registering
48
     *        Autocomplete Generator types
49
     *
50
     * Autocomplete Generator types must implement [[GeneratorInterface]]. [[Generator]]
51
     * provides a base implementation.
52
     *
53
     * ```php
54
     * use nystudio107\autocomplete\Autocomplete;
55
     * use craft\events\RegisterComponentTypesEvent;
56
     * use yii\base\Event;
57
     *
58
     * Event::on(Autocomplete::class,
59
     *     Autocomplete::EVENT_REGISTER_AUTOCOMPLETE_GENERATORS,
60
     *     function(RegisterComponentTypesEvent $event) {
61
     *         $event->types[] = MyAutocompleteGenerator::class;
62
     *     }
63
     * );
64
     * ```
65
     */
66
    public const EVENT_REGISTER_AUTOCOMPLETE_GENERATORS = 'registerAutocompleteGenerators';
67
68
    public const DEFAULT_AUTOCOMPLETE_GENERATORS = [
69
        AutocompleteVariableGenerator::class,
70
        AutocompleteTwigExtensionGenerator::class,
71
    ];
72
73
    // Private Properties
74
    // =========================================================================
75
76
    private $allAutocompleteGenerators;
0 ignored issues
show
Coding Style introduced by
Private member variable "allAutocompleteGenerators" must be prefixed with an underscore
Loading history...
77
78
    // Public Methods
79
    // =========================================================================
80
81
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $id should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $parent should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
82
     * @inerhitdoc
83
     */
84
    public function __construct($id = self::ID, $parent = null, $config = [])
85
    {
86
        /**
87
         * Explicitly set the $id parameter, as earlier versions of Yii2 look for a
88
         * default parameter, and depend on $id being explicitly set:
89
         * https://github.com/yiisoft/yii2/blob/f3d1534125c9c3dfe8fa65c28a4be5baa822e721/framework/di/Container.php#L436-L448
90
         */
91
        parent::__construct($id, $parent, $config);
92
    }
93
94
    /**
95
     * Bootstraps the extension
96
     *
97
     * @param YiiApp $app
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
98
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
99
    public function bootstrap($app)
100
    {
101
        // Set the currently requested instance of this module class,
102
        // so we can later access it with `Autocomplete::getInstance()`
103
        static::setInstance($this);
104
105
        // Make sure it's Craft
106
        if (!($app instanceof CraftWebApp || $app instanceof CraftConsoleApp)) {
107
            return;
108
        }
109
        // Make sure we're in devMode
110
        if (!Craft::$app->config->general->devMode) {
111
            return;
112
        }
113
114
        // Register our event handlers
115
        $this->registerEventHandlers();
116
117
        // Add our console controller
118
        if (Craft::$app->request->isConsoleRequest) {
119
            Craft::$app->controllerMap['autocomplete'] = AutocompleteController::class;
120
        }
121
    }
122
123
    /**
124
     * Registers our event handlers
125
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
126
    public function registerEventHandlers()
127
    {
128
        Event::on(Plugins::class, Plugins::EVENT_AFTER_INSTALL_PLUGIN, [$this, 'regenerateAutocompleteClasses']);
129
        Event::on(Plugins::class, Plugins::EVENT_AFTER_UNINSTALL_PLUGIN, [$this, 'deleteAutocompleteClasses']);
130
        Event::on(Globals::class, Globals::EVENT_AFTER_SAVE_GLOBAL_SET, [$this, 'deleteAutocompleteClasses']);
131
        Event::on(CraftWebApp::class, CraftWebApp::EVENT_INIT, [$this, 'generateAutocompleteClasses']);
132
        Craft::info('Event Handlers installed', __METHOD__);
133
    }
134
135
    /**
136
     * Call each of the autocomplete generator classes to tell them to generate their classes if they don't exist already
137
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
138
    public function generateAutocompleteClasses()
139
    {
140
        if (Craft::$app->getIsInstalled()) {
141
            $autocompleteGenerators = $this->getAllAutocompleteGenerators();
142
            foreach ($autocompleteGenerators as $generatorClass) {
143
                /* @var Generator $generatorClass */
144
                $generatorClass::generate();
145
            }
146
            Craft::info('Autocomplete classes generated', __METHOD__);
147
        }
148
    }
149
150
    /**
151
     * Call each of the autocomplete generator classes to tell them to regenerate their classes from scratch
152
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
153
    public function regenerateAutocompleteClasses()
154
    {
155
        $autocompleteGenerators = $this->getAllAutocompleteGenerators();
156
        foreach ($autocompleteGenerators as $generatorClass) {
157
            /* @var Generator $generatorClass */
158
            $generatorClass::regenerate();
159
        }
160
        Craft::info('Autocomplete classes regenerated', __METHOD__);
161
    }
162
163
    /**
164
     * Call each of the autocomplete generator classes to tell them to delete their classes
165
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
166
    public function deleteAutocompleteClasses()
167
    {
168
        $autocompleteGenerators = $this->getAllAutocompleteGenerators();
169
        foreach ($autocompleteGenerators as $generatorClass) {
170
            /* @var Generator $generatorClass */
171
            $generatorClass::delete();
172
        }
173
        Craft::info('Autocomplete classes deleted', __METHOD__);
174
    }
175
176
    // Protected Methods
177
    // =========================================================================
178
179
    /**
180
     * Returns all available autocomplete generator classes.
181
     *
182
     * @return string[] The available autocomplete generator classes
183
     */
184
    public function getAllAutocompleteGenerators(): array
185
    {
186
        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...
187
            return $this->allAutocompleteGenerators;
188
        }
189
190
        $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...
191
            'types' => self::DEFAULT_AUTOCOMPLETE_GENERATORS,
192
        ]);
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...
193
        $this->trigger(self::EVENT_REGISTER_AUTOCOMPLETE_GENERATORS, $event);
194
        $this->allAutocompleteGenerators = $event->types;
0 ignored issues
show
Bug introduced by
The property allAutocompleteGenerators is declared read-only in nystudio107\autocomplete\Autocomplete.
Loading history...
195
196
        return $this->allAutocompleteGenerators;
197
    }
198
}
199