Completed
Push — issue#666 ( 928a8e...2654e9 )
by Guilherme
04:24
created

AuthorizationSubscriberTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 209
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 4

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetSubscribedEvents() 0 10 1
B testOnNewAuthorizationRequest() 0 27 1
B testOnRemoteClaimNotFound() 0 27 1
A testOnNewAuthorization() 0 12 1
A testOnUpdateAuthorization() 0 12 1
A testOnRevokeAuthorizationWithRemoteClaims() 0 18 1
A testOnRevokeAuthorizationWithoutRemoteClaims() 0 11 1
A prepareEnforceRemoteClaimsTest() 0 21 1
A getRemoteClaim() 0 6 1
A getCompleteRemoteClaim() 0 12 2
A getRemoteClaimManager() 0 4 1
A getRemoteClaimFetcher() 0 4 1
A getClient() 0 4 1
A getEvent() 0 5 1
A getPerson() 0 4 1
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\RemoteClaimsBundle\Tests\EventSubscriber;
12
13
use LoginCidadao\CoreBundle\Model\PersonInterface;
14
use LoginCidadao\OpenIDBundle\Event\AuthorizationEvent;
15
use LoginCidadao\OpenIDBundle\LoginCidadaoOpenIDEvents;
16
use LoginCidadao\RemoteClaimsBundle\EventSubscriber\AuthorizationSubscriber;
17
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimFetcherInterface;
18
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimManagerInterface;
19
use LoginCidadao\OAuthBundle\Model\ClientInterface;
20
use LoginCidadao\RemoteClaimsBundle\Model\TagUri;
21
use Psr\Log\LoggerInterface;
22
use Psr\Log\LogLevel;
23
24
class AuthorizationSubscriberTest extends \PHPUnit_Framework_TestCase
25
{
26
    public function testGetSubscribedEvents()
27
    {
28
        $this->assertEquals([
29
            LoginCidadaoOpenIDEvents::NEW_AUTHORIZATION_REQUEST => 'onNewAuthorizationRequest',
30
            LoginCidadaoOpenIDEvents::NEW_AUTHORIZATION => 'onNewAuthorization',
31
            LoginCidadaoOpenIDEvents::UPDATE_AUTHORIZATION => 'onUpdateAuthorization',
32
            LoginCidadaoOpenIDEvents::REVOKE_AUTHORIZATION => 'onRevokeAuthorization',
33
        ], AuthorizationSubscriber::getSubscribedEvents()
34
        );
35
    }
36
37
    public function testOnNewAuthorizationRequest()
38
    {
39
        $scope = [
40
            'openid',
41
            'simple_claim',
42
            'https://claim.provider.example.com/my-claim',
43
            'tag:example.com,2017:my_claim',
44
        ];
45
46
        $remoteClaims = [];
47
48
        $fetcher = $this->getRemoteClaimFetcher();
49
        $fetcher->expects($this->atLeastOnce())->method('getRemoteClaim')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\RemoteClaim...teClaimFetcherInterface.

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...
50
            ->willReturnCallback(function ($scope) use (&$remoteClaims) {
51
                $remoteClaims[] = $scope;
52
53
                return $this->getRemoteClaim();
54
            });
55
56
        $event = $this->getEvent();
57
        $event->expects($this->atLeastOnce())->method('getScope')->willReturn($scope);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\OpenIDBundle\Event\AuthorizationEvent.

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...
58
59
        $subscriber = new AuthorizationSubscriber($this->getRemoteClaimManager(), $fetcher);
0 ignored issues
show
Bug introduced by
It seems like $this->getRemoteClaimManager() targeting LoginCidadao\RemoteClaim...getRemoteClaimManager() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\RemoteClaim...bscriber::__construct() does only seem to accept object<LoginCidadao\Remo...eClaimManagerInterface>, 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...
Bug introduced by
It seems like $fetcher defined by $this->getRemoteClaimFetcher() on line 48 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\RemoteClaim...bscriber::__construct() does only seem to accept object<LoginCidadao\Remo...eClaimFetcherInterface>, 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...
60
        $subscriber->onNewAuthorizationRequest($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->getEvent() on line 56 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\RemoteClaim...wAuthorizationRequest() does only seem to accept object<LoginCidadao\Open...ent\AuthorizationEvent>, 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...
61
62
        $this->assertCount(2, $remoteClaims);
63
    }
64
65
    public function testOnRemoteClaimNotFound()
66
    {
67
        $scope = [
68
            'openid',
69
            'simple_claim',
70
            'https://not.actually.a.remote.claim/fake',
71
        ];
72
73
        $remoteClaims = [];
74
75
        $fetcher = $this->getRemoteClaimFetcher();
76
        $fetcher->expects($this->atLeastOnce())->method('getRemoteClaim')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\RemoteClaim...teClaimFetcherInterface.

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
            ->willThrowException(new \RuntimeException('Random error'));
78
79
        /** @var \PHPUnit_Framework_MockObject_MockObject|LoggerInterface $logger */
80
        $logger = $this->getMock('Psr\Log\LoggerInterface');
81
        $logger->expects($this->once())->method('log')->with(LogLevel::ERROR);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Psr\Log\LoggerInterface.

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...
82
83
        $event = $this->getEvent();
84
        $event->expects($this->atLeastOnce())->method('getScope')->willReturn($scope);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\OpenIDBundle\Event\AuthorizationEvent.

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...
85
86
        $subscriber = new AuthorizationSubscriber($this->getRemoteClaimManager(), $fetcher);
0 ignored issues
show
Bug introduced by
It seems like $this->getRemoteClaimManager() targeting LoginCidadao\RemoteClaim...getRemoteClaimManager() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\RemoteClaim...bscriber::__construct() does only seem to accept object<LoginCidadao\Remo...eClaimManagerInterface>, 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...
Bug introduced by
It seems like $fetcher defined by $this->getRemoteClaimFetcher() on line 75 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\RemoteClaim...bscriber::__construct() does only seem to accept object<LoginCidadao\Remo...eClaimFetcherInterface>, 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...
87
        $subscriber->setLogger($logger);
88
        $subscriber->onNewAuthorizationRequest($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->getEvent() on line 83 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\RemoteClaim...wAuthorizationRequest() does only seem to accept object<LoginCidadao\Open...ent\AuthorizationEvent>, 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...
89
90
        $this->assertEmpty($remoteClaims);
91
    }
92
93
    public function testOnNewAuthorization()
94
    {
95
        $test = $this->prepareEnforceRemoteClaimsTest();
96
97
        /** @var AuthorizationSubscriber $subscriber */
98
        $subscriber = $test['subscriber'];
99
100
        /** @var AuthorizationEvent $event */
101
        $event = $test['event'];
102
103
        $subscriber->onNewAuthorization($event);
104
    }
105
106
    public function testOnUpdateAuthorization()
107
    {
108
        $test = $this->prepareEnforceRemoteClaimsTest();
109
110
        /** @var AuthorizationSubscriber $subscriber */
111
        $subscriber = $test['subscriber'];
112
113
        /** @var AuthorizationEvent $event */
114
        $event = $test['event'];
115
116
        $subscriber->onUpdateAuthorization($event);
117
    }
118
119
    public function testOnRevokeAuthorizationWithRemoteClaims()
120
    {
121
        $remoteClaims = [$this->getCompleteRemoteClaim(), $this->getCompleteRemoteClaim()];
122
123
        $authorization = $this->getMock('LoginCidadao\CoreBundle\Entity\Authorization');
124
125
        $event = $this->getEvent();
126
        $event->expects($this->once())->method('getRemoteClaims')->willReturn($remoteClaims);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\OpenIDBundle\Event\AuthorizationEvent.

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...
127
        $event->expects($this->once())->method('getAuthorization')->willReturn($authorization);
128
129
        $manager = $this->getRemoteClaimManager();
130
        $manager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\RemoteClaim...teClaimManagerInterface.

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...
131
            ->method('revokeAllAuthorizations')
132
            ->with($this->isInstanceOf('LoginCidadao\CoreBundle\Entity\Authorization'));
133
134
        $subscriber = new AuthorizationSubscriber($manager, $this->getRemoteClaimFetcher());
0 ignored issues
show
Bug introduced by
It seems like $manager defined by $this->getRemoteClaimManager() on line 129 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\RemoteClaim...bscriber::__construct() does only seem to accept object<LoginCidadao\Remo...eClaimManagerInterface>, 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...
Bug introduced by
It seems like $this->getRemoteClaimFetcher() targeting LoginCidadao\RemoteClaim...getRemoteClaimFetcher() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\RemoteClaim...bscriber::__construct() does only seem to accept object<LoginCidadao\Remo...eClaimFetcherInterface>, 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...
135
        $subscriber->onRevokeAuthorization($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->getEvent() on line 125 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\RemoteClaim...onRevokeAuthorization() does only seem to accept object<LoginCidadao\Open...ent\AuthorizationEvent>, 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...
136
    }
137
138
    public function testOnRevokeAuthorizationWithoutRemoteClaims()
139
    {
140
        $event = $this->getEvent();
141
        $event->expects($this->once())->method('getRemoteClaims')->willReturn(null);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\OpenIDBundle\Event\AuthorizationEvent.

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...
142
143
        $manager = $this->getRemoteClaimManager();
144
        $manager->expects($this->never())->method('revokeAllAuthorizations');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\RemoteClaim...teClaimManagerInterface.

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...
145
146
        $subscriber = new AuthorizationSubscriber($manager, $this->getRemoteClaimFetcher());
0 ignored issues
show
Bug introduced by
It seems like $manager defined by $this->getRemoteClaimManager() on line 143 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\RemoteClaim...bscriber::__construct() does only seem to accept object<LoginCidadao\Remo...eClaimManagerInterface>, 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...
Bug introduced by
It seems like $this->getRemoteClaimFetcher() targeting LoginCidadao\RemoteClaim...getRemoteClaimFetcher() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\RemoteClaim...bscriber::__construct() does only seem to accept object<LoginCidadao\Remo...eClaimFetcherInterface>, 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...
147
        $subscriber->onRevokeAuthorization($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->getEvent() on line 140 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\RemoteClaim...onRevokeAuthorization() does only seem to accept object<LoginCidadao\Open...ent\AuthorizationEvent>, 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...
148
    }
149
150
    private function prepareEnforceRemoteClaimsTest()
151
    {
152
        $remoteClaims = [$this->getCompleteRemoteClaim(), $this->getCompleteRemoteClaim()];
153
154
        $event = $this->getEvent();
155
        $event->expects($this->once())->method('getRemoteClaims')->willReturn($remoteClaims);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\OpenIDBundle\Event\AuthorizationEvent.

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...
156
        $event->expects($this->exactly(2))->method('getClient')->willReturn($this->getClient());
157
        $event->expects($this->exactly(2))->method('getPerson')->willReturn($this->getPerson());
158
159
        $manager = $this->getRemoteClaimManager();
160
        $manager->expects($this->exactly(2))
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\RemoteClaim...teClaimManagerInterface.

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...
161
            ->method('enforceAuthorization')
162
            ->with($this->isInstanceOf('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimAuthorizationInterface'));
163
164
        $subscriber = new AuthorizationSubscriber($manager, $this->getRemoteClaimFetcher());
0 ignored issues
show
Bug introduced by
It seems like $manager defined by $this->getRemoteClaimManager() on line 159 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\RemoteClaim...bscriber::__construct() does only seem to accept object<LoginCidadao\Remo...eClaimManagerInterface>, 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...
Bug introduced by
It seems like $this->getRemoteClaimFetcher() targeting LoginCidadao\RemoteClaim...getRemoteClaimFetcher() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\RemoteClaim...bscriber::__construct() does only seem to accept object<LoginCidadao\Remo...eClaimFetcherInterface>, 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...
165
166
        return [
167
            'subscriber' => $subscriber,
168
            'event' => $event,
169
        ];
170
    }
171
172
    private function getRemoteClaim()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
173
    {
174
        $remoteClaim = $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface');
175
176
        return $remoteClaim;
177
    }
178
179
    private function getCompleteRemoteClaim($name = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
180
    {
181
        $provider = $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\ClaimProviderInterface');
182
183
        $remoteClaim = $this->getRemoteClaim();
184
        $remoteClaim->expects($this->any())->method('getProvider')
185
            ->willReturn($provider);
186
        $remoteClaim->expects($this->any())->method('getName')
187
            ->willReturn($name ?: TagUri::createFromString('tag:example.com,2017:my_claim'));
188
189
        return $remoteClaim;
190
    }
191
192
    /**
193
     * @return \PHPUnit_Framework_MockObject_MockObject|RemoteClaimManagerInterface
194
     */
195
    private function getRemoteClaimManager()
196
    {
197
        return $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimManagerInterface');
198
    }
199
200
    /**
201
     * @return \PHPUnit_Framework_MockObject_MockObject|RemoteClaimFetcherInterface
202
     */
203
    private function getRemoteClaimFetcher()
204
    {
205
        return $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimFetcherInterface');
206
    }
207
208
    /**
209
     * @return \PHPUnit_Framework_MockObject_MockObject|ClientInterface
210
     */
211
    private function getClient()
212
    {
213
        return $this->getMock('LoginCidadao\OAuthBundle\Model\ClientInterface');
214
    }
215
216
    /**
217
     * @return AuthorizationEvent|\PHPUnit_Framework_MockObject_MockObject
218
     */
219
    private function getEvent()
220
    {
221
        return $this->getMockBuilder('LoginCidadao\OpenIDBundle\Event\AuthorizationEvent')
222
            ->disableOriginalConstructor()->getMock();
223
    }
224
225
    /**
226
     * @return \PHPUnit_Framework_MockObject_MockObject|PersonInterface
227
     */
228
    private function getPerson()
229
    {
230
        return $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface');
231
    }
232
}
233