Passed
Push — main ( 7189b9...23cbe4 )
by Vasil
03:17
created

ToArray::toArray()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 4
nop 0
dl 0
loc 22
ccs 12
cts 12
cp 1
crap 5
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VasilDakov\Speedy\Traits;
6
7
use ReflectionClass;
8
9
use function is_object;
10
use function method_exists;
11
12
trait ToArray
13
{
14 19
    public function toArray(): array
15
    {
16 19
        $array = [];
17
18 19
        $reflection = new ReflectionClass($this);
19 19
        $properties = $reflection->getProperties();
20
21 19
        foreach ($properties as $property) {
22 18
            if ($property->getValue($this)) {
23 16
                $value = $property->getValue($this);
24
                if (
25 16
                    is_object($value) &&
26 16
                    method_exists($value, 'toArray')
27
                ) {
28 6
                    $array[$property->getName()] = $value->toArray();
29
                } else {
30 15
                    $array[$property->getName()] = $property->getValue($this);
31
                }
32
            }
33
        }
34
35 19
        return $array;
36
    }
37
}
38