MaskSelector::getValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

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 1
nc 1
nop 0
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\MaskSelectorInterface;
9
use Smoren\ArrayView\Views\ArrayMaskView;
10
11
/**
12
 * Represents a mask selector that selects elements based on the provided array of boolean mask values.
13
 *
14
 * ```php
15
 * $originalArray = [1, 2, 3, 4, 5];
16
 * $view = ArrayView::toView($originalArray);
17
 *
18
 * $selector = new MaskSelector([true, false, true, false, true]);
19
 * print_r($view[$selector]); // [1, 3, 5]
20
 * print_r($view->subview($selector)->toArray()); // [1, 3, 5]
21
 * ```
22
 */
23
final class MaskSelector implements MaskSelectorInterface
24
{
25
    /**
26
     * @var array<bool> The array of boolean mask values to select elements based on.
27
     */
28
    private $value;
29
30
    /**
31
     * Creates a new MaskSelector instance with the provided array of boolean mask values.
32
     *
33
     * @param array<bool>|ArrayViewInterface<bool> $value The array or array view of boolean mask values.
34
     */
35
    public function __construct($value)
36
    {
37
        $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\Array...w\Interfaces\T|boolean>, but the property $value was declared to be of type boolean[]. 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...
38
    }
39
40
    /**
41
     * Selects elements from the source array based on the mask values.
42
     *
43
     * @template T The type of elements in the source array view.
44
     *
45
     * @param ArrayViewInterface<T> $source The source array to select elements from.
46
     * @param bool|null $readonly Whether the selection should be read-only.
47
     *
48
     * @return ArrayMaskView<T> The view containing the selected elements.
49
     *
50
     * {@inheritDoc}
51
     */
52
    public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayMaskView
53
    {
54
        return new ArrayMaskView($source, $this->value, $readonly ?? $source->isReadonly());
55
    }
56
57
    /**
58
     * Checks if the selector is compatible with the given view.
59
     *
60
     * @template T View elements type.
61
     *
62
     * @param ArrayViewInterface<T> $view the view to check compatibility with.
63
     *
64
     * @return bool true if the element is compatible, false otherwise
65
     *
66
     * {@inheritDoc}
67
     */
68
    public function compatibleWith(ArrayViewInterface $view): bool
69
    {
70
        return \count($this->value) === \count($view);
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function getValue(): array
77
    {
78
        return $this->value;
79
    }
80
}
81