Passed
Push — master ( 550b45...c1fb0d )
by Enjoys
01:45
created

Asset::isMinify()   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
        $projectDir = '';
52 12
        if (isset($_ENV['ASSETS_PROJECT_DIRECTORY'])) {
53
            $projectDir = $_ENV['ASSETS_PROJECT_DIRECTORY'] . '/';
54
        }
55
        $paths = [
56 12
            $this->path,
57 12
            $projectDir . $this->path
58
        ];
59
60 12
        foreach ($paths as $path) {
61 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

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

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

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

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