Passed
Push — master ( 9df60a...0fb430 )
by Smoren
02:58 queued 01:09
created

ArrayViewAccessTrait   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 21
eloc 35
c 1
b 0
f 1
dl 0
loc 108
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B offsetGet() 0 21 7
A offsetExists() 0 15 5
B offsetSet() 0 30 8
A offsetUnset() 0 3 1
1
<?php
2
3
namespace Smoren\ArrayView\Traits;
4
5
use Smoren\ArrayView\Exceptions\IndexError;
6
use Smoren\ArrayView\Exceptions\KeyError;
7
use Smoren\ArrayView\Exceptions\NotSupportedError;
8
use Smoren\ArrayView\Exceptions\ReadonlyError;
9
use Smoren\ArrayView\Interfaces\ArraySelectorInterface;
10
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
11
use Smoren\ArrayView\Selectors\SliceSelector;
12
use Smoren\ArrayView\Structs\Slice;
13
14
/**
15
 * @template T
16
 */
17
trait ArrayViewAccessTrait
18
{
19
    /**
20
     * @param numeric|string|ArraySelectorInterface $offset
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...
21
     *
22
     * @return bool
23
     *
24
     * {@inheritDoc}
25
     */
26
    public function offsetExists($offset): bool
27
    {
28
        if (\is_numeric($offset)) {
29
            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

29
            return $this->/** @scrutinizer ignore-call */ numericOffsetExists($offset);
Loading history...
30
        }
31
32
        if (\is_string($offset) && Slice::isSlice($offset)) {
33
            return true;
34
        }
35
36
        if ($offset instanceof ArraySelectorInterface) {
37
            return true;
38
        }
39
40
        return false;
41
    }
42
43
    /**
44
     * @param numeric|string|ArraySelectorInterface $offset
45
     *
46
     * @return T|array<T>
47
     *
48
     * {@inheritDoc}
49
     */
50
    #[\ReturnTypeWillChange]
51
    public function offsetGet($offset)
52
    {
53
        /** @var mixed $offset */
54
        if (\is_numeric($offset)) {
55
            if (!$this->numericOffsetExists($offset)) {
56
                throw new IndexError("Index {$offset} is out of range.");
57
            }
58
            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

58
            return $this->source[$this->/** @scrutinizer ignore-call */ convertIndex(\intval($offset))];
Loading history...
59
        }
60
61
        if (\is_string($offset) && Slice::isSlice($offset)) {
62
            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

62
            return $this->/** @scrutinizer ignore-call */ subview(new SliceSelector($offset))->toArray();
Loading history...
63
        }
64
65
        if ($offset instanceof ArraySelectorInterface) {
66
            return $this->subview($offset)->toArray();
67
        }
68
69
        $strOffset = \is_scalar($offset) ? \strval($offset) : \gettype($offset);
70
        throw new KeyError("Invalid key: \"{$strOffset}\".");
71
    }
72
73
    /**
74
     * @param numeric|string|ArraySelectorInterface $offset
75
     * @param T|array<T>|ArrayViewInterface<T> $value
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...
76
     *
77
     * @return void
78
     *
79
     * {@inheritDoc}
80
     */
81
    public function offsetSet($offset, $value): void
82
    {
83
        /** @var mixed $offset */
84
        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

84
        if ($this->/** @scrutinizer ignore-call */ isReadonly()) {
Loading history...
85
            throw new ReadonlyError("Cannot modify a readonly view.");
86
        }
87
88
        if (\is_numeric($offset)) {
89
            if (!$this->numericOffsetExists($offset)) {
90
                throw new IndexError("Index {$offset} is out of range.");
91
            }
92
93
            // @phpstan-ignore-next-line
94
            $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...
95
            return;
96
        }
97
98
        if (\is_string($offset) && Slice::isSlice($offset)) {
99
            /** @var array<T>|ArrayViewInterface<T> $value */
100
            $this->subview(new SliceSelector($offset))->set($value);
101
            return;
102
        }
103
104
        if ($offset instanceof ArraySelectorInterface) {
105
            $this->subview($offset)->set($value);
106
            return;
107
        }
108
109
        $strOffset = \is_scalar($offset) ? \strval($offset) : \gettype($offset);
110
        throw new KeyError("Invalid key: \"{$strOffset}\".");
111
    }
112
113
    /**
114
     * @param numeric|string|ArraySelectorInterface $offset
115
     *
116
     * @return void
117
     *
118
     * @throws NotSupportedError
119
     *
120
     * {@inheritDoc}
121
     */
122
    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

122
    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...
123
    {
124
        throw new NotSupportedError();
125
    }
126
}
127