Passed
Push — master ( ecdd3a...7919f4 )
by Enjoys
01:55
created

Asset::getId()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
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 !== $this->path = realpath($this->path)) {
0 ignored issues
show
Bug introduced by
It seems like $this->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

51
        if (false !== $this->path = realpath(/** @scrutinizer ignore-type */ $this->path)) {
Loading history...
52 9
            $this->setId();
53
        }
54 12
    }
55
56 27
    private function checkIsUrl(): bool
57
    {
58 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

58
        if (\str_starts_with(/** @scrutinizer ignore-type */ $this->path, '//')) {
Loading history...
59 15
            $this->path = $this->defineHttpScheme() . ':' . $this->path;
60 15
            return true;
61
        }
62
63 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

63
        if (in_array(strpos(/** @scrutinizer ignore-type */ $this->path, '://'), [4, 5])) {
Loading history...
64 4
            return true;
65
        }
66 12
        return false;
67
    }
68
69 15
    private function defineHttpScheme(): string
70
    {
71 15
        $scheme = 'http';
72 15
        if (isset($_SERVER['HTTP_SCHEME'])) {
73 1
            return $_SERVER['HTTP_SCHEME'];
74
        }
75
76 14
        if (isset($_SERVER['HTTPS']) && \strtolower($_SERVER['HTTPS']) != 'off') {
77 1
            return 'https';
78
        }
79
80 13
        if (isset($_SERVER['SERVER_PORT']) && 443 == $_SERVER['SERVER_PORT']) {
81 1
            return 'https';
82
        }
83 12
        return $scheme;
84
    }
85
86
87 10
    public function isMinify(): bool
88
    {
89 10
        return $this->getOption(self::PARAM_MINIFY, true);
90
    }
91
92
    /**
93
     * @return false|string
94
     */
95 25
    public function getPath()
96
    {
97 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...
98
    }
99
100 16
    public function getType(): string
101
    {
102 16
        return $this->type;
103
    }
104
105 27
    public function isUrl(): bool
106
    {
107 27
        return $this->isUrl;
108
    }
109
110
111
    /**
112
     * @return string|null
113
     */
114 14
    public function getId(): ?string
115
    {
116 14
        return $this->id;
117
    }
118
119
    /**
120
     * @return string
121
     */
122 2
    public function getOrigPath(): string
123
    {
124 2
        return $this->origPath;
125
    }
126
127 24
    private function setId(): void
128
    {
129 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

129
        $this->id = md5(/** @scrutinizer ignore-type */ $this->path);
Loading history...
130 24
    }
131
132
133
}