Passed
Pull Request — v1 (#2)
by John D
10:56
created

ViteVariableTrait::devServerRunning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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
     * Inline the contents of a local file (via path) or remote file (via URL) in your templates.
78
     * Yii2 aliases and/or environment variables may be used
79
     *
80
     * @param string $pathOrUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
81
     *
82
     * @return Markup
83
     */
84
    public function inline(string $pathOrUrl): Markup
85
    {
86
        $file = $this->viteService->fetch($pathOrUrl);
87
        if ($file === null) {
88
            $file = '';
89
        }
90
91
        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

91
        return Template::raw(/** @scrutinizer ignore-type */ $file);
Loading history...
92
    }
93
94
    /**
95
     * Determine whether the Vite dev server is running
96
     *
97
     * @return bool
98
     */
99
    public function devServerRunning(): bool
100
    {
101
        return $this->viteService->devServerRunning();
102
    }
103
}
104