FixedTypedCollection::offsetGet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Linio\Collection;
6
7
use Doctrine\Common\Collections\Criteria;
8
use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
9
10
abstract class FixedTypedCollection extends TypedCollection
11
{
12
    /**
13
     * @var int
14
     */
15
    protected $size;
16
17
    /**
18
     * @param int   $size
19
     * @param array $elements
20
     */
21
    public function __construct($size, array $elements = [])
22
    {
23
        $this->size = (int) $size;
24
25
        parent::__construct($elements);
26
27
        if ($this->count() > $this->size) {
28
            throw new \InvalidArgumentException('Index invalid or out of range');
29
        }
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function offsetSet($offset, $value)
36
    {
37
        $this->validateSize();
38
        parent::offsetSet($offset, $value);
39
    }
40
41
    /**
42
     * @param int|string $key
43
     *
44
     * @throws \InvalidArgumentException
45
     *
46
     * @return mixed|null
47
     */
48
    public function get($key)
49
    {
50
        if (!$this->containsKey($key)) {
51
            throw new \InvalidArgumentException('Index invalid or out of range');
52
        }
53
54
        return parent::get($key);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function offsetGet($offset)
61
    {
62
        return $this->get($offset);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function add($value)
69
    {
70
        $this->validateSize();
71
        parent::add($value);
72
    }
73
74
    /**
75
     * @throws \InvalidArgumentException
76
     */
77
    protected function validateSize()
78
    {
79
        if ($this->count() >= $this->size) {
80
            throw new \InvalidArgumentException('Index invalid or out of range');
81
        }
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function matching(Criteria $criteria)
88
    {
89
        $expr = $criteria->getWhereExpression();
90
        $filtered = $this->toArray();
91
92
        if ($expr) {
93
            $visitor = new ClosureExpressionVisitor();
94
            $filter = $visitor->dispatch($expr);
95
            $filtered = array_filter($filtered, $filter);
96
        }
97
98
        if ($orderings = $criteria->getOrderings()) {
99
            $next = null;
100
            foreach (array_reverse($orderings) as $field => $ordering) {
101
                $next = ClosureExpressionVisitor::sortByField($field, $ordering == 'DESC' ? -1 : 1, $next);
102
            }
103
104
            usort($filtered, $next);
105
        }
106
107
        $offset = $criteria->getFirstResult();
108
        $length = $criteria->getMaxResults();
109
110
        if ($offset || $length) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $offset of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $length of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
111
            $filtered = array_slice($filtered, (int) $offset, $length);
112
        }
113
114
        return new static(count($filtered), $filtered);
115
    }
116
}
117