Failed Conditions
Push — master ( 2b8acb...aa13e4 )
by Luís
14s
created

DDC1690Parent   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 3 1
A getName() 0 3 1
A setName() 0 4 1
A setChild() 0 3 1
A getChild() 0 3 1
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Common\NotifyPropertyChanged;
6
use Doctrine\Common\PropertyChangedListener;
7
use Doctrine\ORM\Proxy\Proxy;
8
9
class DDC1690Test extends \Doctrine\Tests\OrmFunctionalTestCase
10
{
11
    protected function setUp() {
12
        parent::setUp();
13
        try {
14
            $this->_schemaTool->createSchema(
15
                [
16
                $this->_em->getClassMetadata(DDC1690Parent::class),
17
                $this->_em->getClassMetadata(DDC1690Child::class)
18
                ]
19
            );
20
        } catch (\Exception $e) {
21
            // Swallow all exceptions. We do not test the schema tool here.
22
        }
23
    }
24
25
    public function testChangeTracking()
26
    {
27
        $parent = new DDC1690Parent();
28
        $child = new DDC1690Child();
29
        $parent->setName('parent');
30
        $child->setName('child');
31
32
        $parent->setChild($child);
33
        $child->setParent($parent);
34
35
        $this->_em->persist($parent);
36
        $this->_em->persist($child);
37
38
        $this->assertEquals(1, count($parent->listeners));
39
        $this->assertEquals(1, count($child->listeners));
40
41
        $this->_em->flush();
42
        $this->_em->clear();
43
44
        $this->assertEquals(1, count($parent->listeners));
45
        $this->assertEquals(1, count($child->listeners));
46
47
        $parentId = $parent->getId();
48
        $childId = $child->getId();
49
        unset($parent, $child);
50
51
        $parent = $this->_em->find(DDC1690Parent::class, $parentId);
52
        $child = $this->_em->find(DDC1690Child::class, $childId);
53
54
        $this->assertEquals(1, count($parent->listeners));
55
        $this->assertInstanceOf(Proxy::class, $child, 'Verifying that $child is a proxy before using proxy API');
56
        $this->assertCount(0, $child->listeners);
57
        $child->__load();
58
        $this->assertCount(1, $child->listeners);
59
        unset($parent, $child);
60
61
        $parent = $this->_em->find(DDC1690Parent::class, $parentId);
62
        $child = $parent->getChild();
63
64
        $this->assertEquals(1, count($parent->listeners));
65
        $this->assertEquals(1, count($child->listeners));
66
        unset($parent, $child);
67
68
        $child = $this->_em->find(DDC1690Child::class, $childId);
69
        $parent = $child->getParent();
70
71
        $this->assertEquals(1, count($parent->listeners));
72
        $this->assertEquals(1, count($child->listeners));
73
    }
74
}
75
76 View Code Duplication
class NotifyBaseEntity implements NotifyPropertyChanged {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
77
    public $listeners = [];
78
79
    public function addPropertyChangedListener(PropertyChangedListener $listener) {
80
        if (!in_array($listener, $this->listeners)) {
81
            $this->listeners[] = $listener;
82
        }
83
    }
84
85
    protected function onPropertyChanged($propName, $oldValue, $newValue) {
86
        if ($this->listeners) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->listeners of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
87
            foreach ($this->listeners as $listener) {
88
                $listener->propertyChanged($this, $propName, $oldValue, $newValue);
89
            }
90
        }
91
    }
92
}
93
94
/** @Entity @ChangeTrackingPolicy("NOTIFY") */
95
class DDC1690Parent extends NotifyBaseEntity {
96
    /** @Id @Column(type="integer") @GeneratedValue */
97
    private $id;
98
99
    /** @Column */
100
    private $name;
101
102
    /** @OneToOne(targetEntity="DDC1690Child") */
103
    private $child;
104
105
    function getId() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
106
        return $this->id;
107
    }
108
109
    function getName() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
110
        return $this->name;
111
    }
112
113
    function setName($name) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
114
        $this->onPropertyChanged('name', $this->name, $name);
115
        $this->name = $name;
116
    }
117
118
    function setChild($child) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
119
        $this->child = $child;
120
    }
121
122
    function getChild() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
123
        return $this->child;
124
    }
125
}
126
127
/** @Entity */
128
class DDC1690Child extends NotifyBaseEntity {
129
    /** @Id @Column(type="integer") @GeneratedValue */
130
    private $id;
131
132
    /** @Column */
133
    private $name;
134
135
    /** @OneToOne(targetEntity="DDC1690Parent", mappedBy="child") */
136
    private $parent;
137
138
    function getId() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
139
        return $this->id;
140
    }
141
142
    function getName() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
143
        return $this->name;
144
    }
145
146
    function setName($name) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
147
        $this->onPropertyChanged('name', $this->name, $name);
148
        $this->name = $name;
149
    }
150
151
    function setParent($parent) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
152
        $this->parent = $parent;
153
    }
154
155
    function getParent() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
156
        return $this->parent;
157
    }
158
}
159