Completed
Push — develop ( ddc457...b629d8 )
by
unknown
07:19
created

ClonePropertiesTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 46
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 4 1
C cloneProperties() 0 38 11
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Entity;
12
13
use Core\Entity\Collection\ArrayCollection;
14
use Doctrine\Common\Collections\Collection;
15
16
/**
17
 * ${CARET}
18
 * 
19
 * @author Mathias Gelhausen <[email protected]>
20
 * @todo write test 
21
 */
22
trait ClonePropertiesTrait
23
{
24
    public function __clone()
25
    {
26
        $this->cloneProperties();
27
    }
28
29
    private function cloneProperties(array $properties = null)
30
    {
31
        if (null === $properties) {
32
            $properties = isset($this->cloneProperties) ? $this->cloneProperties : [];
0 ignored issues
show
Bug introduced by
The property cloneProperties does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
        }
34
35
        foreach ($properties as $property) {
36
            if (0 === strpos($property, '!')) {
37
                $property = substr($property, 1);
38
                $loop = false;
39
            } else {
40
                $loop = true;
41
            }
42
43
            $value = $this->{$property};
44
            if ($value instanceOf Collection && $loop) {
45
                $collection = new ArrayCollection();
46
                foreach ($value as $item) {
47
                    $collection->add(clone $item);
48
                }
49
                $value = $collection;
50
            } elseif(null === $value) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
51
52
            } else {
53
                $value = clone $value;
54
            }
55
56
            $this->{$property} = $value;
57
        }
58
59
        if ($this instanceOf IdentifiableEntityInterface) {
60
            $this->setId(null);
61
        }
62
63
        if (is_callable('parent::__clone')) {
64
            parent::__clone();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (__clone() instead of cloneProperties()). Are you sure this is correct? If so, you might want to change this to $this->__clone().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
65
        }
66
    }
67
}