Completed
Push — master ( 2247f3...204e6d )
by Stéphane
01:33
created

WeightedAssetCollection   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
dl 0
loc 88
ccs 33
cts 44
cp 0.75
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 6 1
A dump() 0 6 1
A all() 0 5 1
A add() 0 6 1
A flattenAndOrderAssets() 0 25 5
A getAssetReferenceResolveMethod() 0 7 1
A flatten() 0 21 5
1
<?php namespace Rocket\UI\Assets\Assetic;
2
3
use Assetic\Asset\AssetCollection;
4
use Assetic\Asset\AssetInterface;
5
use Assetic\Filter\FilterInterface;
6
use Assetic\Asset\AssetReference;
7
use Rocket\UI\Assets\Assetic\Asset\WeightedAsset;
8
9
class WeightedAssetCollection extends AssetCollection
10
{
11
    protected $orderable = [];
12
    protected $flattened = false;
13
14
    public function load(FilterInterface $additionalFilter = null)
15
    {
16
        $this->flattenAndOrderAssets();
17
18
        parent::load($additionalFilter);
19
    }
20
21
    public function dump(FilterInterface $additionalFilter = null)
22
    {
23
        $this->flattenAndOrderAssets();
24
25
        return parent::dump($additionalFilter);
26
    }
27
28 9
    public function all()
29
    {
30 9
        $this->flattenAndOrderAssets();
31 9
        return parent::all();
32
    }
33
34 9
    public function add(AssetInterface $asset)
35
    {
36 9
        parent::add($asset);
37
38 9
        $this->flattened = false;
39 9
    }
40
41 9
    protected function flattenAndOrderAssets()
42
    {
43 9
        if ($this->flattened) {
44
            return;
45
        }
46
47
        //recursively get assets
48 9
        $original = parent::all();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (all() instead of flattenAndOrderAssets()). Are you sure this is correct? If so, you might want to change this to $this->all().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
49 9
        foreach ($original as $asset) {
50 9
            $this->flatten($asset);
51 9
            $this->removeLeaf($asset);
52
        }
53
54
        //order
55 9
        ksort($this->orderable);
56
57
        //flatten
58 9
        foreach ($this->orderable as $weight) {
59 9
            foreach ($weight as $item) {
60 9
                $this->add($item);
61
            }
62
        }
63
64 9
        $this->flattened = true;
65 9
    }
66
67 6
    protected function getAssetReferenceResolveMethod()
68
    {
69 6
        $class = new \ReflectionClass(AssetReference::class);
70 6
        $method = $class->getMethod("resolve");
71 6
        $method->setAccessible(true);
72 6
        return $method;
73
    }
74
75 9
    protected function flatten($asset)
76
    {
77 9
        if ($asset instanceof AssetCollection) {
78
            foreach ($asset->all() as $leaf) {
79
                $this->flatten($leaf);
80
            }
81
82
            return;
83
        }
84
85 9
        $weight = 0;
86 9
        if ($asset instanceof WeightedAsset) {
87 6
            $weight = $asset->getWeight();
88
        }
89
90 9
        if ($asset instanceof AssetReference) {
91 6
            $weight = $this->getAssetReferenceResolveMethod()->invokeArgs($asset, [])->getWeight();
92
        }
93
 
94 9
        $this->orderable[$weight][] = $asset;
95 9
    }
96
}
97