Passed
Push — master ( ac1451...1d2831 )
by Alan
02:29 queued 11s
created

ArrayAccessData   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetGet() 0 3 1
A getIterator() 0 3 1
A offsetExists() 0 3 1
A offsetUnset() 0 3 1
A toArray() 0 3 1
A offsetSet() 0 3 1
1
<?php
2
3
namespace FigTree\Config\Concerns;
4
5
use ArrayIterator;
6
use Traversable;
7
use FigTree\Config\Exceptions\{
8
	ReadOnlyException,
9
};
10
11
trait ArrayAccessData
12
{
13
	protected array $data = [];
14
15
	/**
16
	 * Magic method to handle key_exists/isset checks of an array value on the object.
17
	 *
18
	 * @param string|int $offset
19
	 *
20
	 * @return boolean
21
	 */
22
	public function offsetExists($offset): bool
23
	{
24
		return key_exists($offset, $this->data);
25
	}
26
27
	/**
28
	 * Magic method to handle retrieval of an array value on the object.
29
	 *
30
	 * @param string|int $offset
31
	 *
32
	 * @return mixed
33
	 */
34
	public function offsetGet($offset)
35
	{
36
		return $this->data[$offset] ?? null;
37
	}
38
39
	/**
40
	 * Magic method to handle modification of an array value on the object.
41
	 *
42
	 * @param string|int $offset
43
	 * @param mixed $value
44
	 *
45
	 * @return void
46
	 *
47
	 * @throws \FigTree\Config\Exceptions\ReadOnlyException
48
	 */
49
	public function offsetSet($offset, $value): void
0 ignored issues
show
Unused Code introduced by
The parameter $value 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

49
	public function offsetSet($offset, /** @scrutinizer ignore-unused */ $value): 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...
50
	{
51
		throw new ReadOnlyException($offset);
52
	}
53
54
	/**
55
	 * Magic method to handle removal of an array value on the object.
56
	 *
57
	 * @param string|int $offset
58
	 *
59
	 * @return void
60
	 *
61
	 * @throws \FigTree\Config\Exceptions\ReadOnlyException
62
	 */
63
	public function offsetUnset($offset): void
64
	{
65
		throw new ReadOnlyException($offset);
66
	}
67
68
	/**
69
	 * Convert the object into an array.
70
	 *
71
	 * @return array
72
	 */
73
	public function toArray(): array
74
	{
75
		return $this->data;
76
	}
77
78
	/**
79
	 * Retrieve an external iterator.
80
	 *
81
	 * @return \Traversable
82
	 */
83
	public function getIterator(): Traversable
84
	{
85
		return new ArrayIterator($this->data);
86
	}
87
}
88