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

Generator::saveTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
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
    // Public Static Methods
27
    // =========================================================================
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @inheritDoc
31
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
32
    public static function getGeneratorName(): string
33
    {
34
        return '';
35
    }
36
37
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
38
     * @inheritDoc
39
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
40
    public static function getGeneratorStubsPath(): string
41
    {
42
        return Autocomplete::getInstance()->basePath . self::STUBS_DIR;
43
    }
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46
     * @inheritDoc
47
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
48
    public static function generate()
49
    {
50
    }
51
52
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
53
     * @inheritDoc
54
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
55
    public static function regenerate()
56
    {
57
    }
58
59
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
60
     * @inheritDoc
61
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
62
    public static function delete()
63
    {
64
        unlink(static::getGeneratedFilePath());
65
    }
66
67
    // Protected Static Methods
68
    // =========================================================================
69
70
    /**
71
     * Save the template to disk, with variable substitution
72
     *
73
     * @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...
74
     * @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...
75
     */
76
    protected static function saveTemplate(array $vars): bool
77
    {
78
        $stub = file_get_contents(static::getStubFilePath());
79
        if ($stub) {
80
            $template = str_replace(array_keys($vars), array_values($vars), $stub);
81
82
            return !(file_put_contents(static::getGeneratedFilePath(), $template) === false);
83
        }
84
85
        return false;
86
    }
87
88
    /**
89
     * Don't regenerate the file if it already exists
90
     *
91
     * @return bool
92
     */
93
    protected static function shouldRegenerateFile(): bool
94
    {
95
        return !is_file(static::getGeneratedFilePath());
96
    }
97
98
    /**
99
     * Return a path to the generated autocomplete template file
100
     *
101
     * @return string
102
     */
103
    protected static function getGeneratedFilePath(): string
104
    {
105
        return Craft::$app->getPath()->getCompiledClassesPath() . DIRECTORY_SEPARATOR . static::getGeneratorName() . self::TEMPLATE_EXTENSION;
106
    }
107
108
    /**
109
     * Return a path to the autocomplete template stub
110
     *
111
     * @return string
112
     */
113
    protected static function getStubFilePath(): string
114
    {
115
        return static::getGeneratorStubsPath() . DIRECTORY_SEPARATOR . static::getGeneratorName() . self::STUBS_EXTENSION;
116
    }
117
}
118