Completed
Pull Request — 1.0 (#1)
by Marc
01:54
created

CompositeArrayAccess   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 0
dl 0
loc 53
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 9 3
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
1
<?php
2
3
namespace Drupal\easy_entity_reader;
4
5
/**
6
 * Composite to read an array or a wrapper.
7
 *
8
 * Class EntityAdapter.
9
 */
10
class CompositeArrayAccess implements \ArrayAccess
11
{
12
    /**
13
     * @var array
14
     */
15
    private $arrays;
16
17
    /**
18
     * @param array $arrays
19
     */
20
    public function __construct(array $arrays)
21
    {
22
        $this->arrays = $arrays;
23
    }
24
25
    /**
26
     * @param $offset
27
     */
28
    public function offsetExists($offset)
29
    {
30
        return isset($this->entity->$offset);
0 ignored issues
show
Bug introduced by
The property entity does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
    }
32
33
    /**
34
     * @param $offset
35
     */
36
    public function offsetGet($offset)
37
    {
38
        foreach ($this->arrays as $array) {
39
            if (isset($array[$offset])) {
40
                return $array[$offset];
41
            }
42
        }
43
        throw new \Exception('The index "'.$offset.'" doesn\'t exist');
44
    }
45
46
    /**
47
     * @param $offset
48
     * @param $value
49
     */
50
    public function offsetSet($offset, $value)
51
    {
52
        throw new \Exception('Not supported');
53
    }
54
55
    /**
56
     * @param $offset
57
     */
58
    public function offsetUnset($offset)
59
    {
60
        throw new \Exception('Not supported');
61
    }
62
}
63