Passed
Pull Request — master (#5)
by Ivan
01:48
created

OrderedContainer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 45
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getItems() 0 19 4
A get() 0 4 1
A ascending() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\Navigation;
6
7
use Everlution\Navigation\Item\ItemInterface;
8
use Everlution\Navigation\Item\SortableInterface;
9
10
/**
11
 * Class FilteredContainer.
12
 *
13
 * @author Ivan Barlog <[email protected]>
14
 */
15
class OrderedContainer implements ContainerInterface
16
{
17
    /** @var FilteredContainerInterface */
18
    private $container;
19
    /** @var ItemInterface[] */
20
    private $items;
21
22
    public function __construct(ContainerInterface $container)
23
    {
24
        $this->container = $container;
0 ignored issues
show
Documentation Bug introduced by
$container is of type object<Everlution\Navigation\ContainerInterface>, but the property $container was declared to be of type object<Everlution\Naviga...eredContainerInterface>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
25
    }
26
27
    /**
28
     * @return ItemInterface[]
29
     */
30
    public function getItems(): array
31
    {
32
        if (!$this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type Everlution\Navigation\Item\ItemInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
33
            $items = $this->container->getItems();
34
35
            $sortable = [];
36
            foreach ($items as $key => $item) {
37
                if ($item instanceof SortableInterface) {
38
                    $sortable[$key] = $item;
39
                    unset($items[$key]);
40
                }
41
            }
42
43
            uasort($sortable, [$this, 'ascending']);
44
            $this->items = array_merge($sortable, $items);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($sortable, $items) of type array is incompatible with the declared type array<integer,object<Eve...on\Item\ItemInterface>> of property $items.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
        }
46
47
        return $this->items;
48
    }
49
50
    public function get(string $name): ItemInterface
51
    {
52
        return $this->items[$name];
53
    }
54
55
    private function ascending(SortableInterface $first, SortableInterface $second)
56
    {
57
        return $first->getOrder() <=> $second->getOrder();
58
    }
59
}
60