Passed
Push — master ( 675cc6...9d7ef7 )
by Andrea Marco
01:40 queued 12s
created

TurnsIntoArray::toSnakeCaseArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A TurnsIntoArray::getIterator() 0 3 1
1
<?php
2
3
namespace Cerbero\Dto\Traits;
4
5
use ArrayIterator;
6
use Cerbero\Dto\Exceptions\ImmutableDtoException;
7
use Cerbero\Dto\Exceptions\UnknownDtoPropertyException;
8
use Cerbero\Dto\Exceptions\UnsetDtoPropertyException;
9
use Cerbero\Dto\Manipulators\ArrayConverter;
10
use Traversable;
11
12
use const Cerbero\Dto\MUTABLE;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\MUTABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
13
14
/**
15
 * Trait to turn a DTO into an array.
16
 *
17
 */
18
trait TurnsIntoArray
19
{
20
    /**
21
     * Retrieve the DTO as an array
22
     *
23
     * @return array
24
     */
25 54
    public function toArray(): array
26
    {
27 54
        $data = [];
28
29 54
        foreach ($this->getPropertiesMap() as $name => $property) {
0 ignored issues
show
Bug introduced by
It seems like getPropertiesMap() 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
        foreach ($this->/** @scrutinizer ignore-call */ getPropertiesMap() as $name => $property) {
Loading history...
30 54
            $key = strtolower(preg_replace(ArrayConverter::RE_SNAKE_CASE, '_', $name));
31 54
            $data[$key] = ArrayConverter::instance()->convert($property->value(), true);
32
        }
33
34 54
        return $data;
35
    }
36
37
    /**
38
     * Retrieve the DTO as an iterator
39
     *
40
     * @return Traversable
41
     */
42 15
    public function getIterator(): Traversable
43
    {
44 15
        return new ArrayIterator($this->toArray(), ArrayIterator::ARRAY_AS_PROPS);
45
    }
46
47
    /**
48
     * Determine whether a given property has a value
49
     *
50
     * @param mixed $property
51
     * @return bool
52
     */
53 18
    public function offsetExists($property): bool
54
    {
55 18
        return $this->has($property);
0 ignored issues
show
Bug introduced by
It seems like has() 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

55
        return $this->/** @scrutinizer ignore-call */ has($property);
Loading history...
56
    }
57
58
    /**
59
     * Retrieve the given property value
60
     *
61
     * @param mixed $property
62
     * @return mixed
63
     * @throws UnknownDtoPropertyException
64
     */
65 33
    public function &offsetGet($property)
66
    {
67 33
        $value = $this->get($property);
0 ignored issues
show
Bug introduced by
It seems like get() 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

67
        /** @scrutinizer ignore-call */ 
68
        $value = $this->get($property);
Loading history...
68
69 27
        return $value;
70
    }
71
72
    /**
73
     * Set the given property to the provided value
74
     *
75
     * @param mixed $property
76
     * @param mixed $value
77
     * @return void
78
     * @throws ImmutableDtoException
79
     * @throws UnknownDtoPropertyException
80
     */
81 12
    public function offsetSet($property, $value): void
82
    {
83 12
        if (!($this->getFlags() & MUTABLE)) {
0 ignored issues
show
Bug introduced by
It seems like getFlags() 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

83
        if (!($this->/** @scrutinizer ignore-call */ getFlags() & MUTABLE)) {
Loading history...
84 6
            throw new ImmutableDtoException(static::class);
85
        }
86
87 6
        $this->set($property, $value);
0 ignored issues
show
Bug introduced by
It seems like set() 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

87
        $this->/** @scrutinizer ignore-call */ 
88
               set($property, $value);
Loading history...
88 6
    }
89
90
    /**
91
     * Set the given property to the provided value
92
     *
93
     * @param mixed $property
94
     * @return void
95
     * @throws ImmutableDtoException
96
     * @throws UnsetDtoPropertyException
97
     * @throws UnknownDtoPropertyException
98
     */
99 9
    public function offsetUnset($property): void
100
    {
101 9
        if (!($this->getFlags() & MUTABLE)) {
102 3
            throw new ImmutableDtoException(static::class);
103
        }
104
105 6
        $this->unset($property);
0 ignored issues
show
Bug introduced by
It seems like unset() 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

105
        $this->/** @scrutinizer ignore-call */ 
106
               unset($property);
Loading history...
106 3
    }
107
}
108