Passed
Push — master ( b9addd...bd3c1c )
by Enjoys
01:53
created

Asset   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 97.67%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
eloc 43
c 4
b 0
f 2
dl 0
loc 118
ccs 42
cts 43
cp 0.9767
rs 10
wmc 17

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getPath() 0 3 1
A setId() 0 3 1
A getOrigPath() 0 3 1
A getType() 0 3 1
A checkIsUrl() 0 11 3
A __construct() 0 10 1
A isUrl() 0 3 1
A isMinify() 0 3 1
A normalizePath() 0 19 6
A getId() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\AssetsCollector;
6
7
use Enjoys\Traits\Options;
8
9
class Asset
10
{
11
    use Options;
12
13
    public const TYPE_CSS = 'css';
14
    public const TYPE_JS = 'js';
15
16
    public const PARAM_MINIFY = 'minify';
17
    private ?string $id = null;
18
    /**
19
     * @var false|string
20
     */
21
    private $path;
22
    private string $type;
23
    private bool $isUrl;
24
    private string $origPath;
25
    private bool $minify;
26
27
28
    /**
29
     * Asset constructor.
30
     * @param string $type
31
     * @param string $path
32
     * @param array<mixed> $params
33
     */
34 27
    public function __construct(string $type, string $path, array $params = [])
35
    {
36 27
        $this->setOptions($params);
37 27
        $this->type = $type;
38 27
        $this->origPath = $path;
39 27
        $this->path = $path;
40 27
        $this->minify = $this->getOption(self::PARAM_MINIFY, true);
41 27
        $this->isUrl = $this->checkIsUrl();
42
43 27
        $this->normalizePath();
44 27
    }
45
46 27
    private function normalizePath(): void
47
    {
48 27
        if ($this->isUrl() && $this->path !== false) {
49 18
            $this->setId();
50 18
            return;
51
        }
52
53 12
        if (false === $projectDir = \getenv('ASSETS_PROJECT_DIRECTORY')) {
54
            $projectDir = '';
55
        }
56
        $paths = [
57 12
            $this->path,
58 12
            $projectDir . $this->path
59
        ];
60
61 12
        foreach ($paths as $path) {
62 12
            if (false !== $this->path = realpath($path)) {
0 ignored issues
show
Bug introduced by
It seems like $path can also be of type boolean; however, parameter $path of realpath() 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

62
            if (false !== $this->path = realpath(/** @scrutinizer ignore-type */ $path)) {
Loading history...
63 9
                $this->setId();
64 9
                break;
65
            }
66
        }
67 12
    }
68
69 27
    private function checkIsUrl(): bool
70
    {
71 27
        if (\str_starts_with($this->path, '//')) {
0 ignored issues
show
Bug introduced by
It seems like $this->path can also be of type boolean; however, parameter $haystack of str_starts_with() 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

71
        if (\str_starts_with(/** @scrutinizer ignore-type */ $this->path, '//')) {
Loading history...
72 15
            $this->path = Helpers::getHttpScheme() . ':' . $this->path;
73 15
            return true;
74
        }
75
76 14
        if (in_array(strpos($this->path, '://'), [4, 5])) {
0 ignored issues
show
Bug introduced by
It seems like $this->path can also be of type boolean; however, parameter $haystack of strpos() 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

76
        if (in_array(strpos(/** @scrutinizer ignore-type */ $this->path, '://'), [4, 5])) {
Loading history...
77 4
            return true;
78
        }
79 12
        return false;
80
    }
81
82
83
84 10
    public function isMinify(): bool
85
    {
86 10
        return $this->minify;
87
    }
88
89
    /**
90
     * @return false|string
91
     */
92 25
    public function getPath()
93
    {
94 25
        return $this->path;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->path also could return the type boolean which is incompatible with the documented return type false|string.
Loading history...
95
    }
96
97 16
    public function getType(): string
98
    {
99 16
        return $this->type;
100
    }
101
102 27
    public function isUrl(): bool
103
    {
104 27
        return $this->isUrl;
105
    }
106
107
108
    /**
109
     * @return string|null
110
     */
111 14
    public function getId(): ?string
112
    {
113 14
        return $this->id;
114
    }
115
116
    /**
117
     * @return string
118
     */
119 2
    public function getOrigPath(): string
120
    {
121 2
        return $this->origPath;
122
    }
123
124 24
    private function setId(): void
125
    {
126 24
        $this->id = md5($this->path);
0 ignored issues
show
Bug introduced by
It seems like $this->path can also be of type boolean; however, parameter $string of md5() 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

126
        $this->id = md5(/** @scrutinizer ignore-type */ $this->path);
Loading history...
127 24
    }
128
}
129