Passed
Push — develop ( 47186b...746e68 )
by Brent
02:35
created

Css::link()   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 Stitcher\File;
7
use Stitcher\Renderer\Extension;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
class Css implements Extension
11
{
12
    protected $sourceDirectory;
13
    protected $publicDirectory;
14
    protected $sass;
15
16 16
    public function __construct(
17
        string $sourceDirectory,
18
        string $publicDirectory,
19
        Sass $sass
20
    ) {
21 16
        $this->sourceDirectory = $sourceDirectory;
22 16
        $this->publicDirectory = $publicDirectory;
23 16
        $this->sass = $sass;
24 16
    }
25
26 12
    public function name(): string
27
    {
28 12
        return 'css';
29
    }
30
31 8
    public function link(string $src): string
32
    {
33 8
        [$url, $content] = $this->parseSource($src);
1 ignored issue
show
Bug introduced by
The variable $url 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 $content 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...
34
35 8
        return "<link rel=\"stylesheet\" href=\"{$url}\" />";
36
    }
37
38 8
    public function inline(string $src): string
39
    {
40 8
        [$url, $content] = $this->parseSource($src);
1 ignored issue
show
Bug introduced by
The variable $url 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 $content 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...
41
42 8
        return '<style>' . $content . '</style>';
43
    }
44
45 11
    public function parseSource(string $src): array
46
    {
47 11
        $src = ltrim($src, '/');
48
49 11
        ['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...
50
51 11
        $content = File::read("{$this->sourceDirectory}/{$src}");
52
53 11
        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...
54 9
            $content = $this->sass->compile($content);
55 9
            $extension = 'css';
56
        }
57
58 11
        $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...
59
60 11
        $this->saveFile($path, $content);
61
62 11
        return ["/{$path}", $content];
63
    }
64
65 11
    protected function saveFile(string $path, string $content): void
66
    {
67 11
        $fs = new Filesystem();
68
69 11
        $fs->dumpFile(
70 11
            File::path("{$this->publicDirectory}/{$path}"),
71 11
            $content
72
        );
73 11
    }
74
}
75