Passed
Push — develop ( e22c82...346da8 )
by Andrew
04:14
created

Helper::getCssHash()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 20
rs 9.9
c 0
b 0
f 0
cc 4
nc 4
nop 1
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 @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license 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\vite\services;
12
13
use nystudio107\vite\Vite;
14
use nystudio107\vite\models\Settings;
15
16
use nystudio107\pluginvite\helpers\FileHelper;
17
use nystudio107\pluginvite\helpers\ManifestHelper;
18
19
use Craft;
20
use craft\base\Component;
21
use craft\helpers\Html;
22
23
use Twig\Error\LoaderError;
24
25
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
26
 * @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...
27
 * @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...
28
 * @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...
29
 */
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...
30
class Helper extends Component
31
{
32
    /**
33
     * Returns the Critical CSS file for $template wrapped in <style></style>
34
     * tags
35
     *
36
     * @param null|string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
37
     * @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...
38
     *
39
     * @return string
40
     * @throws LoaderError
41
     */
42
    public function getCriticalCssTags($name = null, array $attributes = []): string
43
    {
44
        // Resolve the template name
45
        $template = Craft::$app->getView()->resolveTemplate($name ?? Vite::$templateName ?? '');
46
        if ($template) {
47
            /** @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...
48
            $settings = Vite::$plugin->getSettings();
49
            $name = FileHelper::createUrl(
50
                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

50
                /** @scrutinizer ignore-type */ pathinfo($template, PATHINFO_DIRNAME),
Loading history...
51
                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

51
                /** @scrutinizer ignore-type */ pathinfo($template, PATHINFO_FILENAME)
Loading history...
52
            );
53
            $dirPrefix = 'templates/';
54
            if (defined('CRAFT_TEMPLATES_PATH')) {
55
                $dirPrefix = CRAFT_TEMPLATES_PATH;
56
            }
57
            $name = strstr($name, $dirPrefix);
58
            $name = (string)str_replace($dirPrefix, '', $name);
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
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
71
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
72
     * @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...
73
     *
74
     * @return string
75
     */
76
    public function getCssInlineTags(string $path, array $attributes = []): string
77
    {
78
        /** @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...
79
        $settings = Vite::$plugin->getSettings();
80
        $result = FileHelper::fetch($path, null, $settings->cacheKeySuffix);
81
        if ($result) {
82
            $config = [];
83
84
            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

84
            return Html::style(/** @scrutinizer ignore-type */ $result, array_merge($config, $attributes));
Loading history...
85
        }
86
87
        return '';
88
    }
89
90
    /**
91
     * Return the hash value for the first CSS file bundled with the module specified via $path
92
     *
93
     * @param $path
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
94
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
95
     */
96
    public function getCssHash($path): string
97
    {
98
        /** @var Settings $settings */
0 ignored issues
show
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...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
99
        $settings = Vite::$plugin->getSettings();
100
        ManifestHelper::fetchManifest($settings->manifestPath);
101
        $tags = ManifestHelper::manifestTags($path, false);
102
        foreach($tags as $tag) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
103
            if (!empty($tag)) {
104
                if ($tag['type'] === 'css') {
105
                    // Extract only the Hash Value
106
                    $modulePath = pathinfo($tag['url']);
107
                    $moduleFilename = $modulePath['filename'];
108
                    $moduleHash = substr($moduleFilename, strpos($moduleFilename, ".") + 1);
109
110
                    return $moduleHash;
111
                }
112
            }
113
        }
114
115
        return '';
116
    }
117
}
118