Passed
Push — master ( 9fff06...1ba6fb )
by Smoren
02:33 queued 16s
created

ArrayViewAccessTrait::offsetExists()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
c 1
b 0
f 1
dl 0
loc 15
rs 9.6111
cc 5
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\ArrayView\Traits;
6
7
use Smoren\ArrayView\Exceptions\IndexError;
8
use Smoren\ArrayView\Exceptions\KeyError;
9
use Smoren\ArrayView\Exceptions\NotSupportedError;
10
use Smoren\ArrayView\Exceptions\ReadonlyError;
11
use Smoren\ArrayView\Interfaces\ArraySelectorInterface;
12
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
13
use Smoren\ArrayView\Selectors\SliceSelector;
14
use Smoren\ArrayView\Structs\Slice;
15
16
/**
17
 * Trait providing methods for accessing elements in ArrayView object.
18
 * The trait implements methods for accessing, retrieving, setting,
19
 * and unsetting elements in the ArrayView object.
20
 *
21
 * @template T Type of ArrayView values.
22
 */
23
trait ArrayViewAccessTrait
24
{
25
    /**
26
     * Check if the specified offset exists in the ArrayView object.
27
     *
28
     * @param numeric|string|ArraySelectorInterface $offset The offset to check.
0 ignored issues
show
Bug introduced by
The type Smoren\ArrayView\Traits\numeric was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
     *
30
     * @return bool
31
     *
32
     * {@inheritDoc}
33
     */
34
    public function offsetExists($offset): bool
35
    {
36
        if (\is_numeric($offset)) {
37
            return $this->numericOffsetExists($offset);
0 ignored issues
show
Bug introduced by
It seems like numericOffsetExists() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            return $this->/** @scrutinizer ignore-call */ numericOffsetExists($offset);
Loading history...
38
        }
39
40
        if (\is_string($offset) && Slice::isSlice($offset)) {
41
            return true;
42
        }
43
44
        if ($offset instanceof ArraySelectorInterface) {
45
            return $offset->compatibleWith($this);
0 ignored issues
show
Bug introduced by
$this of type Smoren\ArrayView\Traits\ArrayViewAccessTrait is incompatible with the type Smoren\ArrayView\Interfaces\ArrayViewInterface expected by parameter $view of Smoren\ArrayView\Interfa...rface::compatibleWith(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
            return $offset->compatibleWith(/** @scrutinizer ignore-type */ $this);
Loading history...
46
        }
47
48
        return false;
49
    }
50
51
    /**
52
     * Get the value at the specified offset in the ArrayView object.
53
     *
54
     * @param numeric|string|ArraySelectorInterface $offset The offset to get the value from.
55
     *
56
     * @return T|array<T> The value at the specified offset.
57
     *
58
     * @throws IndexError if the offset is out of range.
59
     * @throws KeyError if the key is invalid.
60
     *
61
     * {@inheritDoc}
62
     */
63
    #[\ReturnTypeWillChange]
64
    public function offsetGet($offset)
65
    {
66
        /** @var mixed $offset */
67
        if (\is_numeric($offset)) {
68
            if (!$this->numericOffsetExists($offset)) {
69
                throw new IndexError("Index {$offset} is out of range.");
70
            }
71
            return $this->source[$this->convertIndex(\intval($offset))];
0 ignored issues
show
Bug introduced by
It seems like convertIndex() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
            return $this->source[$this->/** @scrutinizer ignore-call */ convertIndex(\intval($offset))];
Loading history...
72
        }
73
74
        if (\is_string($offset) && Slice::isSlice($offset)) {
75
            return $this->subview(new SliceSelector($offset))->toArray();
0 ignored issues
show
Bug introduced by
It seems like subview() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            return $this->/** @scrutinizer ignore-call */ subview(new SliceSelector($offset))->toArray();
Loading history...
76
        }
77
78
        if ($offset instanceof ArraySelectorInterface) {
79
            return $this->subview($offset)->toArray();
80
        }
81
82
        $strOffset = \is_scalar($offset) ? \strval($offset) : \gettype($offset);
83
        throw new KeyError("Invalid key: \"{$strOffset}\".");
84
    }
85
86
    /**
87
     * Set the value at the specified offset in the ArrayView object.
88
     *
89
     * @param numeric|string|ArraySelectorInterface $offset The offset to set the value at.
90
     * @param T|array<T>|ArrayViewInterface<T> $value The value to set.
0 ignored issues
show
Bug introduced by
The type Smoren\ArrayView\Traits\T was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
91
     *
92
     * @return void
93
     *
94
     * @throws IndexError if the offset is out of range.
95
     * @throws KeyError if the key is invalid.
96
     * @throws ReadonlyError if the object is readonly.
97
     *
98
     * {@inheritDoc}
99
     */
100
    public function offsetSet($offset, $value): void
101
    {
102
        /** @var mixed $offset */
103
        if ($this->isReadonly()) {
0 ignored issues
show
Bug introduced by
It seems like isReadonly() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        if ($this->/** @scrutinizer ignore-call */ isReadonly()) {
Loading history...
104
            throw new ReadonlyError("Cannot modify a readonly view.");
105
        }
106
107
        if (\is_numeric($offset)) {
108
            if (!$this->numericOffsetExists($offset)) {
109
                throw new IndexError("Index {$offset} is out of range.");
110
            }
111
112
            // @phpstan-ignore-next-line
113
            $this->source[$this->convertIndex(\intval($offset))] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property source does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
114
            return;
115
        }
116
117
        if (\is_string($offset) && Slice::isSlice($offset)) {
118
            /** @var array<T>|ArrayViewInterface<T> $value */
119
            $this->subview(new SliceSelector($offset))->set($value);
120
            return;
121
        }
122
123
        if ($offset instanceof ArraySelectorInterface) {
124
            $this->subview($offset)->set($value);
125
            return;
126
        }
127
128
        $strOffset = \is_scalar($offset) ? \strval($offset) : \gettype($offset);
129
        throw new KeyError("Invalid key: \"{$strOffset}\".");
130
    }
131
132
    /**
133
     * Unset the value at the specified offset in the array-like object.
134
     *
135
     * @param numeric|string|ArraySelectorInterface $offset The offset to unset the value at.
136
     *
137
     * @return void
138
     *
139
     * @throws NotSupportedError always.
140
     *
141
     * {@inheritDoc}
142
     */
143
    public function offsetUnset($offset): void
0 ignored issues
show
Unused Code introduced by
The parameter $offset is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

143
    public function offsetUnset(/** @scrutinizer ignore-unused */ $offset): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
144
    {
145
        throw new NotSupportedError();
146
    }
147
}
148