ArraySerializer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 16 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 12
loc 75
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serialize() 0 4 1
B serializeItem() 0 24 6
B serializeObject() 12 29 2
A isIterable() 0 4 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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