Passed
Push — develop ( cd4090...9696c4 )
by Andrew
04:47
created

Helper::combinePaths()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 24
rs 9.6666
cc 3
nc 1
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\variables;
12
13
use craft\base\Component;
14
use nystudio107\pluginvite\variables\ViteVariableInterface;
15
use nystudio107\pluginvite\variables\ViteVariableTrait;
16
17
use craft\helpers\Template;
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
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...
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.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...
25
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
26
class Helper extends Component
27
{
28
    /**
29
     * Returns the Critical CSS file for $template wrapped in <style></style>
30
     * tags
31
     *
32
     * @param null|string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
33
     * @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...
34
     *
35
     * @return Markup
36
     * @throws \Twig\Error\LoaderError
37
     */
38
    public function includeCriticalCssTags($name = null, array $attributes = []): Markup
39
    {
40
        // Resolve the template name
41
        $template = Craft::$app->getView()->resolveTemplate($name ?? Twigpack::$templateName ?? '');
0 ignored issues
show
Bug introduced by
The type nystudio107\vite\variables\Craft was not found. Did you mean Craft? If so, make sure to prefix the type with \.
Loading history...
Bug introduced by
The type nystudio107\vite\variables\Twigpack was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
        if ($template) {
43
            $name = self::combinePaths(
0 ignored issues
show
Bug Best Practice introduced by
The method nystudio107\vite\variables\Helper::combinePaths() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
            /** @scrutinizer ignore-call */ 
44
            $name = self::combinePaths(
Loading history...
44
                pathinfo($template, PATHINFO_DIRNAME),
0 ignored issues
show
Bug introduced by
It seems like pathinfo($template, nyst...ables\PATHINFO_DIRNAME) can also be of type array; however, parameter $paths of nystudio107\vite\variables\Helper::combinePaths() 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

44
                /** @scrutinizer ignore-type */ pathinfo($template, PATHINFO_DIRNAME),
Loading history...
45
                pathinfo($template, PATHINFO_FILENAME)
46
            );
47
            $dirPrefix = 'templates/';
48
            if (defined('CRAFT_TEMPLATES_PATH')) {
49
                $dirPrefix = CRAFT_TEMPLATES_PATH;
50
            }
51
            $name = strstr($name, $dirPrefix);
52
            $name = (string)str_replace($dirPrefix, '', $name);
53
            $path = self::combinePaths(
54
                    $config['localFiles']['basePath'],
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...
Comprehensibility Best Practice introduced by
The variable $config seems to be never defined.
Loading history...
55
                    $config['localFiles']['criticalPrefix'],
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...
56
                    $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...
57
                ) . $config['localFiles']['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...
58
59
            return self::getCssInlineTags($path, $attributes);
0 ignored issues
show
Bug introduced by
The method getCssInlineTags() does not exist on nystudio107\vite\variables\Helper. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
            return self::/** @scrutinizer ignore-call */ getCssInlineTags($path, $attributes);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
        }
61
62
        return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the type-hinted return Twig\Markup.
Loading history...
63
64
        return Template::raw(
0 ignored issues
show
Unused Code introduced by
return craft\helpers\Tem...ll, $attributes) ?? '') is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
65
            Twigpack::$plugin->manifest->getCriticalCssTags($name, null, $attributes) ?? ''
66
        );
67
    }
68
69
    /**
70
     * Combined the passed in paths, whether file system or URL
71
     *
72
     * @param string ...$paths
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
73
     *
74
     * @return string
75
     */
76
    protected function combinePaths(string ...$paths): string
77
    {
78
        $last_key = count($paths) - 1;
79
        array_walk($paths, function (&$val, $key) use ($last_key) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
80
            switch ($key) {
81
                case 0:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
82
                    $val = rtrim($val, '/ ');
83
                    break;
84
                case $last_key:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
85
                    $val = ltrim($val, '/ ');
86
                    break;
87
                default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
88
                    $val = trim($val, '/ ');
89
                    break;
90
            }
91
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
92
93
        $first = array_shift($paths);
94
        $last = array_pop($paths);
95
        $paths = array_filter($paths);
96
        array_unshift($paths, $first);
97
        $paths[] = $last;
98
99
        return implode('/', $paths);
100
    }
101
102
}
103