Passed
Push — v1 ( 270350...b42adf )
by Andrew
09:36 queued 04:37
created

ViteVariableTrait::script()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * Vite plugin for Craft CMS 3.x
4
 *
5
 * Allows the use of the Vite.js next generation frontend tooling with Craft CMS
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
 * @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...
9
 */
0 ignored issues
show
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
10
11
namespace nystudio107\pluginvite\variables;
12
13
use nystudio107\pluginvite\services\ViteService;
14
15
use craft\helpers\Template;
16
17
use yii\base\InvalidConfigException;
18
19
use Twig\Markup;
20
21
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
22
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
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
The tag in position 1 should be the @package tag
Loading history...
23
 * @package   Vite
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
24
 * @since     1.0.4
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...
25
 */
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...
26
trait ViteVariableTrait
27
{
28
    // Public Properties
29
    // =========================================================================
30
31
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
32
     * @var ViteService the Vite service
33
     */
34
    public $viteService;
35
36
    // Public Methods
37
    // =========================================================================
38
39
    /**
40
     * Return the appropriate tags to load the Vite script, either via the dev server or
41
     * extracting it from the manifest.json file
42
     *
43
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
44
     * @param bool $asyncCss
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
45
     * @param array $scriptTagAttrs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
46
     * @param array $cssTagAttrs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
47
     *
48
     * @return Markup
49
     */
50
    public function script(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): Markup
51
    {
52
        return Template::raw(
53
            $this->viteService->script($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs)
54
        );
55
    }
56
57
    /**
58
     * Register the appropriate tags to the Craft View to load the Vite script, either via the dev server or
59
     * extracting it from the manifest.json file
60
     *
61
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
62
     * @param bool $asyncCss
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
63
     * @param array $scriptTagAttrs
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
64
     * @param array $cssTagAttrs
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
65
     *
66
     * @return Markup
67
     * @throws InvalidConfigException
68
     */
69
    public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): Markup
70
    {
71
        $this->viteService->register($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
72
73
        return Template::raw('');
74
    }
75
76
    /**
77
     * Return the URL for the given entry
78
     *
79
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
80
     *
81
     * @return Markup
82
     */
83
    public function entry(string $path): Markup
84
    {
85
        return Template::raw(
86
            $this->viteService->entry($path)
87
        );
88
    }
89
90
    /**
91
     * Return the URL for the given asset
92
     *
93
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
94
     *
95
     * @return Markup
96
     */
97
    public function asset(string $path): Markup
98
    {
99
        return Template::raw(
100
            $this->viteService->asset($path)
101
        );
102
    }
103
104
    /**
105
     * Inline the contents of a local file (via path) or remote file (via URL) in your templates.
106
     * Yii2 aliases and/or environment variables may be used
107
     *
108
     * @param string $pathOrUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
109
     *
110
     * @return Markup
111
     */
112
    public function inline(string $pathOrUrl): Markup
113
    {
114
        $file = $this->viteService->fetch($pathOrUrl);
115
        if ($file === null) {
116
            $file = '';
117
        }
118
119
        return Template::raw($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type array; however, parameter $value of craft\helpers\Template::raw() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

119
        return Template::raw(/** @scrutinizer ignore-type */ $file);
Loading history...
120
    }
121
122
    /**
123
     * Determine whether the Vite dev server is running
124
     *
125
     * @return bool
126
     */
127
    public function devServerRunning(): bool
128
    {
129
        return $this->viteService->devServerRunning();
130
    }
131
}
132