Completed
Push — master ( b1e1a4...dbe8ee )
by Eric
10:13 queued 49s
created

testLoadClassMetadataWithResourceAndParentClassesWithoutClassMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50
Code Lines 38

Duplication

Lines 50
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 50
loc 50
rs 9.3333
cc 1
eloc 38
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\ResourceBundle\Tests\EventSubscriber\Doctrine\ORM;
13
14
use Doctrine\Common\EventSubscriber;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
17
use Doctrine\ORM\Events;
18
use Doctrine\ORM\Mapping\ClassMetadata;
19
use Doctrine\ORM\Mapping\MappingException;
20
use Lug\Bundle\ResourceBundle\EventSubscriber\Doctrine\ORM\ResourceSubscriber;
21
use Lug\Component\Registry\Model\RegistryInterface;
22
use Lug\Component\Resource\Model\ResourceInterface;
23
24
/**
25
 * @author GeLo <[email protected]>
26
 */
27
class ResourceSubscriberTest extends \PHPUnit_Framework_TestCase
28
{
29
    /**
30
     * @var ResourceSubscriber
31
     */
32
    private $resourceSubscriber;
33
34
    /**
35
     * @var \PHPUnit_Framework_MockObject_MockObject|RegistryInterface
36
     */
37
    private $serviceRegistry;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function setUp()
43
    {
44
        $this->serviceRegistry = $this->createServiceRegistryMock();
45
        $this->resourceSubscriber = new ResourceSubscriber($this->serviceRegistry);
46
    }
47
48
    public function testInheritance()
49
    {
50
        $this->assertInstanceOf(EventSubscriber::class, $this->resourceSubscriber);
51
    }
52
53
    public function testSubscribedEvents()
54
    {
55
        $this->assertSame([Events::loadClassMetadata], $this->resourceSubscriber->getSubscribedEvents());
56
    }
57
58 View Code Duplication
    public function testLoadClassMetadataWithResourceAndNoParentClasses()
0 ignored issues
show
Duplication introduced by
This method 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...
59
    {
60
        $event = $this->createLoadClassMetadataEventArgsMock();
61
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Event\LoadClassMetadataEventArgs.

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
            ->expects($this->once())
63
            ->method('getClassMetadata')
64
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
65
66
        $classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Mapping\ClassMetadata.

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...
67
            ->expects($this->once())
68
            ->method('getName')
69
            ->will($this->returnValue($model = 'model'));
70
71
        $this->serviceRegistry
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Registry\Model\RegistryInterface.

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...
72
            ->expects($this->once())
73
            ->method('getIterator')
74
            ->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()])));
75
76
        $resource
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\Model\ResourceInterface.

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...
77
            ->expects($this->once())
78
            ->method('getModel')
79
            ->will($this->returnValue($model));
80
81
        $resource
82
            ->expects($this->once())
83
            ->method('getRepository')
84
            ->will($this->returnValue($repository = 'repository'));
85
86
        $classMetadata
87
            ->expects($this->once())
88
            ->method('setCustomRepositoryClass')
89
            ->with($this->identicalTo($repository));
90
91
        $classMetadata->parentClasses = [];
92
        $classMetadata->isMappedSuperclass = false;
93
94
        $this->resourceSubscriber->loadClassMetadata($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createLoadClassMetadataEventArgsMock() on line 60 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...er::loadClassMetadata() does only seem to accept object<Doctrine\ORM\Even...ClassMetadataEventArgs>, 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...
95
96
        $this->assertFalse($classMetadata->isMappedSuperclass);
97
    }
98
99 View Code Duplication
    public function testLoadClassMetadataWithResourceAndParentClassesWithoutInheritance()
0 ignored issues
show
Duplication introduced by
This method 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...
100
    {
101
        $event = $this->createLoadClassMetadataEventArgsMock();
102
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Event\LoadClassMetadataEventArgs.

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...
103
            ->expects($this->once())
104
            ->method('getClassMetadata')
105
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
106
107
        $classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Mapping\ClassMetadata.

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...
108
            ->expects($this->once())
109
            ->method('getName')
110
            ->will($this->returnValue($model = 'model'));
111
112
        $this->serviceRegistry
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Registry\Model\RegistryInterface.

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...
113
            ->expects($this->once())
114
            ->method('getIterator')
115
            ->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()])));
116
117
        $resource
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\Model\ResourceInterface.

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...
118
            ->expects($this->once())
119
            ->method('getModel')
120
            ->will($this->returnValue($model));
121
122
        $classMetadata->parentClasses = [$parentClass = 'ParentClass'];
123
124
        $event
125
            ->expects($this->once())
126
            ->method('getObjectManager')
127
            ->will($this->returnValue($objectManager = $this->createObjectManagerMock()));
128
129
        $objectManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\ObjectManager.

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...
130
            ->expects($this->once())
131
            ->method('getClassMetadata')
132
            ->with($this->identicalTo($parentClass))
133
            ->will($this->returnValue($parentMetadata = $this->createClassMetadataMock()));
134
135
        $parentMetadata->isMappedSuperclass = false;
136
        $parentMetadata->inheritanceType = ClassMetadata::INHERITANCE_TYPE_NONE;
137
138
        $resource
139
            ->expects($this->once())
140
            ->method('getRepository')
141
            ->will($this->returnValue($repository = 'repository'));
142
143
        $classMetadata
144
            ->expects($this->once())
145
            ->method('setCustomRepositoryClass')
146
            ->with($this->identicalTo($repository));
147
148
        $this->resourceSubscriber->loadClassMetadata($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createLoadClassMetadataEventArgsMock() on line 101 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...er::loadClassMetadata() does only seem to accept object<Doctrine\ORM\Even...ClassMetadataEventArgs>, 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...
149
150
        $this->assertFalse($classMetadata->isMappedSuperclass);
151
        $this->assertTrue($parentMetadata->isMappedSuperclass);
152
    }
153
154 View Code Duplication
    public function testLoadClassMetadataWithResourceAndParentClassesWithInheritance()
0 ignored issues
show
Duplication introduced by
This method 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...
155
    {
156
        $event = $this->createLoadClassMetadataEventArgsMock();
157
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Event\LoadClassMetadataEventArgs.

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...
158
            ->expects($this->once())
159
            ->method('getClassMetadata')
160
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
161
162
        $classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Mapping\ClassMetadata.

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...
163
            ->expects($this->once())
164
            ->method('getName')
165
            ->will($this->returnValue($model = 'model'));
166
167
        $this->serviceRegistry
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Registry\Model\RegistryInterface.

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...
168
            ->expects($this->once())
169
            ->method('getIterator')
170
            ->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()])));
171
172
        $resource
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\Model\ResourceInterface.

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...
173
            ->expects($this->once())
174
            ->method('getModel')
175
            ->will($this->returnValue($model));
176
177
        $classMetadata->parentClasses = [$parentClass = 'ParentClass'];
178
179
        $event
180
            ->expects($this->once())
181
            ->method('getObjectManager')
182
            ->will($this->returnValue($objectManager = $this->createObjectManagerMock()));
183
184
        $objectManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\ObjectManager.

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...
185
            ->expects($this->once())
186
            ->method('getClassMetadata')
187
            ->with($this->identicalTo($parentClass))
188
            ->will($this->returnValue($parentMetadata = $this->createClassMetadataMock()));
189
190
        $parentMetadata->isMappedSuperclass = false;
191
        $parentMetadata->inheritanceType = ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE;
192
193
        $resource
194
            ->expects($this->once())
195
            ->method('getRepository')
196
            ->will($this->returnValue($repository = 'repository'));
197
198
        $classMetadata
199
            ->expects($this->once())
200
            ->method('setCustomRepositoryClass')
201
            ->with($this->identicalTo($repository));
202
203
        $this->resourceSubscriber->loadClassMetadata($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createLoadClassMetadataEventArgsMock() on line 156 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...er::loadClassMetadata() does only seem to accept object<Doctrine\ORM\Even...ClassMetadataEventArgs>, 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...
204
205
        $this->assertFalse($classMetadata->isMappedSuperclass);
206
        $this->assertFalse($parentMetadata->isMappedSuperclass);
207
    }
208
209 View Code Duplication
    public function testLoadClassMetadataWithResourceAndParentClassesWithoutClassMetadata()
0 ignored issues
show
Duplication introduced by
This method 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...
210
    {
211
        $event = $this->createLoadClassMetadataEventArgsMock();
212
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Event\LoadClassMetadataEventArgs.

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...
213
            ->expects($this->once())
214
            ->method('getClassMetadata')
215
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
216
217
        $classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Mapping\ClassMetadata.

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...
218
            ->expects($this->once())
219
            ->method('getName')
220
            ->will($this->returnValue($model = 'model'));
221
222
        $this->serviceRegistry
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Registry\Model\RegistryInterface.

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...
223
            ->expects($this->once())
224
            ->method('getIterator')
225
            ->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()])));
226
227
        $resource
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\Model\ResourceInterface.

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...
228
            ->expects($this->once())
229
            ->method('getModel')
230
            ->will($this->returnValue($model));
231
232
        $classMetadata->parentClasses = [$parentClass = 'ParentClass'];
233
234
        $event
235
            ->expects($this->once())
236
            ->method('getObjectManager')
237
            ->will($this->returnValue($objectManager = $this->createObjectManagerMock()));
238
239
        $objectManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\ObjectManager.

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...
240
            ->expects($this->once())
241
            ->method('getClassMetadata')
242
            ->with($this->identicalTo($parentClass))
243
            ->will($this->throwException($this->createMappingExceptionMock()));
0 ignored issues
show
Bug introduced by
It seems like $this->createMappingExceptionMock() targeting Lug\Bundle\ResourceBundl...eMappingExceptionMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PHPUnit_Framework_TestCase::throwException() does only seem to accept object<Throwable>|object<Exception>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
244
245
        $resource
246
            ->expects($this->once())
247
            ->method('getRepository')
248
            ->will($this->returnValue($repository = 'repository'));
249
250
        $classMetadata
251
            ->expects($this->once())
252
            ->method('setCustomRepositoryClass')
253
            ->with($this->identicalTo($repository));
254
255
        $this->resourceSubscriber->loadClassMetadata($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createLoadClassMetadataEventArgsMock() on line 211 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...er::loadClassMetadata() does only seem to accept object<Doctrine\ORM\Even...ClassMetadataEventArgs>, 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...
256
257
        $this->assertFalse($classMetadata->isMappedSuperclass);
258
    }
259
260 View Code Duplication
    public function testLoadClassMetadataWithoutResource()
0 ignored issues
show
Duplication introduced by
This method 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...
261
    {
262
        $event = $this->createLoadClassMetadataEventArgsMock();
263
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Event\LoadClassMetadataEventArgs.

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...
264
            ->expects($this->once())
265
            ->method('getClassMetadata')
266
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
267
268
        $classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Mapping\ClassMetadata.

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...
269
            ->expects($this->once())
270
            ->method('getName')
271
            ->will($this->returnValue($model = 'model'));
272
273
        $this->serviceRegistry
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Registry\Model\RegistryInterface.

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...
274
            ->expects($this->once())
275
            ->method('getIterator')
276
            ->will($this->returnValue(new \ArrayIterator([])));
277
278
        $classMetadata
279
            ->expects($this->never())
280
            ->method('setCustomRepositoryClass');
281
282
        $classMetadata->isMappedSuperclass = true;
283
284
        $this->resourceSubscriber->loadClassMetadata($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createLoadClassMetadataEventArgsMock() on line 262 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...er::loadClassMetadata() does only seem to accept object<Doctrine\ORM\Even...ClassMetadataEventArgs>, 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...
285
286
        $this->assertTrue($classMetadata->isMappedSuperclass);
287
    }
288
289
    /**
290
     * @return \PHPUnit_Framework_MockObject_MockObject|RegistryInterface
291
     */
292
    private function createServiceRegistryMock()
293
    {
294
        return $this->getMock(RegistryInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
295
    }
296
297
    /**
298
     * @return \PHPUnit_Framework_MockObject_MockObject|LoadClassMetadataEventArgs
299
     */
300
    private function createLoadClassMetadataEventArgsMock()
301
    {
302
        return $this->getMockBuilder(LoadClassMetadataEventArgs::class)
303
            ->disableOriginalConstructor()
304
            ->getMock();
305
    }
306
307
    /**
308
     * @return \PHPUnit_Framework_MockObject_MockObject|ObjectManager
309
     */
310
    private function createObjectManagerMock()
311
    {
312
        return $this->getMock(ObjectManager::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
313
    }
314
315
    /**
316
     * @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
317
     */
318
    private function createClassMetadataMock()
319
    {
320
        return $this->getMockBuilder(ClassMetadata::class)
321
            ->disableOriginalConstructor()
322
            ->getMock();
323
    }
324
325
    /**
326
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
327
     */
328
    private function createResourceMock()
329
    {
330
        return $this->getMock(ResourceInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
331
    }
332
333
    /**
334
     * @return \PHPUnit_Framework_MockObject_MockObject|MappingException
335
     */
336
    private function createMappingExceptionMock()
337
    {
338
        return $this->getMock(MappingException::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
339
    }
340
}
341