Passed
Pull Request — master (#23)
by Smoren
02:06
created

IndexListSelector::compatibleWith()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\ArrayView\Selectors;
6
7
use Smoren\ArrayView\Exceptions\IndexError;
8
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
9
use Smoren\ArrayView\Interfaces\IndexListSelectorInterface;
10
use Smoren\ArrayView\Views\ArrayIndexListView;
11
12
/**
13
 * Represents an index list selector that selects elements based on the provided array of indexes.
14
 */
15
final class IndexListSelector implements IndexListSelectorInterface
16
{
17
    /**
18
     * @var array<int> The array of indexes to select elements from.
19
     */
20
    private array $value;
21
22
    /**
23
     * Creates a new IndexListSelector instance with the provided array of indexes.
24
     *
25
     * @param array<int>|ArrayViewInterface<int> $value The array of indexes or array view containing indexes.
26
     */
27
    public function __construct($value)
28
    {
29
        $this->value = \is_array($value) ? $value : $value->toArray();
0 ignored issues
show
Documentation Bug introduced by
is_array($value) ? $value : $value->toArray() is of type array<mixed,Smoren\ArrayView\Interfaces\T|mixed>, but the property $value was declared to be of type integer[]. 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...
30
    }
31
32
    /**
33
     * Selects elements from the source array based on the index list.
34
     *
35
     * @template T The type of elements in the source array view.
36
     *
37
     * @param ArrayViewInterface<T> $source The source array view to select elements from.
38
     * @param bool|null $readonly Whether the selection should be read-only.
39
     *
40
     * @return ArrayIndexListView<T> The view containing the selected elements.
41
     *
42
     * {@inheritDoc}
43
     */
44
    public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayIndexListView
45
    {
46
        if (!$this->compatibleWith($source)) {
47
            throw new IndexError('Some indexes are out of range.');
48
        }
49
50
        return new ArrayIndexListView($source, $this->value, $readonly ?? $source->isReadonly());
51
    }
52
53
    /**
54
     * Checks if the selector is compatible with the given view.
55
     *
56
     * @template T View elements type.
57
     *
58
     * @param ArrayViewInterface<T> $view the view to check compatibility with.
59
     *
60
     * @return bool true if the element is compatible, false otherwise
61
     *
62
     * {@inheritDoc}
63
     */
64
    public function compatibleWith(ArrayViewInterface $view): bool
65
    {
66
        return \count($this->value) === 0 || \max($this->value) < \count($view) && \min($this->value) >= -\count($view);
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72
    public function getValue(): array
73
    {
74
        return $this->value;
75
    }
76
}
77