Completed
Push — master ( 9218af...d5ea06 )
by Stéphane
13:04
created

WeightedAssetCollection::dump()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 Rocket\UI\Assets\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 9
    protected function flatten($asset)
68
    {
69 9
        if ($asset instanceof AssetCollection) {
70
            foreach ($asset->all() as $leaf) {
71
                $this->flatten($leaf);
72
            }
73
74
            return;
75
        }
76
77 9
        $weight = 0;
78 9
        if ($asset instanceof WeightedAsset) {
79 6
            $weight = $asset->getWeight();
80
        }
81
82 9
        if ($asset instanceof AssetReference) {
83 6
            $weight = $asset->getAsset()->getWeight();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Assetic\Asset\AssetInterface as the method getWeight() does only exist in the following implementations of said interface: Rocket\UI\Assets\Assetic\Asset\CssAsset, Rocket\UI\Assets\Assetic\Asset\JsAsset, Rocket\UI\Assets\Assetic\Asset\WeightedAsset.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
84
        }
85
 
86 9
        $this->orderable[$weight][] = $asset;
87 9
    }
88
}
89