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

DDC1238Test::testIssue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 12
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 ProxyManager\Proxy\GhostObjectInterface;
9
10
/**
11
 * @group DDC-1238
12
 */
13
class DDC1238Test extends \Doctrine\Tests\OrmFunctionalTestCase
14
{
15
    public function setUp()
16
    {
17
        parent::setUp();
18
        try {
19
            $this->schemaTool->createSchema(
20
                [
21
                $this->em->getClassMetadata(DDC1238User::class),
22
                ]
23
            );
24
        } catch(\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
25
26
        }
27
    }
28
29
    public function testIssue()
30
    {
31
        $user = new DDC1238User;
32
        $user->setName("test");
33
34
        $this->em->persist($user);
35
        $this->em->flush();
36
        $this->em->clear();
37
38
        $userId = $user->getId();
39
        $this->em->clear();
40
41
        $user = $this->em->getReference(DDC1238User::class, $userId);
42
        $this->em->clear();
43
44
        $userId2 = $user->getId();
45
        self::assertEquals($userId, $userId2, "This proxy can still be initialized.");
46
    }
47
48
    public function testIssueProxyClear()
49
    {
50
        $user = new DDC1238User;
51
        $user->setName("test");
52
53
        $this->em->persist($user);
54
        $this->em->flush();
55
        $this->em->clear();
56
57
        // force proxy load, getId() doesn't work anymore
58
        $user->getName();
0 ignored issues
show
Unused Code introduced by
The call to the method Doctrine\Tests\ORM\Funct...\DDC1238User::getName() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
59
        $userId = $user->getId();
60
        $this->em->clear();
61
62
        /* @var $user DDC1238User|GhostObjectInterface */
63
        $user = $this->em->getReference(DDC1238User::class, $userId);
64
65
        $this->em->clear();
66
67
        /* @var $user2 DDC1238User|GhostObjectInterface */
68
        $user2 = $this->em->getReference(DDC1238User::class, $userId);
69
70
        $user->initializeProxy();
0 ignored issues
show
Bug introduced by
The method initializeProxy does only exist in ProxyManager\Proxy\GhostObjectInterface, but not in Doctrine\Tests\ORM\Functional\Ticket\DDC1238User.

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...
71
72
        self::assertInternalType(
73
            'integer',
74
            $user->getId(),
0 ignored issues
show
Bug introduced by
The method getId does only exist in Doctrine\Tests\ORM\Functional\Ticket\DDC1238User, 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...
75
            'Even if a proxy is detached, it should still have an identifier'
76
        );
77
78
        $user2->initializeProxy();
79
80
        self::assertInternalType(
81
            'integer',
82
            $user2->getId(),
83
            'The managed instance still has an identifier'
84
        );
85
    }
86
}
87
88
/**
89
 * @ORM\Entity
90
 */
91
class DDC1238User
92
{
93
    /** @ORM\Id @ORM\GeneratedValue @ORM\Column(type="integer") */
94
    private $id;
95
96
    /**
97
     * @ORM\Column
98
     * @var string
99
     */
100
    private $name;
101
102
    public function getId()
103
    {
104
        return $this->id;
105
    }
106
107
    public function getName()
108
    {
109
        return $this->name;
110
    }
111
112
    public function setName($name)
113
    {
114
        $this->name = $name;
115
    }
116
}
117
118