Failed Conditions
Pull Request — develop (#6719)
by Marco
65:21
created

DDC3223Test::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\ORM\Annotation as ORM;
8
use Doctrine\Tests\OrmFunctionalTestCase;
9
use ProxyManager\Proxy\GhostObjectInterface;
10
11
/**
12
 * Functional tests for get Id after clone child entity
13
 *
14
 * @author Lallement Thomas <[email protected]>
15
 */
16
class DDC3223Test extends OrmFunctionalTestCase
17
{
18
    protected function setUp()
19
    {
20
        parent::setUp();
21
22
        $this->setUpEntitySchema(
23
            [
24
            Journalist::class,
25
            Participant::class,
26
            Status::class,
27
            ProfileStatus::class,
28
            ]
29
        );
30
    }
31
32
    public function testIssueGetId()
33
    {
34
        $profileStatus              = new ProfileStatus();
35
        $participant                = new Journalist();
36
        $participant->profileStatus = $profileStatus;
37
38
        $this->em->persist($profileStatus);
39
        $this->em->persist($participant);
40
        $this->em->flush();
41
        $this->em->clear();
42
43
44
        /* @var $fetchedParticipant Participant */
45
        $fetchedParticipant = $this->em->find(Participant::class, $participant->id);
46
47
        /* @var $clonedProfileStatus GhostObjectInterface|ProfileStatus */
48
        $clonedProfileStatus = clone $fetchedParticipant->profileStatus;
49
50
        self::assertInstanceOf(GhostObjectInterface::class, $clonedProfileStatus);
51
        self::assertInstanceOf(ProfileStatus::class, $clonedProfileStatus);
52
        self::assertTrue($clonedProfileStatus->isProxyInitialized());
0 ignored issues
show
Bug introduced by
The method isProxyInitialized does only exist in ProxyManager\Proxy\GhostObjectInterface, but not in Doctrine\Tests\ORM\Functional\Ticket\ProfileStatus.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
53
54
        $clonedIdentifier = $clonedProfileStatus->getId();
0 ignored issues
show
Bug introduced by
The method getId does only exist in Doctrine\Tests\ORM\Functional\Ticket\ProfileStatus, but not in ProxyManager\Proxy\GhostObjectInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
55
56
        self::assertInternalType('integer', $clonedIdentifier);
57
        self::assertSame(
58
            $profileStatus->getId(),
59
            $clonedIdentifier,
60
            'The identifier on the cloned instance is an integer'
61
        );
62
    }
63
}
64
65
/** @ORM\Entity @ORM\Table(name="ddc3223_journalist") */
66
class Journalist extends Participant
67
{
68
}
69
70
/**
71
 * @ORM\Entity @ORM\Table(name="ddc3223_participant")
72
 * @ORM\InheritanceType("JOINED")
73
 * @ORM\DiscriminatorColumn(name="discr", type="string")
74
 * @ORM\DiscriminatorMap({
75
 *     "journalist"  = "Journalist",
76
 *     "participant" = "Participant",
77
 * })
78
 */
79
class Participant
80
{
81
    /** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue */
82
    public $id;
83
84
    /**
85
     * @ORM\ManyToOne(targetEntity="ProfileStatus")
86
     * @ORM\JoinColumn(name="status_id", nullable=false)
87
     * @var ProfileStatus
88
     */
89
    public $profileStatus;
90
}
91
92
/**
93
 * @ORM\Entity @ORM\Table(name="ddc3223_status")
94
 * @ORM\InheritanceType("SINGLE_TABLE")
95
 * @ORM\DiscriminatorColumn(name="discr", type="string")
96
 * @ORM\DiscriminatorMap({
97
 *     "profile" = "ProfileStatus",
98
 *     "status"  = "Status",
99
 * })
100
 */
101
class Status
102
{
103
    /** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue(strategy="AUTO") */
104
    private $id;
105
106
    public function getId()
107
    {
108
        return $this->id;
109
    }
110
}
111
112
/**
113
 * @ORM\Entity
114
 */
115
class ProfileStatus extends Status
116
{
117
}
118