Issues (169)

src/Libraries/Asset/Asset.php (2 issues)

1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.0
13
 *
14
 */
15
16
namespace Quantum\Libraries\Asset;
17
18
/**
19
 * Class Asset
20
 * @package Quantum\Libraries\Asset
21
 */
22
class Asset
23
{
24
25
    /**
26
     * Type CSS
27
     */
28
    const CSS = 1;
29
30
    /**
31
     * Type JS
32
     */
33
    const JS = 2;
34
35
    /**
36
     * @var int
37
     */
38
    private $type;
39
40
    /**
41
     * @var string
42
     */
43
    private $path;
44
45
    /**
46
     * @var string|null
47
     */
48
    private $name;
49
50
    /**
51
     * @var array|null
52
     */
53
    private $attributes = [];
54
55
    /**
56
     * @var int
57
     */
58
    private $position;
59
60
    /**
61
     * Asset templates
62
     * @var string[]
63
     */
64
    private $templates = [
65
        self::CSS => '<link rel="stylesheet" type="text/css" href="{%1}">',
66
        self::JS => '<script src="{%1}" {%2}></script>',
67
    ];
68
69
    /**
70
     * Asset constructor
71
     * @param int $type
72
     * @param string $path
73
     * @param string|null $name
74
     * @param int|null $position
75
     * @param array|null $attributes
76
     */
77
    public function __construct(int $type, string $path, ?string $name = null, ?int $position = -1, ?array $attributes = [])
78
    {
79
        $this->type = $type;
80
        $this->path = $path;
81
        $this->name = $name;
82
        $this->position = $position;
83
        $this->attributes = $attributes;
84
    }
85
86
    /**
87
     * Gets asset type
88
     * @return int
89
     */
90
    public function getType(): int
91
    {
92
        return $this->type;
93
    }
94
95
    /**
96
     * Gets asset path
97
     * @return string
98
     */
99
    public function getPath(): string
100
    {
101
        return $this->path;
102
    }
103
104
    /**
105
     * Gets asset name
106
     * @return string|null
107
     */
108
    public function getName(): ?string
109
    {
110
        return $this->name;
111
    }
112
113
    /**
114
     * Gets asset position
115
     * @return int|null
116
     */
117
    public function getPosition(): ?int
118
    {
119
        return $this->position;
120
    }
121
122
    /**
123
     * Gets asset attributes
124
     * @return array
125
     */
126
    public function getAttributes(): array
127
    {
128
        return $this->attributes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->attributes could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
129
    }
130
131
    /**
132
     * Gets asset url
133
     * @return string
134
     */
135
    public function url(): string
136
    {
137
        if (!parse_url($this->path, PHP_URL_HOST)) {
138
            return base_url() . '/assets/' . $this->path;
139
        }
140
141
        return $this->path;
142
    }
143
144
    /**
145
     * Renders asset tag
146
     * @return string
147
     */
148
    public function tag(): string
149
    {
150
        return _message(
151
            $this->templates[$this->type],
152
            [$this->url(), implode(' ', $this->attributes)]
0 ignored issues
show
It seems like $this->attributes can also be of type null; however, parameter $pieces of implode() does only seem to accept array, 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

152
            [$this->url(), implode(' ', /** @scrutinizer ignore-type */ $this->attributes)]
Loading history...
153
        );
154
    }
155
}