Completed
Push — master ( 1bc38b...e5e579 )
by Paul
05:30
created

ArrayAccessTrait::ArrayAccessTrait_unwrap()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 16
nop 1
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Mcneely\Core\Traits;
5
6
use ArrayIterator;
7
use Mcneely\Core\CoreObject;
8
9
/**
10
 * Trait ArrayAccessTrait.
11
 *
12
 * @package Mcneely\Core\Traits
13
 *
14
 * @method mixed setCoreObject_CoreTrait()
15
 * @method CoreObject CoreTrait_getCoreObject()
16
 * @method mixed fireEvents_CoreTrait($eventClassObject, $eventImmediateClass, $eventMethod, $eventTrait)
17
 */
18
trait ArrayAccessTrait
19
{
20 2
    public function offsetExists(string $offset): bool
21
    {
22 2
        $this->CoreTrait_fireEvents($this, __CLASS__, __METHOD__, __TRAIT__);
0 ignored issues
show
Bug introduced by
It seems like CoreTrait_fireEvents() 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

22
        $this->/** @scrutinizer ignore-call */ 
23
               CoreTrait_fireEvents($this, __CLASS__, __METHOD__, __TRAIT__);
Loading history...
23
24 2
        $object = $this->ArrayAccessTrait_unwrap();
25
26 2
        return $object->offsetExists($offset);
27
    }
28
29 1
    public function offsetGet(string $offset)
30
    {
31 1
        $this->CoreTrait_fireEvents($this, __CLASS__, __METHOD__, __TRAIT__);
32
33 1
        $object = $this->ArrayAccessTrait_unwrap();
34
35 1
        return $object->offsetGet($offset);
36
    }
37
38 1
    public function offsetSet(string $offset, $value): self
39
    {
40 1
        $this->CoreTrait_fireEvents($this, __CLASS__, __METHOD__, __TRAIT__);
41
42 1
        $object = $this->ArrayAccessTrait_unwrap(true);
43 1
        $object->offsetSet($offset, $value);
44
45 1
        return $this;
46
    }
47
48 1
    public function offsetUnset(string $offset): self
49
    {
50 1
        $this->CoreTrait_fireEvents($this, __CLASS__, __METHOD__, __TRAIT__);
51 1
        $object = $this->ArrayAccessTrait_unwrap(true);
52 1
        $object->offsetUnset($offset);
53
54 1
        return $this;
55
    }
56
57
58 2
    protected function ArrayAccessTrait_unwrap(?bool $update = false): ArrayIterator
59
    {
60 2
        $coreObject = $this->CoreTrait_getCoreObject();
61 2
        $object     = $coreObject->getObject(true);
62 2
        $object     = ($object instanceof \IteratorAggregate) ? $object->getIterator() : $object;
63 2
        $object     = ($object instanceof \IteratorIterator) ? $object->getInnerIterator() : $object;
64 2
        $object     = ($object instanceof \Iterator) ? iterator_to_array($object) : (array)$object;
65 2
        $object     = new ArrayIterator($object);
66
67 2
        if ($update) {
68 1
            $coreObject->setObject($object);
69
        }
70
71 2
        return $object;
72
    }
73
}
74