Completed
Push — master ( f8c34a...03d8b8 )
by Denis
02:13
created

src/Manager.php (2 issues)

Labels
Severity

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\Utils\Arr;
20
21
/**
22
 * Class Manager
23
 *
24
 * @package JBZoo\Assets
25
 */
26
class Manager
27
{
28
    /**
29
     * @var Factory
30
     */
31
    protected $_factory;
32
33
    /**
34
     * @var Collection
35
     */
36
    protected $_collection;
37
38
    /**
39
     * @var array
40
     */
41
    protected $_queued = [];
42
43
    /**
44
     * Manager constructor.
45
     *
46
     * @param Factory $factory
47
     */
48
    public function __construct(Factory $factory)
49
    {
50
        $this->_factory    = $factory;
51
        $this->_collection = new Collection();
52
    }
53
54
    /**
55
     * Adds a registered asset or a new asset to the queue.
56
     *
57
     * @param string       $name
58
     * @param string|null  $source
59
     * @param string|array $dependencies
60
     * @param string|array $options
61
     * @return $this
62
     */
63
    public function add($name, $source = null, $dependencies = [], $options = [])
64
    {
65
        if ($source !== null) {
66
            $asset = $this->_factory->create($name, $source, $dependencies, $options);
67
            $this->_collection->add($asset);
68
        } else {
69
            $asset = $this->_collection->get($name);
70
        }
71
72
        if ($asset) {
73
            $this->_queued[$name] = true;
74
        }
75
76
        return $this;
77
    }
78
79
    /**
80
     * Registers an asset.
81
     *
82
     * @param string       $name
83
     * @param null|string  $source
84
     * @param array        $dependencies
85
     * @param string|array $options
86
     * @return $this
87
     */
88
    public function register($name, $source = null, $dependencies = [], $options = [])
89
    {
90
        $this->_collection->add($this->_factory->create($name, $source, $dependencies, $options));
91
        return $this;
92
    }
93
94
    /**
95
     * Removes an asset from the queue.
96
     *
97
     * @param string $name
98
     * @return $this
99
     */
100
    public function remove($name)
101
    {
102
        unset($this->_queued[$name]);
103
        return $this;
104
    }
105
106
    /**
107
     * Unregisters an asset from collection.
108
     *
109
     * @param string $name
110
     * @return $this
111
     */
112
    public function unRegister($name)
113
    {
114
        $this->_collection->remove($name);
115
        $this->remove($name);
116
        return $this;
117
    }
118
119
    /**
120
     * Get asset collections.
121
     *
122
     * @return Collection
123
     */
124
    public function collection()
125
    {
126
        return $this->_collection;
127
    }
128
129
    /**
130
     * Resolves asset dependencies.
131
     *
132
     * @param Asset|null $asset
133
     * @param Asset[]    $resolved
134
     * @param Asset[]    $unresolved
135
     * @return Asset[]
136
     */
137
    public function resolveDependencies(Asset $asset, &$resolved = [], &$unresolved = [])
138
    {
139
        $unresolved[$asset->getName()] = $asset;
140
141
        foreach ($asset->getDependencies() as $dependency) {
142
            if (!Arr::key($dependency, $resolved)) {
143
144
                if (isset($unresolved[$dependency])) {
145
                    throw new \RuntimeException(sprintf(
146
                        'Circular asset dependency "%s > %s" detected.',
147
                        $asset->getName(),
148
                        $dependency
149
                    ));
150
                }
151
152
                if ($dep = $this->_collection->get($dependency)) {
0 ignored issues
show
Are you sure the assignment to $dep is correct as $this->_collection->get($dependency) (which targets JBZoo\Assets\Collection::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
153
                    $this->resolveDependencies($dep, $resolved, $unresolved);
154
                }
155
            }
156
        }
157
158
        $resolved[$asset->getName()] = $asset;
159
        unset($unresolved[$asset->getName()]);
160
161
        return $resolved;
162
    }
163
164
    /**
165
     * Build assets.
166
     *
167
     * @param array $filters
168
     * @return array
169
     */
170
    public function build(array $filters = [])
171
    {
172
        $assets = [];
173
        $output = ['css' => [], 'js' => []];
174
        foreach (array_keys($this->_queued) as $name) {
175
            $this->resolveDependencies($this->_collection->get($name), $assets);
0 ignored issues
show
It seems like $this->_collection->get($name) 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...
176
        }
177
178
        $collection = new Collection($assets);
179
        $assets     = $collection->getAssets();
180
181
        /** @var Asset $asset */
182
        foreach ($assets as $asset) {
183
            list($type, $source) = $asset->load($filters);
184
            if ($source && !Arr::in($source, (array)$output[$type])) {
185
                $output[$type][] = $source;
186
            }
187
        }
188
189
        return $output;
190
    }
191
}
192