Completed
Push — dev ( 12a618...f4e83e )
by
unknown
17:00 queued 07:52
created

Cloner::cloneObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
/**
3
 * @year    2016
4
 * @link    https://github.com/nnx-framework/cloner
5
 * @author  Lobanov Aleksandr <[email protected]>
6
 */
7
8
namespace Nnx\Cloner;
9
use Assert\Assertion;
10
11
/**
12
 * Class Cloner
13
 *
14
 * @package Nnx\Cloner
15
 */
16
class Cloner implements ClonerInterface
17
{
18
19
    /**
20
     * @var ClonerManagerInterface
21
     */
22
    private $clonerManager;
23
24
    /**
25
     * @var Options\ClonerOptions
26
     */
27
    private $options;
28
29
    /**
30
     * Cloner constructor.
31
     *
32
     * @param ClonerInterface       $clonerManager
33
     * @param Options\ClonerOptions $options
34
     */
35
    public function __construct(ClonerInterface $clonerManager, Options\ClonerOptions $options)
36
    {
37
        $this->clonerManager = $clonerManager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $clonerManager of type object<Nnx\Cloner\ClonerInterface> is incompatible with the declared type object<Nnx\Cloner\ClonerManagerInterface> of property $clonerManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
        $this->options = $options;
39
    }
40
41
    /**
42
     * @param mixed
43
     *
44
     * @return mixed
45
     */
46
    public function cloneObject($object)
47
    {
48
        Assertion::isObject($object);
49
50
        $cloneObject = clone $object;
51
        $this->afterClone($object, $cloneObject);
52
53
        foreach ($this->options->getRelations() as $relationName => $relation) {
54
            $cloneRelation = $this->handleRelation($object, $relationName, $relation);
55
56
            $relationSetter = 'set' . ucfirst($relationName);
57
            Assertion::methodExists($relationSetter, $object);
58
59
            $cloneObject->$relationSetter($cloneRelation);
60
        }
61
62
        return $cloneObject;
63
    }
64
65
    /**
66
     * @param mixed                          $object
67
     * @param string                         $relationName
68
     * @param Options\Cloner\RelationOptions $options
69
     *
70
     * @return mixed
71
     */
72
    protected function handleRelation($object, $relationName, Options\Cloner\RelationOptions $options)
73
    {
74
        $relationGetter = 'get' . ucfirst($relationName);
75
        Assertion::methodExists($relationGetter, $object);
76
77
        $relationData = $object->$relationGetter();
78
        if ($relationData === null) {
79
            return null;
80
        }
81
        $cloneData = null;
0 ignored issues
show
Unused Code introduced by
$cloneData is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82
        if (is_array($relationData)
83
            || $relationData instanceof \Traversable
84
        ) {
85
            $cloneData = [];
86
            foreach ($relationData as $data) {
87
                if ($relationArchiverResult = $this->getRelationClonerResult($options->getClonerName(), $data)) {
88
                    $cloneData[] = $relationArchiverResult;
89
                }
90
            }
91
        } else {
92
            $cloneData = $this->getRelationClonerResult($options->getClonerName(), $relationData);
93
        }
94
        return $cloneData;
95
    }
96
97
    /**
98
     * @param string $clonerName
99
     * @param mixed  $relationData
100
     *
101
     * @return mixed
102
     */
103
    protected function getRelationClonerResult($clonerName, $relationData)
104
    {
105
        $cloner = $this->getClonerManager()->get($clonerName);
106
        return $cloner->cloneObject($relationData);
107
    }
108
109
    /**
110
     * @param mixed $object      Исходный объект
111
     * @param mixed $cloneObject Склонированный объект
112
     */
113
    protected function afterClone($object, $cloneObject)
114
    {
115
    }
116
117
    /**
118
     * @return ClonerManagerInterface
119
     */
120
    protected function getClonerManager()
121
    {
122
        return $this->clonerManager;
123
    }
124
125
    /**
126
     * @return Options\ClonerOptions
127
     */
128
    protected function getOptions()
129
    {
130
        return $this->options;
131
    }
132
}
133