Issues (6)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Manager.php (2 issues)

Labels

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * JBZoo Assets
4
 *
5
 * This file is part of the JBZoo CCK package.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Assets
10
 * @license   MIT
11
 * @copyright Copyright (C) JBZoo.com,  All rights reserved.
12
 * @link      https://github.com/JBZoo/Assets
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace JBZoo\Assets;
17
18
use JBZoo\Assets\Asset\Asset;
19
use JBZoo\Data\Data;
20
use JBZoo\Path\Path;
21
use JBZoo\Utils\Arr;
22
23
/**
24
 * Class Manager
25
 * @package JBZoo\Assets
26
 */
27
class Manager
28
{
29
    /**
30
     * @var Factory
31
     */
32
    protected $_factory;
33
34
    /**
35
     * @var Collection
36
     */
37
    protected $_collection;
38
39
    /**
40
     * @var array
41
     */
42
    protected $_queued = [];
43
44
    /**
45
     * @var Path
46
     */
47
    protected $_path;
48
49
    /**
50
     * @var array
51
     */
52
    protected $_default = [
53
        'debug'       => false,
54
        'strict_mode' => false,
55
        'less'        => [],
56
    ];
57
58
    /**
59
     * Manager constructor.
60
     *
61
     * @param Path  $path
62
     * @param array $params
63
     */
64
    public function __construct(Path $path, $params = [])
65
    {
66
        $this->_params = new Data(array_merge($this->_default, $params));
0 ignored issues
show
The property _params does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67
68
        $this->_path = $path;
69
        $this->_factory = new Factory($this);
70
        $this->_collection = new Collection();
71
    }
72
73
    /**
74
     * @return Data
75
     */
76
    public function getParams()
77
    {
78
        return $this->_params;
79
    }
80
81
    /**
82
     * @param string $key
83
     * @param mixed  $value
84
     */
85
    public function setParam($key, $value)
86
    {
87
        $this->_params->set($key, $value);
88
    }
89
90
    /**
91
     * @return Path
92
     */
93
    public function getPath()
94
    {
95
        return $this->_path;
96
    }
97
98
    /**
99
     * Adds a registered asset or a new asset to the queue.
100
     *
101
     * @param string       $alias
102
     * @param string|null  $source
103
     * @param string|array $dependencies
104
     * @param string|array $options
105
     * @return $this
106
     */
107
    public function add($alias, $source = null, $dependencies = [], $options = [])
108
    {
109
        if ($source !== null) {
110
            $asset = $this->_factory->create($alias, $source, $dependencies, $options);
111
            $this->_collection->add($asset);
112
        }
113
114
        $this->_queued[$alias] = true;
115
116
        return $this;
117
    }
118
119
    /**
120
     * Removes an asset from the queue.
121
     *
122
     * @param string $alias
123
     * @return $this
124
     */
125
    public function remove($alias)
126
    {
127
        unset($this->_queued[$alias]);
128
        return $this;
129
    }
130
131
    /**
132
     * Registers an asset.
133
     *
134
     * @param string       $alias
135
     * @param null|string  $source
136
     * @param array        $dependencies
137
     * @param string|array $options
138
     * @return $this
139
     */
140
    public function register($alias, $source = null, $dependencies = [], $options = [])
141
    {
142
        $asset = $this->_factory->create($alias, $source, $dependencies, $options);
143
        $this->_collection->add($asset);
144
145
        return $this;
146
    }
147
148
    /**
149
     * Unregisters an asset from collection.
150
     *
151
     * @param string $alias
152
     * @return $this
153
     */
154
    public function unregister($alias)
155
    {
156
        $this->_collection->remove($alias);
157
        $this->remove($alias);
158
        return $this;
159
    }
160
161
    /**
162
     * Get asset collections.
163
     *
164
     * @return Collection
165
     */
166
    public function getCollection()
167
    {
168
        return $this->_collection;
169
    }
170
171
    /**
172
     * Get asset collections.
173
     *
174
     * @return Factory
175
     */
176
    public function getFactory()
177
    {
178
        return $this->_factory;
179
    }
180
181
    /**
182
     * Build assets.
183
     *
184
     * @param array $filters
185
     * @return array
186
     */
187
    public function build(array $filters = [])
188
    {
189
        $assets = [];
190
        foreach (array_keys($this->_queued) as $alias) {
191
            $this->_resolveDependencies($this->_collection->get($alias), $assets);
0 ignored issues
show
It seems like $this->_collection->get($alias) can be null; however, _resolveDependencies() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
192
        }
193
194
        /** @var Asset $asset */
195
        $result = [
196
            Asset::TYPE_JS_FILE  => [],
197
            Asset::TYPE_JS_CODE  => [],
198
            Asset::TYPE_JSX_FILE => [],
199
            Asset::TYPE_JSX_CODE => [],
200
            Asset::TYPE_CSS_FILE => [],
201
            Asset::TYPE_CSS_CODE => [],
202
            Asset::TYPE_CALLBACK => [],
203
        ];
204
        foreach ($assets as $asset) {
205
206
            $source = $asset->load($filters);
207
208
            if (Asset::TYPE_COLLECTION === $source[0]) {
209
                $source = $source[1];
210
            } else {
211
                $source = [$source];
212
            }
213
214
            foreach ($source as $sourceItem) {
215
216
                $type = $sourceItem[0];
217
                $src = $sourceItem[1];
218
219
                if ($src && !Arr::in($src, $result[$type])) {
220
                    $result[$type][] = $src;
221
                }
222
            }
223
        }
224
225
        return $result;
226
    }
227
228
    /**
229
     * Resolves asset dependencies.
230
     *
231
     * @param Asset|null $asset
232
     * @param Asset[]    $resolved
233
     * @param Asset[]    $unresolved
234
     * @return Asset[]
235
     * @throws Exception
236
     */
237
    protected function _resolveDependencies(Asset $asset, &$resolved = [], &$unresolved = [])
238
    {
239
        $unresolved[$asset->getAlias()] = $asset;
240
241
        foreach ($asset->getDependencies() as $dependency) {
242
            if (!Arr::key($dependency, $resolved)) {
243
244
                if (isset($unresolved[$dependency])) {
245
                    throw new Exception(sprintf(
246
                        'Circular asset dependency "%s > %s" detected.',
247
                        $asset->getAlias(),
248
                        $dependency
249
                    ));
250
                }
251
252
                if ($dep = $this->_collection->get($dependency)) {
253
                    $this->_resolveDependencies($dep, $resolved, $unresolved);
254
                } else {
255
                    throw new Exception("Undefined depends: $dependency");
256
                }
257
            }
258
        }
259
260
        $resolved[$asset->getAlias()] = $asset;
261
        unset($unresolved[$asset->getAlias()]);
262
263
        return $resolved;
264
    }
265
}
266