IndexListSelector   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 8
c 1
b 0
f 0
dl 0
loc 60
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A getValue() 0 3 1
A compatibleWith() 0 3 3
A select() 0 7 2
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
 * ```php
16
 * $originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
17
 * $view = ArrayView::toView($originalArray);
18
 *
19
 * $selector = new IndexListSelector([0, 2, 4]);
20
 * print_r($view[$selector]); // [1, 3, 5]
21
 * print_r($view->subview($selector)->toArray()); // [1, 3, 5]
22
 * ```
23
 */
24
final class IndexListSelector implements IndexListSelectorInterface
25
{
26
    /**
27
     * @var array<int> The array of indexes to select elements from.
28
     */
29
    private array $value;
30
31
    /**
32
     * Creates a new IndexListSelector instance with the provided array of indexes.
33
     *
34
     * @param array<int>|ArrayViewInterface<int> $value The array of indexes or array view containing indexes.
35
     */
36
    public function __construct($value)
37
    {
38
        $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...
39
    }
40
41
    /**
42
     * Selects elements from the source array based on the index list.
43
     *
44
     * @template T The type of elements in the source array view.
45
     *
46
     * @param ArrayViewInterface<T> $source The source array view to select elements from.
47
     * @param bool|null $readonly Whether the selection should be read-only.
48
     *
49
     * @return ArrayIndexListView<T> The view containing the selected elements.
50
     *
51
     * {@inheritDoc}
52
     */
53
    public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayIndexListView
54
    {
55
        if (!$this->compatibleWith($source)) {
56
            throw new IndexError('Some indexes are out of range.');
57
        }
58
59
        return new ArrayIndexListView($source, $this->value, $readonly ?? $source->isReadonly());
60
    }
61
62
    /**
63
     * Checks if the selector is compatible with the given view.
64
     *
65
     * @template T View elements type.
66
     *
67
     * @param ArrayViewInterface<T> $view the view to check compatibility with.
68
     *
69
     * @return bool true if the element is compatible, false otherwise
70
     *
71
     * {@inheritDoc}
72
     */
73
    public function compatibleWith(ArrayViewInterface $view): bool
74
    {
75
        return \count($this->value) === 0 || \max($this->value) < \count($view) && \min($this->value) >= -\count($view);
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    public function getValue(): array
82
    {
83
        return $this->value;
84
    }
85
}
86