Completed
Pull Request — master (#5823)
by Mikhail
13:41
created

DDC2230Test   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 56
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testNotifyTrackingNotCalledOnUninitializedProxies() 0 22 1
A testNotifyTrackingCalledOnProxyInitialization() 0 18 1
A setUp() 0 11 2
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\Tools\ToolsException;
8
use Doctrine\Tests\OrmFunctionalTestCase;
9
10
/**
11
 * @group DDC-2230
12
 */
13
class DDC2230Test extends OrmFunctionalTestCase
14
{
15
    protected function setUp()
16
    {
17
        parent::setUp();
18
19
        try {
20
            $this->_schemaTool->createSchema(array(
21
                $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2230User'),
22
                $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2230Address'),
23
            ));
24
        } catch (ToolsException $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
    public function testNotifyTrackingNotCalledOnUninitializedProxies()
28
    {
29
        $insertedUser          = new DDC2230User();
30
        $insertedUser->address = new DDC2230Address();
31
32
        $this->_em->persist($insertedUser);
33
        $this->_em->persist($insertedUser->address);
34
        $this->_em->flush();
35
        $this->_em->clear();
36
37
        $user = $this->_em->find(__NAMESPACE__ . '\\DDC2230User', $insertedUser->id);
38
39
        $this->_em->clear();
40
41
        $mergedUser = $this->_em->merge($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->_em->find(__NAMES...er', $insertedUser->id) on line 37 can also be of type null; however, Doctrine\ORM\EntityManager::merge() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42
43
        /* @var $address \Doctrine\Common\Proxy\Proxy */
44
        $address = $mergedUser->address;
45
46
        $this->assertInstanceOf('Doctrine\\ORM\\Proxy\\Proxy', $address);
47
        $this->assertFalse($address->__isInitialized());
48
    }
49
50
    public function testNotifyTrackingCalledOnProxyInitialization()
51
    {
52
        $insertedAddress = new DDC2230Address();
53
54
        $this->_em->persist($insertedAddress);
55
        $this->_em->flush();
56
        $this->_em->clear();
57
58
        $addressProxy = $this->_em->getReference(__NAMESPACE__ . '\\DDC2230Address', $insertedAddress->id);
59
60
        /* @var $addressProxy \Doctrine\Common\Proxy\Proxy|\Doctrine\Tests\ORM\Functional\Ticket\DDC2230Address */
61
        $this->assertFalse($addressProxy->__isInitialized());
0 ignored issues
show
Bug introduced by
The method __isInitialized does only exist in Doctrine\Common\Proxy\Proxy, but not in Doctrine\Tests\ORM\Funct...l\Ticket\DDC2230Address.

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...
62
        $this->assertNull($addressProxy->listener);
63
64
        $addressProxy->__load();
0 ignored issues
show
Bug introduced by
The method __load does only exist in Doctrine\Common\Proxy\Proxy, but not in Doctrine\Tests\ORM\Funct...l\Ticket\DDC2230Address.

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...
65
66
        $this->assertSame($this->_em->getUnitOfWork(), $addressProxy->listener);
67
    }
68
}
69
70
/** @Entity */
71
class DDC2230User
72
{
73
    /** @Id @Column(type="integer") @GeneratedValue(strategy="AUTO") */
74
    public $id;
75
76
    /**
77
     * @OneToOne(targetEntity="DDC2230Address")
78
     */
79
    public $address;
80
}
81
82
/**
83
 * @Entity
84
 * @ChangeTrackingPolicy("NOTIFY")
85
 */
86
class DDC2230Address implements NotifyPropertyChanged
87
{
88
    /** @Id @Column(type="integer") @GeneratedValue(strategy="AUTO") */
89
    public $id;
90
91
    /**
92
     * @var \Doctrine\Common\PropertyChangedListener
93
     */
94
    public $listener;
95
96
    /** {@inheritDoc} */
97
    function addPropertyChangedListener(PropertyChangedListener $listener)
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...
98
    {
99
        $this->listener = $listener;
100
    }
101
}
102
103