Passed
Push — develop ( 3d3b3e...df5074 )
by Brent
03:50
created

Css::setMinify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Stitcher\Renderer\Extension;
4
5
use Leafo\ScssPhp\Compiler as Sass;
6
use Pageon\Html\Source;
7
use Stitcher\File;
8
use Stitcher\Renderer\Extension;
9
use Symfony\Component\Filesystem\Filesystem;
10
use tubalmartin\CssMin\Minifier;
11
12
class Css implements Extension
13
{
14
    /** @var string */
15
    protected $publicDirectory;
16
17
    /** @var \Leafo\ScssPhp\Compiler */
18
    protected $sass;
19
20
    /** @var \tubalmartin\CssMin\Minifier */
21
    private $minifier;
22
23
    /** @var bool */
24
    private $minify = false;
25
26 9
    public function __construct(
27
        string $publicDirectory,
28
        Sass $sass,
29
        Minifier $minifier
30
    ) {
31 9
        $this->publicDirectory = $publicDirectory;
32 9
        $this->sass = $sass;
33 9
        $this->minifier = $minifier;
34 9
    }
35
36 9
    public function setMinify(bool $minify): self
37
    {
38 9
        $this->minify = $minify;
39
40 9
        return $this;
41
    }
42
43 5
    public function name(): string
44
    {
45 5
        return 'css';
46
    }
47
48 3
    public function link(string $src): string
49
    {
50 3
        $source = $this->parseSource($src);
51
52 3
        return "<link rel=\"stylesheet\" href=\"{$source->url()}\" />";
53
    }
54
55 3
    public function inline(string $src): string
56
    {
57 3
        $source = $this->parseSource($src);
58
59 3
        return '<style>' . $source->content() . '</style>';
60
    }
61
62 6
    public function parseSource(string $src): Source
63
    {
64 6
        $src = ltrim($src, '/');
65
66 6
        ['dirname' => $dirname, 'filename' => $filename, 'extension' => $extension] = pathinfo($src);
0 ignored issues
show
Bug introduced by
The variable $filename does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $extension seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
67
68 6
        $content = File::read($src);
69
70 6
        if (\in_array($extension, ['scss', 'sass'])) {
0 ignored issues
show
Bug introduced by
The variable $extension seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
71 4
            $content = $this->sass->compile($content);
72 4
            $extension = 'css';
73
        }
74
75 6
        if ($this->minify) {
76
            $content = $this->minifier->run($content);
77
        }
78
79 6
        $path = "{$dirname}/{$filename}.{$extension}";
0 ignored issues
show
Bug introduced by
The variable $extension does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
80
81 6
        $this->saveFile($path, $content);
82
83 6
        return new Source("/{$path}", $content);
84
    }
85
86 6
    protected function saveFile(string $path, string $content): void
87
    {
88 6
        $fs = new Filesystem();
89
90 6
        $fs->dumpFile(
91 6
            File::path("{$this->publicDirectory}/{$path}"),
92 6
            $content
93
        );
94 6
    }
95
}
96