ArraySerializer::serializeItem()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 13
nc 6
nop 1
crap 6
1
<?php
2
3
namespace Uvinum\Joiner\Serializer;
4
5
use Uvinum\Joiner\Strategy\Strategy;
6
7
final class ArraySerializer implements Serializer
8
{
9
    private $strategies;
10
11 36
    public function __construct(Strategy $strategy = null)
12
    {
13 36
        $this->strategies = $strategy;
14 36
    }
15
16 36
    public function serialize($arg)
17
    {
18 36
        return $this->serializeItem($arg);
19
    }
20
21
22 36
    private function serializeItem($arg)
23
    {
24 36
        if ($this->isIterable($arg)) {
25 24
            $serializedArray = [];
26 24
            foreach ($arg as $key => $value) {
27 24
                $serializedArray[$key] = $this->serializeItem($value);
28 8
            }
29
30 24
            return $serializedArray;
31
        }
32
33 36
        if (!\is_object($arg)) {
34 33
            return $arg;
35
        }
36
37 30
        if (null !== $this->strategies) {
38 30
            $serializedObjectDueToStrategy = $this->strategies->execute($arg);
39 30
            if (null !== $serializedObjectDueToStrategy) {
40 3
                return $serializedObjectDueToStrategy;
41
            }
42 10
        }
43
44 30
        return $this->serializeObject($arg);
45
    }
46
47 30
    private function serializeObject($arg)
48
    {
49 30
        $reflectionClass  = new \ReflectionClass(\get_class($arg));
50 30
        $serializedObject = \array_reduce(
51 30
            $reflectionClass->getProperties(),
52 View Code Duplication
            function ($carry, $property) use ($arg) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
                /** @var \ReflectionProperty $property */
54 30
                $property->setAccessible(true);
55 30
                $carry[$property->getName()] = $this->serializeItem($property->getValue($arg));
56 30
                return $carry;
57 30
            },
58 30
            []
59 10
        );
60 30
        $parentClass = $reflectionClass->getParentClass();
61 30
        if ($parentClass) {
62 3
            $serializedParentObject = \array_reduce(
63 3
                $parentClass->getProperties(),
64 3 View Code Duplication
                function ($carry, $property) use ($arg) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
                    /** @var \ReflectionProperty $property */
66 3
                    $property->setAccessible(true);
67 3
                    $carry[$property->getName()] = $this->serializeItem($property->getValue($arg));
68 3
                    return $carry;
69 3
                },
70 3
                []
71 1
            );
72 3
            $serializedObject = \array_merge($serializedObject, $serializedParentObject);
73 1
        }
74 30
        return $serializedObject;
75
    }
76
77 36
    private function isIterable($arg)
78
    {
79 36
        return \is_array($arg) || $arg instanceof \Traversable;
80
    }
81
}
82