Issues (194)

src/base/Generator.php (33 issues)

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
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
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 * @copyright Copyright (c) PutYourLightsOn
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
11
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
12
13
namespace nystudio107\autocomplete\base;
14
15
use Craft;
16
use nystudio107\autocomplete\Autocomplete;
17
18
/**
0 ignored issues
show
Missing short description in doc comment
Loading history...
19
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
20
 * @package   autocomplete
0 ignored issues
show
Package name "autocomplete" is not valid; consider "Autocomplete" instead
Loading history...
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
21
 * @since     1.0.0
0 ignored issues
show
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
The tag in position 3 should be the @author tag
Loading history...
22
 */
0 ignored issues
show
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
Missing @category tag in class comment
Loading history...
23
abstract class Generator implements GeneratorInterface
24
{
25
    // Constants
26
    // =========================================================================
27
28
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
29
     * @event Event The event that is triggered before generating autocomplete classes.
30
     *
31
     * ```php
32
     * use nystudio107\autocomplete\events\DefineGeneratorValuesEvent;
33
     * use nystudio107\autocomplete\generators\AutocompleteTwigExtensionGenerator;
34
     * use yii\base\Event;
35
     *
36
     * Event::on(AutocompleteTwigExtensionGenerator::class,
37
     *     AutocompleteTwigExtensionGenerator::EVENT_BEFORE_GENERATE,
38
     *     function(DefineGeneratorValuesEvent $event) {
39
     *         $event->values['myVariable'] = 'value';
40
     *     }
41
     * );
42
     * ```
43
     */
44
    public const EVENT_BEFORE_GENERATE = 'beforeGenerate';
45
46
    // Public Static Methods
47
    // =========================================================================
48
49
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
50
     * @inheritDoc
51
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
52
    public static function getGeneratorName(): string
53
    {
54
        return '';
55
    }
56
57
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
58
     * @inheritDoc
59
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
60
    public static function getGeneratorStubsPath(): string
61
    {
62
        return Autocomplete::getInstance()->basePath . self::STUBS_DIR;
63
    }
64
65
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
66
     * @inheritDoc
67
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
68
    public static function generate()
69
    {
70
    }
71
72
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
73
     * @inheritDoc
74
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
75
    public static function regenerate()
76
    {
77
    }
78
79
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
80
     * @inheritDoc
81
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
82
    public static function delete()
83
    {
84
        $path = static::getGeneratedFilePath();
85
        if (is_file($path)) {
86
            @unlink($path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

86
            /** @scrutinizer ignore-unhandled */ @unlink($path);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
87
        }
88
    }
89
90
    // Protected Static Methods
91
    // =========================================================================
92
93
    /**
94
     * Save the template to disk, with variable substitution
95
     *
96
     * @param array $vars key/value variables to be replaced in the stub
0 ignored issues
show
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
97
     * @return bool Whether the template was successfully saved
0 ignored issues
show
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
98
     */
99
    protected static function saveTemplate(array $vars): bool
100
    {
101
        $stub = file_get_contents(static::getStubFilePath());
102
        if ($stub) {
103
            $template = str_replace(array_keys($vars), array_values($vars), $stub);
104
105
            return !(file_put_contents(static::getGeneratedFilePath(), $template) === false);
106
        }
107
108
        return false;
109
    }
110
111
    /**
112
     * Don't regenerate the file if it already exists
113
     *
114
     * @return bool
115
     */
116
    protected static function shouldRegenerateFile(): bool
117
    {
118
        return !is_file(static::getGeneratedFilePath());
119
    }
120
121
    /**
122
     * Return a path to the generated autocomplete template file
123
     *
124
     * @return string
125
     */
126
    protected static function getGeneratedFilePath(): string
127
    {
128
        return Craft::$app->getPath()->getCompiledClassesPath() . DIRECTORY_SEPARATOR . static::getGeneratorName() . self::TEMPLATE_EXTENSION;
129
    }
130
131
    /**
132
     * Return a path to the autocomplete template stub
133
     *
134
     * @return string
135
     */
136
    protected static function getStubFilePath(): string
137
    {
138
        return static::getGeneratorStubsPath() . DIRECTORY_SEPARATOR . static::getGeneratorName() . self::STUBS_EXTENSION;
139
    }
140
}
141