Passed
Branch master (6982d9)
by Ivan
02:35
created

OrderedContainer::getItems()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 2
nop 0
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 Everlution\Navigation\ContainerInterface, but the property $container was declared to be of type Everlution\Navigation\FilteredContainerInterface. 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);
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