Passed
Push — v1 ( c9cb31...01c3bb )
by Andrew
13:05 queued 08:10
created

Helper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 24
c 0
b 0
f 0
dl 0
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCssInlineTags() 0 12 2
A getCriticalCssTags() 0 26 3
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
18
use Craft;
19
use craft\base\Component;
20
use craft\helpers\Html;
21
22
use Twig\Error\LoaderError;
23
24
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
25
 * @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...
26
 * @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...
27
 * @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...
28
 */
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...
29
class Helper extends Component
30
{
31
    /**
32
     * Returns the Critical CSS file for $template wrapped in <style></style>
33
     * tags
34
     *
35
     * @param null|string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
36
     * @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...
37
     *
38
     * @return string
39
     * @throws LoaderError
40
     */
41
    public function getCriticalCssTags($name = null, array $attributes = []): string
42
    {
43
        // Resolve the template name
44
        $template = Craft::$app->getView()->resolveTemplate($name ?? Vite::$templateName ?? '');
45
        if ($template) {
46
            /** @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...
47
            $settings = Vite::$plugin->getSettings();
48
            $name = FileHelper::createUrl(
49
                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

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

50
                /** @scrutinizer ignore-type */ pathinfo($template, PATHINFO_FILENAME)
Loading history...
51
            );
52
            $dirPrefix = 'templates/';
53
            if (defined('CRAFT_TEMPLATES_PATH')) {
54
                $dirPrefix = CRAFT_TEMPLATES_PATH;
55
            }
56
            $name = strstr($name, $dirPrefix);
57
            $name = (string)str_replace($dirPrefix, '', $name);
58
            $path = FileHelper::createUrl(
59
                    $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...
60
                    $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...
61
                ) . $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...
62
63
            return $this->getCssInlineTags($path, $attributes);
64
        }
65
66
        return '';
67
    }
68
69
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
70
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
71
     * @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...
72
     *
73
     * @return string
74
     */
75
    public function getCssInlineTags(string $path, array $attributes = []): string
76
    {
77
        /** @var Settings $settings */
0 ignored issues
show
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...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
78
        $settings = Vite::$plugin->getSettings();
79
        $result = FileHelper::fetch($path, null, $settings->cacheKeySuffix);
80
        if ($result) {
81
            $config = [];
82
83
            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

83
            return Html::style(/** @scrutinizer ignore-type */ $result, array_merge($config, $attributes));
Loading history...
84
        }
85
86
        return '';
87
    }
88
}
89