Passed
Push — master ( c1fb0d...08f9f3 )
by Enjoys
01:45
created

Asset::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 9
ccs 7
cts 7
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 = $this->defineHttpScheme() . ':' . $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 15
    private function defineHttpScheme(): string
81
    {
82 15
        $scheme = 'http';
83 15
        if (isset($_SERVER['HTTP_SCHEME'])) {
84 1
            return $_SERVER['HTTP_SCHEME'];
85
        }
86
87 14
        if (isset($_SERVER['HTTPS']) && \strtolower($_SERVER['HTTPS']) != 'off') {
88 1
            return 'https';
89
        }
90
91 13
        if (isset($_SERVER['SERVER_PORT']) && 443 == $_SERVER['SERVER_PORT']) {
92 1
            return 'https';
93
        }
94 12
        return $scheme;
95
    }
96
97
98 10
    public function isMinify(): bool
99
    {
100 10
        return $this->getOption(self::PARAM_MINIFY, true);
101
    }
102
103
    /**
104
     * @return false|string
105
     */
106 25
    public function getPath()
107
    {
108 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...
109
    }
110
111 16
    public function getType(): string
112
    {
113 16
        return $this->type;
114
    }
115
116 27
    public function isUrl(): bool
117
    {
118 27
        return $this->isUrl;
119
    }
120
121
122
    /**
123
     * @return string|null
124
     */
125 14
    public function getId(): ?string
126
    {
127 14
        return $this->id;
128
    }
129
130
    /**
131
     * @return string
132
     */
133 2
    public function getOrigPath(): string
134
    {
135 2
        return $this->origPath;
136
    }
137
138 24
    private function setId(): void
139
    {
140 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

140
        $this->id = md5(/** @scrutinizer ignore-type */ $this->path);
Loading history...
141 24
    }
142
143
144
}