Passed
Push — master ( 0a6b04...2ff664 )
by Smoren
01:53
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\Interfaces\ArrayViewInterface;
8
use Smoren\ArrayView\Interfaces\IndexListSelectorInterface;
9
use Smoren\ArrayView\Views\ArrayIndexListView;
10
11
/**
12
 * Represents an index list selector that selects elements based on the provided array of indexes.
13
 */
14
final class IndexListSelector implements IndexListSelectorInterface
15
{
16
    /**
17
     * @var array<int> The array of indexes to select elements from.
18
     */
19
    private array $value;
20
21
    /**
22
     * Creates a new IndexListSelector instance with the provided array of indexes.
23
     *
24
     * @param array<int>|ArrayViewInterface<int> $value The array of indexes or array view containing indexes.
25
     */
26
    public function __construct($value)
27
    {
28
        $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...
29
    }
30
31
    /**
32
     * Selects elements from the source array based on the index list.
33
     *
34
     * @template T The type of elements in the source array view.
35
     *
36
     * @param ArrayViewInterface<T> $source The source array view to select elements from.
37
     * @param bool|null $readonly Whether the selection should be read-only.
38
     *
39
     * @return ArrayIndexListView<T> The view containing the selected elements.
40
     *
41
     * {@inheritDoc}
42
     */
43
    public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayIndexListView
44
    {
45
        return new ArrayIndexListView($source, $this->value, $readonly ?? $source->isReadonly());
46
    }
47
48
    /**
49
     * Checks if the selector is compatible with the given view.
50
     *
51
     * @template T View elements type.
52
     *
53
     * @param ArrayViewInterface<T> $view the view to check compatibility with.
54
     *
55
     * @return bool true if the element is compatible, false otherwise
56
     *
57
     * {@inheritDoc}
58
     */
59
    public function compatibleWith(ArrayViewInterface $view): bool
60
    {
61
        return \count($this->value) === 0 || \max($this->value) < \count($view) && \min($this->value) >= -\count($view);
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function getValue(): array
68
    {
69
        return $this->value;
70
    }
71
}
72