Passed
Push — develop ( a1b3d6...8fb7bf )
by Andrew
03:53
created

Generator::getGeneratorStubsPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\base;
14
15
use nystudio107\autocomplete\Autocomplete;
16
17
use Craft;
18
19
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
20
 * @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...
21
 * @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...
22
 * @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...
23
 */
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...
24
abstract class Generator implements GeneratorInterface
25
{
26
    // Traits
27
    // =========================================================================
28
29
    use GeneratorTrait;
30
31
    // Public Static Methods
32
    // =========================================================================
33
34
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
35
     * @inheritDoc
36
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
37
    public static function getGeneratorName(): string
38
    {
39
        return '';
40
    }
41
42
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
43
     * @inheritDoc
44
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
45
    public static function getGeneratorStubsPath(): string
46
    {
47
        return Autocomplete::getInstance()->basePath . self::STUBS_DIR;
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 generate()
54
    {
55
    }
56
57
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
58
     * @inheritDoc
59
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
60
    public static function regenerate()
61
    {
62
    }
63
64
    // Protected Static Methods
65
    // =========================================================================
66
67
    /**
68
     * Save the template to disk, with variable substitution
69
     *
70
     * @param array $vars key/value variables to be replaced in the stub
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
71
     * @return bool Whether the template was successfully saved
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
72
     */
73
    protected static function saveTemplate(array $vars): bool
74
    {
75
        $stub = file_get_contents(static::getStubFilePath());
76
        if ($stub) {
77
            $template = str_replace(array_keys($vars), array_values($vars), $stub);
78
79
            return !(file_put_contents(static::getGeneratedFilePath(), $template) === false);
80
        }
81
82
        return false;
83
    }
84
85
    /**
86
     * Don't regenerate the file if it already exists
87
     *
88
     * @return bool
89
     */
90
    protected static function shouldRegenerateFile(): bool
91
    {
92
        $path = self::getGeneratedFilePath();
93
94
        return !is_file($path);
95
    }
96
97
    /**
98
     * Return a path to the generated autocomplete template file
99
     *
100
     * @return string
101
     */
102
    protected static function getGeneratedFilePath(): string
103
    {
104
        return Craft::$app->getPath()->getCompiledClassesPath() . DIRECTORY_SEPARATOR . static::getGeneratorName() . self::TEMPLATE_EXTENSION;
105
    }
106
107
    /**
108
     * Return a path to the autocomplete template stub
109
     *
110
     * @return string
111
     */
112
    protected static function getStubFilePath(): string
113
    {
114
        return static::getGeneratorStubsPath() . DIRECTORY_SEPARATOR . static::getGeneratorName() . self::STUBS_EXTENSION;
115
    }
116
117
}
118