Failed Conditions
Pull Request — 2.6 (#7750)
by Luís
07:39
created

IssueGH7735Power::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
namespace Doctrine\Tests\ORM\Functional\Ticket;
3
4
use Doctrine\ORM\Mapping\Cache;
5
use Doctrine\ORM\Mapping\Column;
6
use Doctrine\ORM\Mapping\Entity;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Doctrine\Tests\ORM\Functional\Ticket\Entity. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Doctrine\ORM\Mapping\Id;
8
use Doctrine\ORM\Mapping\JoinColumn;
9
use Doctrine\ORM\Mapping\ManyToOne;
10
use Doctrine\ORM\Mapping\OneToOne;
11
use Doctrine\Tests\OrmFunctionalTestCase;
12
use function assert;
13
14
final class IssueGH7735Test extends OrmFunctionalTestCase
15
{
16
    public function setUp() : void
17
    {
18
        $this->enableSecondLevelCache();
19
        parent::setUp();
20
21
        $this->_schemaTool->createSchema(
22
            [
23
                $this->_em->getClassMetadata(IssueGH7735Car::class),
24
                $this->_em->getClassMetadata(IssueGH7735Power::class),
25
                $this->_em->getClassMetadata(IssueGH7735Engine::class),
26
            ]
27
        );
28
29
        $this->_em->persist(new IssueGH7735Car(1, new IssueGH7735Engine(1, 'turbo', new IssueGH7735Power(1))));
30
        $this->_em->flush();
31
        $this->_em->clear();
32
    }
33
34
    /**
35
     * @test
36
     * @group GH7735
37
     */
38
    public function findByReturnsCachedEntity() : void
39
    {
40
        $this->_em->getCache()->evictEntityRegion(IssueGH7735Power::class);
41
42
        $car = $this->_em->find(IssueGH7735Car::class, 1);
43
        assert($car instanceof IssueGH7735Car);
44
45
        self::assertSame('turbo', $car->getEngine()->getModel());
46
        self::assertSame(1, $car->getEngine()->getPower()->getId());
47
    }
48
}
49
50
/**
51
 * @Entity @Cache(usage="READ_ONLY")
52
 */
53
class IssueGH7735Car
54
{
55
    /**
56
     * @Id
57
     * @Column(type="integer")
58
     *
59
     * @var int
60
     */
61
    private $id;
62
63
    /**
64
     * @ManyToOne(targetEntity=IssueGH7735Engine::class, cascade={"all"})
65
     * @JoinColumn(nullable=false)
66
     * @Cache("READ_ONLY")
67
     *
68
     * @var IssueGH7735Engine
69
     */
70
    private $engine;
71
72
    public function __construct(int $id, IssueGH7735Engine $engine)
73
    {
74
        $this->id     = $id;
75
        $this->engine = $engine;
76
    }
77
78
    public function getId(): int
79
    {
80
        return $this->id;
81
    }
82
83
    public function getEngine(): IssueGH7735Engine
84
    {
85
        return $this->engine;
86
    }
87
}
88
89
/**
90
 * @Entity
91
 * @Cache(usage="READ_ONLY")
92
 */
93
class IssueGH7735Engine
94
{
95
    /**
96
     * @Id
97
     * @Column(type="integer")
98
     *
99
     * @var int
100
     */
101
    private $id;
102
103
    /**
104
     * @OneToOne(targetEntity=IssueGH7735Power::class, mappedBy="engine", cascade={"all"})
105
     * @Cache("READ_ONLY")
106
     *
107
     * @var IssueGH7735Power
108
     */
109
    private $power;
110
111
    /**
112
     * @Column
113
     *
114
     * @var string
115
     */
116
    private $model;
117
118
    public function __construct(int $id, string $model, IssueGH7735Power $power)
119
    {
120
        $this->id    = $id;
121
        $this->model = $model;
122
        $this->power = $power;
123
124
        $power->setEngine($this);
125
    }
126
127
    public function getId(): int
128
    {
129
        return $this->id;
130
    }
131
132
    public function getPower(): IssueGH7735Power
133
    {
134
        return $this->power;
135
    }
136
137
    public function getModel(): string
138
    {
139
        return $this->model;
140
    }
141
}
142
143
/**
144
 * @Entity
145
 * @Cache(usage="READ_ONLY")
146
 */
147
class IssueGH7735Power
148
{
149
    /**
150
     * @Id
151
     * @Column(type="integer")
152
     */
153
    private $id;
154
155
    /**
156
     * @OneToOne(targetEntity=IssueGH7735Engine::class, inversedBy="power")
157
     * @Cache("READ_ONLY")
158
     *
159
     * @var IssueGH7735Engine
160
     */
161
    private $engine;
162
163
    public function __construct(int $id)
164
    {
165
        $this->id = $id;
166
    }
167
168
    public function getId(): int
169
    {
170
        return $this->id;
171
    }
172
173
    public function setEngine(IssueGH7735Engine $engine): void
174
    {
175
        $this->engine = $engine;
176
    }
177
178
    public function getEngine(): IssueGH7735Engine
179
    {
180
        return $this->engine;
181
    }
182
}
183