Completed
Pull Request — master (#44)
by Eric
12:38
created

testLoadClassMetadataWithResourceAndParentClassesWithInheritance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 41

Duplication

Lines 54
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 54
loc 54
rs 9.6716
cc 1
eloc 41
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\MongoDB;
13
14
use Doctrine\Common\EventSubscriber;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs;
17
use Doctrine\ODM\MongoDB\Events;
18
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
19
use Doctrine\ODM\MongoDB\Mapping\MappingException;
20
use Lug\Bundle\ResourceBundle\EventSubscriber\Doctrine\MongoDB\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
        if (!class_exists(LoadClassMetadataEventArgs::class)) {
45
            $this->markTestSkipped();
46
        }
47
48
        $this->serviceRegistry = $this->createServiceRegistryMock();
49
        $this->resourceSubscriber = new ResourceSubscriber($this->serviceRegistry);
50
    }
51
52
    public function testInheritance()
53
    {
54
        $this->assertInstanceOf(EventSubscriber::class, $this->resourceSubscriber);
55
    }
56
57
    public function testSubscribedEvents()
58
    {
59
        $this->assertSame([Events::loadClassMetadata], $this->resourceSubscriber->getSubscribedEvents());
60
    }
61
62 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...
63
    {
64
        $event = $this->createLoadClassMetadataEventArgsMock();
65
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Eve...dClassMetadataEventArgs.

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...
66
            ->expects($this->once())
67
            ->method('getClassMetadata')
68
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
69
70
        $classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\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...
71
            ->expects($this->once())
72
            ->method('getName')
73
            ->will($this->returnValue($model = 'model'));
74
75
        $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...
76
            ->expects($this->once())
77
            ->method('getIterator')
78
            ->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()])));
79
80
        $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...
81
            ->expects($this->once())
82
            ->method('getModel')
83
            ->will($this->returnValue($model));
84
85
        $resource
86
            ->expects($this->once())
87
            ->method('getRepository')
88
            ->will($this->returnValue($repository = 'repository'));
89
90
        $classMetadata
91
            ->expects($this->once())
92
            ->method('setCustomRepositoryClass')
93
            ->with($this->identicalTo($repository));
94
95
        $classMetadata->parentClasses = [];
96
        $classMetadata->isMappedSuperclass = false;
97
98
        $this->resourceSubscriber->loadClassMetadata($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createLoadClassMetadataEventArgsMock() on line 64 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...er::loadClassMetadata() does only seem to accept object<Doctrine\ODM\Mong...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...
99
100
        $this->assertFalse($classMetadata->isMappedSuperclass);
101
    }
102
103 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...
104
    {
105
        $event = $this->createLoadClassMetadataEventArgsMock();
106
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Eve...dClassMetadataEventArgs.

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...
107
            ->expects($this->once())
108
            ->method('getClassMetadata')
109
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
110
111
        $classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\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...
112
            ->expects($this->once())
113
            ->method('getName')
114
            ->will($this->returnValue($model = 'model'));
115
116
        $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...
117
            ->expects($this->once())
118
            ->method('getIterator')
119
            ->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()])));
120
121
        $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...
122
            ->expects($this->once())
123
            ->method('getModel')
124
            ->will($this->returnValue($model));
125
126
        $classMetadata->parentClasses = [$parentClass = 'ParentClass'];
127
128
        $event
129
            ->expects($this->once())
130
            ->method('getObjectManager')
131
            ->will($this->returnValue($objectManager = $this->createObjectManagerMock()));
132
133
        $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...
134
            ->expects($this->once())
135
            ->method('getClassMetadata')
136
            ->with($this->identicalTo($parentClass))
137
            ->will($this->returnValue($parentMetadata = $this->createClassMetadataMock()));
138
139
        $parentMetadata->isMappedSuperclass = false;
140
        $parentMetadata->inheritanceType = ClassMetadata::INHERITANCE_TYPE_NONE;
141
142
        $resource
143
            ->expects($this->once())
144
            ->method('getRepository')
145
            ->will($this->returnValue($repository = 'repository'));
146
147
        $classMetadata
148
            ->expects($this->once())
149
            ->method('setCustomRepositoryClass')
150
            ->with($this->identicalTo($repository));
151
152
        $this->resourceSubscriber->loadClassMetadata($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createLoadClassMetadataEventArgsMock() on line 105 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...er::loadClassMetadata() does only seem to accept object<Doctrine\ODM\Mong...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...
153
154
        $this->assertFalse($classMetadata->isMappedSuperclass);
155
        $this->assertTrue($parentMetadata->isMappedSuperclass);
156
    }
157
158 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...
159
    {
160
        $event = $this->createLoadClassMetadataEventArgsMock();
161
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Eve...dClassMetadataEventArgs.

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...
162
            ->expects($this->once())
163
            ->method('getClassMetadata')
164
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
165
166
        $classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\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...
167
            ->expects($this->once())
168
            ->method('getName')
169
            ->will($this->returnValue($model = 'model'));
170
171
        $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...
172
            ->expects($this->once())
173
            ->method('getIterator')
174
            ->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()])));
175
176
        $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...
177
            ->expects($this->once())
178
            ->method('getModel')
179
            ->will($this->returnValue($model));
180
181
        $classMetadata->parentClasses = [$parentClass = 'ParentClass'];
182
183
        $event
184
            ->expects($this->once())
185
            ->method('getObjectManager')
186
            ->will($this->returnValue($objectManager = $this->createObjectManagerMock()));
187
188
        $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...
189
            ->expects($this->once())
190
            ->method('getClassMetadata')
191
            ->with($this->identicalTo($parentClass))
192
            ->will($this->returnValue($parentMetadata = $this->createClassMetadataMock()));
193
194
        $parentMetadata->isMappedSuperclass = false;
195
        $parentMetadata->inheritanceType = ClassMetadata::INHERITANCE_TYPE_SINGLE_COLLECTION;
196
197
        $resource
198
            ->expects($this->once())
199
            ->method('getRepository')
200
            ->will($this->returnValue($repository = 'repository'));
201
202
        $classMetadata
203
            ->expects($this->once())
204
            ->method('setCustomRepositoryClass')
205
            ->with($this->identicalTo($repository));
206
207
        $this->resourceSubscriber->loadClassMetadata($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createLoadClassMetadataEventArgsMock() on line 160 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...er::loadClassMetadata() does only seem to accept object<Doctrine\ODM\Mong...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...
208
209
        $this->assertFalse($classMetadata->isMappedSuperclass);
210
        $this->assertFalse($parentMetadata->isMappedSuperclass);
211
    }
212
213 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...
214
    {
215
        $event = $this->createLoadClassMetadataEventArgsMock();
216
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Eve...dClassMetadataEventArgs.

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...
217
            ->expects($this->once())
218
            ->method('getClassMetadata')
219
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
220
221
        $classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\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...
222
            ->expects($this->once())
223
            ->method('getName')
224
            ->will($this->returnValue($model = 'model'));
225
226
        $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...
227
            ->expects($this->once())
228
            ->method('getIterator')
229
            ->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()])));
230
231
        $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...
232
            ->expects($this->once())
233
            ->method('getModel')
234
            ->will($this->returnValue($model));
235
236
        $classMetadata->parentClasses = [$parentClass = 'ParentClass'];
237
238
        $event
239
            ->expects($this->once())
240
            ->method('getObjectManager')
241
            ->will($this->returnValue($objectManager = $this->createObjectManagerMock()));
242
243
        $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...
244
            ->expects($this->once())
245
            ->method('getClassMetadata')
246
            ->with($this->identicalTo($parentClass))
247
            ->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...
248
249
        $resource
250
            ->expects($this->once())
251
            ->method('getRepository')
252
            ->will($this->returnValue($repository = 'repository'));
253
254
        $classMetadata
255
            ->expects($this->once())
256
            ->method('setCustomRepositoryClass')
257
            ->with($this->identicalTo($repository));
258
259
        $this->resourceSubscriber->loadClassMetadata($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createLoadClassMetadataEventArgsMock() on line 215 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...er::loadClassMetadata() does only seem to accept object<Doctrine\ODM\Mong...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...
260
261
        $this->assertFalse($classMetadata->isMappedSuperclass);
262
    }
263
264 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...
265
    {
266
        $event = $this->createLoadClassMetadataEventArgsMock();
267
        $event
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Eve...dClassMetadataEventArgs.

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...
268
            ->expects($this->once())
269
            ->method('getClassMetadata')
270
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
271
272
        $classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\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...
273
            ->expects($this->once())
274
            ->method('getName')
275
            ->will($this->returnValue($model = 'model'));
276
277
        $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...
278
            ->expects($this->once())
279
            ->method('getIterator')
280
            ->will($this->returnValue(new \ArrayIterator([])));
281
282
        $classMetadata
283
            ->expects($this->never())
284
            ->method('setCustomRepositoryClass');
285
286
        $classMetadata->isMappedSuperclass = true;
287
288
        $this->resourceSubscriber->loadClassMetadata($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->createLoadClassMetadataEventArgsMock() on line 266 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...er::loadClassMetadata() does only seem to accept object<Doctrine\ODM\Mong...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...
289
290
        $this->assertTrue($classMetadata->isMappedSuperclass);
291
    }
292
293
    /**
294
     * @return \PHPUnit_Framework_MockObject_MockObject|RegistryInterface
295
     */
296
    private function createServiceRegistryMock()
297
    {
298
        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...
299
    }
300
301
    /**
302
     * @return \PHPUnit_Framework_MockObject_MockObject|LoadClassMetadataEventArgs
303
     */
304
    private function createLoadClassMetadataEventArgsMock()
305
    {
306
        return $this->getMockBuilder(LoadClassMetadataEventArgs::class)
307
            ->disableOriginalConstructor()
308
            ->getMock();
309
    }
310
311
    /**
312
     * @return \PHPUnit_Framework_MockObject_MockObject|ObjectManager
313
     */
314
    private function createObjectManagerMock()
315
    {
316
        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...
317
    }
318
319
    /**
320
     * @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
321
     */
322
    private function createClassMetadataMock()
323
    {
324
        return $this->getMockBuilder(ClassMetadata::class)
325
            ->disableOriginalConstructor()
326
            ->getMock();
327
    }
328
329
    /**
330
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
331
     */
332
    private function createResourceMock()
333
    {
334
        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...
335
    }
336
337
    /**
338
     * @return \PHPUnit_Framework_MockObject_MockObject|MappingException
339
     */
340
    private function createMappingExceptionMock()
341
    {
342
        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...
343
    }
344
}
345