Passed
Push — master ( 1c8d05...42c076 )
by Enjoys
01:48
created

Asset::getPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\AssetsCollector;
6
7
8
use Enjoys\Traits\Options;
9
10
class Asset
11
{
12
    use Options;
13
14
    public const TYPE_CSS = 'css';
15
    public const TYPE_JS = 'js';
16
17
    public const PARAM_MINIFY = 'minify';
18
    private ?string $id = null;
19
    /**
20
     * @var false|string
21
     */
22
    private $path;
23
    private string $type;
24
    private bool $isUrl;
25
    private string $origPath;
26
27
    /**
28
     * Asset constructor.
29
     * @param string $type
30
     * @param string $path
31
     * @param array<mixed> $params
32
     */
33 27
    public function __construct(string $type, string $path, array $params = [])
34
    {
35 27
        $this->setOptions($params);
36 27
        $this->type = $type;
37 27
        $this->origPath = $path;
38 27
        $this->path = $path;
39 27
        $this->isUrl = $this->checkIsUrl();
40
41 27
        $this->normalizePath();
42 27
    }
43
44 27
    private function normalizePath(): void
45
    {
46 27
        if ($this->isUrl() && $this->path !== false) {
47 18
            $this->setId();
48 18
            return;
49
        }
50
51 12
        if (false === $projectDir = \getenv('ASSETS_PROJECT_DIRECTORY')) {
52
            $projectDir = '';
53
        }
54
        $paths = [
55 12
            $this->path,
56 12
            $projectDir . $this->path
57
        ];
58
59 12
        foreach ($paths as $path) {
60 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

60
            if (false !== $this->path = realpath(/** @scrutinizer ignore-type */ $path)) {
Loading history...
61 9
                $this->setId();
62 9
                break;
63
            }
64
        }
65 12
    }
66
67 27
    private function checkIsUrl(): bool
68
    {
69 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

69
        if (\str_starts_with(/** @scrutinizer ignore-type */ $this->path, '//')) {
Loading history...
70 15
            $this->path = Helpers::getHttpScheme() . ':' . $this->path;
71 15
            return true;
72
        }
73
74 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

74
        if (in_array(strpos(/** @scrutinizer ignore-type */ $this->path, '://'), [4, 5])) {
Loading history...
75 4
            return true;
76
        }
77 12
        return false;
78
    }
79
80
81
82 10
    public function isMinify(): bool
83
    {
84 10
        return $this->getOption(self::PARAM_MINIFY, true);
85
    }
86
87
    /**
88
     * @return false|string
89
     */
90 25
    public function getPath()
91
    {
92 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...
93
    }
94
95 16
    public function getType(): string
96
    {
97 16
        return $this->type;
98
    }
99
100 27
    public function isUrl(): bool
101
    {
102 27
        return $this->isUrl;
103
    }
104
105
106
    /**
107
     * @return string|null
108
     */
109 14
    public function getId(): ?string
110
    {
111 14
        return $this->id;
112
    }
113
114
    /**
115
     * @return string
116
     */
117 2
    public function getOrigPath(): string
118
    {
119 2
        return $this->origPath;
120
    }
121
122 24
    private function setId(): void
123
    {
124 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

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