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

ToArray   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 24
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 22 5
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