Helper   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 13
eloc 39
c 4
b 2
f 0
dl 0
loc 95
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCriticalCssTags() 0 29 4
A getCssInlineTags() 0 12 2
B getCssHash() 0 23 7
1
<?php
2
/**
3
 * Vite plugin for Craft CMS
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
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...
10
11
namespace nystudio107\vite\services;
12
13
use Craft;
14
use craft\base\Component;
15
use craft\helpers\Html;
16
use nystudio107\pluginvite\helpers\FileHelper;
17
use nystudio107\pluginvite\helpers\ManifestHelper;
18
use nystudio107\vite\models\Settings;
19
use nystudio107\vite\Vite;
20
use Twig\Error\LoaderError;
21
22
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
 * @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...
24
 * @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...
25
 * @since     1.0.5
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...
26
 */
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...
27
class Helper extends Component
28
{
29
    /**
30
     * Returns the Critical CSS file for $template wrapped in <style></style>
31
     * tags
32
     *
33
     * @param null|string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
34
     * @param array $attributes additional HTML key/value pair attributes to add to the resulting tag
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
35
     *
36
     * @return string
37
     * @throws LoaderError
38
     */
39
    public function getCriticalCssTags(?string $name = null, array $attributes = []): string
40
    {
41
        // Resolve the template name
42
        $template = Craft::$app->getView()->resolveTemplate($name ?? Vite::$templateName ?? '');
43
        if ($template) {
44
            /** @var Settings $settings */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
45
            $settings = Vite::$plugin->getSettings();
46
            $name = FileHelper::createUrl(
47
                pathinfo($template, PATHINFO_DIRNAME),
0 ignored issues
show
Bug introduced by
It seems like pathinfo($template, nyst...vices\PATHINFO_DIRNAME) can also be of type array; however, parameter $url of nystudio107\pluginvite\h...FileHelper::createUrl() 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

47
                /** @scrutinizer ignore-type */ pathinfo($template, PATHINFO_DIRNAME),
Loading history...
48
                pathinfo($template, PATHINFO_FILENAME)
0 ignored issues
show
Bug introduced by
It seems like pathinfo($template, nyst...ices\PATHINFO_FILENAME) can also be of type array; however, parameter $path of nystudio107\pluginvite\h...FileHelper::createUrl() 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

48
                /** @scrutinizer ignore-type */ pathinfo($template, PATHINFO_FILENAME)
Loading history...
49
            );
50
            $dirPrefix = 'templates/';
51
            if (defined('CRAFT_TEMPLATES_PATH')) {
52
                $dirPrefix = CRAFT_TEMPLATES_PATH;
53
            }
54
            $name = strstr($name, $dirPrefix);
55
            $pos = strpos($name, $dirPrefix);
56
            if ($pos !== false) {
57
                $name = (string)substr_replace($name, '', $pos, strlen($dirPrefix));
58
            }
59
            $path = FileHelper::createUrl(
60
                    $settings->criticalPath,
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 20.
Loading history...
61
                    $name
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 20.
Loading history...
62
                ) . $settings->criticalSuffix;
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
63
64
            return $this->getCssInlineTags($path, $attributes);
65
        }
66
67
        return '';
68
    }
69
70
    /**
71
     * Returns the passed in CSS file at the specified file system path or URL, wrapped in
72
     * <style></style> tags
73
     *
74
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
75
     * @param array $attributes additional HTML key/value pair attributes to add to the resulting tag
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
76
     *
77
     * @return string
78
     */
79
    public function getCssInlineTags(string $path, array $attributes = []): string
80
    {
81
        /** @var Settings $settings */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
82
        $settings = Vite::$plugin->getSettings();
83
        $result = FileHelper::fetch($path, null, $settings->cacheKeySuffix);
84
        if ($result) {
85
            $config = [];
86
87
            return Html::style($result, array_merge($config, $attributes));
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type array; however, parameter $content of yii\helpers\BaseHtml::style() 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

87
            return Html::style(/** @scrutinizer ignore-type */ $result, array_merge($config, $attributes));
Loading history...
88
        }
89
90
        return '';
91
    }
92
93
    /**
94
     * Return the hash value for the first CSS file bundled with the module specified via $path
95
     *
96
     * @param $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
97
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
98
     */
99
    public function getCssHash($path): string
100
    {
101
        /** @var Settings $settings */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
102
        $settings = Vite::$plugin->getSettings();
103
        ManifestHelper::fetchManifest($settings->manifestPath);
104
        $tags = ManifestHelper::manifestTags($path, false);
105
        foreach ($tags as $tag) {
106
            if (!empty($tag) && $tag['type'] === 'css') {
107
                // Extract only the Hash Value
108
                $modulePath = pathinfo($tag['url']);
109
                $moduleFilename = $modulePath['filename'];
110
                $hashPos = strpos($moduleFilename, '.') ?: strlen($moduleFilename);
111
                $moduleHash = substr($moduleFilename, $hashPos + 1);
112
                // Vite 5 now uses a `-` to separate the version hash, so account for that as well
113
                if (empty($moduleHash) && str_contains($moduleFilename, '-')) {
114
                    $moduleHash = substr($moduleFilename, strpos($moduleFilename, '-') + 1);
115
                }
116
117
                return $moduleHash;
118
            }
119
        }
120
121
        return '';
122
    }
123
}
124