Passed
Push — v1 ( 4c751d...5017f9 )
by Andrew
49:44 queued 11:22
created

ViteVariableTrait::asset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
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 asset
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 asset(string $path): Markup
84
    {
85
        return Template::raw(
86
            $this->viteService->asset($path)
87
        );
88
    }
89
90
    /**
91
     * Inline the contents of a local file (via path) or remote file (via URL) in your templates.
92
     * Yii2 aliases and/or environment variables may be used
93
     *
94
     * @param string $pathOrUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
95
     *
96
     * @return Markup
97
     */
98
    public function inline(string $pathOrUrl): Markup
99
    {
100
        $file = $this->viteService->fetch($pathOrUrl);
101
        if ($file === null) {
102
            $file = '';
103
        }
104
105
        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

105
        return Template::raw(/** @scrutinizer ignore-type */ $file);
Loading history...
106
    }
107
108
    /**
109
     * Determine whether the Vite dev server is running
110
     *
111
     * @return bool
112
     */
113
    public function devServerRunning(): bool
114
    {
115
        return $this->viteService->devServerRunning();
116
    }
117
}
118