Completed
Pull Request — master (#735)
by Guilherme
03:50
created

getSubjectIdentifierService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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\OAuthBundle\Tests\EventListener;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use FOS\OAuthServerBundle\Event\OAuthEvent;
15
use LoginCidadao\CoreBundle\Entity\Authorization;
16
use LoginCidadao\CoreBundle\Entity\Person;
17
use LoginCidadao\CoreBundle\Model\PersonInterface;
18
use LoginCidadao\OAuthBundle\Entity\Client;
19
use LoginCidadao\OAuthBundle\EventListener\OAuthEventListener;
20
use LoginCidadao\OAuthBundle\Helper\ScopeFinderHelper;
21
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService;
22
23
class OAuthEventListenerTest extends \PHPUnit_Framework_TestCase
24
{
25
    public function testOnPreAuthorizationProcessNotPreAuthorized()
26
    {
27
        $person = $this->getPerson();
28
        $person->expects($this->once())->method('isAuthorizedClient')->willReturn(false);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Model\PersonInterface.

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...
29
30
        $personRepo = $this->getPersonRepository();
31
        $personRepo->expects($this->once())->method('findOneBy')->willReturn($person);
32
33
        $em = $this->getEntityManager(['LoginCidadaoCoreBundle:Person' => $personRepo]);
34
35
        $subIdService = $this->getSubjectIdentifierService();
36
37
        $listener = new OAuthEventListener($em, $this->getScopeFinder(['openid']), $subIdService);
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager(...erson' => $personRepo)) on line 33 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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->getScopeFinder(array('openid')) targeting LoginCidadao\OAuthBundle...rTest::getScopeFinder() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<LoginCidadao\OAut...lper\ScopeFinderHelper>, 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 $subIdService defined by $this->getSubjectIdentifierService() on line 35 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<LoginCidadao\Open...bjectIdentifierService>, 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...
38
39
        $event = new OAuthEvent($person, new Client(), false);
40
        $listener->onPreAuthorizationProcess($event);
41
    }
42
43
    public function testOnPreAuthorizationProcessPreAuthorizedSubNotPersisted()
44
    {
45
        $sub = 'abc123';
46
47
        $person = $this->getPerson();
48
        $person->expects($this->once())->method('isAuthorizedClient')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Model\PersonInterface.

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...
49
50
        $personRepo = $this->getPersonRepository();
51
        $personRepo->expects($this->once())->method('findOneBy')->willReturn($person);
52
53
        $em = $this->getEntityManager(['LoginCidadaoCoreBundle:Person' => $personRepo]);
54
        $em->expects($this->once())->method('persist')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManagerInterface.

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...
55
            ->with($this->isInstanceOf('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier'));
56
57
        $subIdService = $this->getSubjectIdentifierService();
58
        $subIdService->expects($this->once())->method('isSubjectIdentifierPersisted')->willReturn(false);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\OpenIDBundl...ubjectIdentifierService.

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...
59
        $subIdService->expects($this->once())->method('getSubjectIdentifier')->willReturn($sub);
60
61
        $listener = new OAuthEventListener($em, $this->getScopeFinder(['openid']), $subIdService);
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager(...erson' => $personRepo)) on line 53 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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->getScopeFinder(array('openid')) targeting LoginCidadao\OAuthBundle...rTest::getScopeFinder() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<LoginCidadao\OAut...lper\ScopeFinderHelper>, 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 $subIdService defined by $this->getSubjectIdentifierService() on line 57 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<LoginCidadao\Open...bjectIdentifierService>, 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...
62
63
        $event = new OAuthEvent($person, new Client(), false);
64
        $listener->onPreAuthorizationProcess($event);
65
    }
66
67
    public function testOnPreAuthorizationProcessPreAuthorizedSubPersisted()
68
    {
69
        $person = $this->getPerson();
70
        $person->expects($this->once())->method('isAuthorizedClient')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\CoreBundle\Model\PersonInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
71
72
        $personRepo = $this->getPersonRepository();
73
        $personRepo->expects($this->once())->method('findOneBy')->willReturn($person);
74
75
        $em = $this->getEntityManager(['LoginCidadaoCoreBundle:Person' => $personRepo]);
76
77
        $subIdService = $this->getSubjectIdentifierService();
78
        $subIdService->expects($this->once())->method('isSubjectIdentifierPersisted')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\OpenIDBundl...ubjectIdentifierService.

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...
79
80
        $listener = new OAuthEventListener($em, $this->getScopeFinder(['openid']), $subIdService);
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager(...erson' => $personRepo)) on line 75 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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->getScopeFinder(array('openid')) targeting LoginCidadao\OAuthBundle...rTest::getScopeFinder() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<LoginCidadao\OAut...lper\ScopeFinderHelper>, 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 $subIdService defined by $this->getSubjectIdentifierService() on line 77 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<LoginCidadao\Open...bjectIdentifierService>, 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...
81
82
        $event = new OAuthEvent($person, new Client(), false);
83
        $listener->onPreAuthorizationProcess($event);
84
    }
85
86
    public function testOnPreAuthorizationProcessNoUser()
87
    {
88
        $person = $this->getPerson();
89
        $personRepo = $this->getPersonRepository();
90
        $personRepo->expects($this->once())->method('findOneBy')->willReturn(null);
91
92
        $em = $this->getEntityManager(['LoginCidadaoCoreBundle:Person' => $personRepo]);
93
94
        $subIdService = $this->getSubjectIdentifierService();
95
96
        $event = new OAuthEvent($person, new Client(), false);
97
98
        $listener = new OAuthEventListener($em, $this->getScopeFinder(['openid']), $subIdService);
0 ignored issues
show
Bug introduced by
It seems like $this->getScopeFinder(array('openid')) targeting LoginCidadao\OAuthBundle...rTest::getScopeFinder() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<LoginCidadao\OAut...lper\ScopeFinderHelper>, 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 $em defined by $this->getEntityManager(...erson' => $personRepo)) on line 92 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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 $subIdService defined by $this->getSubjectIdentifierService() on line 94 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<LoginCidadao\Open...bjectIdentifierService>, 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
        $listener->onPreAuthorizationProcess($event);
100
    }
101
102
    public function testOnPostAuthorizationProcessNotAuthorized()
103
    {
104
        $event = new OAuthEvent(new Person(), new Client(), false);
105
        $listener = new OAuthEventListener($this->getEntityManager(), $this->getScopeFinder(['openid']),
0 ignored issues
show
Bug introduced by
It seems like $this->getEntityManager() targeting LoginCidadao\OAuthBundle...est::getEntityManager() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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 $this->getScopeFinder(array('openid')) targeting LoginCidadao\OAuthBundle...rTest::getScopeFinder() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<LoginCidadao\OAut...lper\ScopeFinderHelper>, 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...
106
            $this->getSubjectIdentifierService());
0 ignored issues
show
Bug introduced by
It seems like $this->getSubjectIdentifierService() targeting LoginCidadao\OAuthBundle...jectIdentifierService() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<LoginCidadao\Open...bjectIdentifierService>, 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...
107
        $listener->onPostAuthorizationProcess($event);
108
    }
109
110
    public function testOnPostAuthorizationProcessNewAuth()
111
    {
112
        $sub = 'abc123';
113
114
        $person = $this->getPerson();
115
        $personRepo = $this->getPersonRepository();
116
        $personRepo->expects($this->once())->method('findOneBy')->willReturn($person);
117
118
        $authRepo = $this->getAuthorizationRepository();
119
        $em = $this->getEntityManager([
120
            'LoginCidadaoCoreBundle:Person' => $personRepo,
121
            'LoginCidadaoCoreBundle:Authorization' => $authRepo,
122
        ]);
123
        $em->expects($this->exactly(2))->method('persist');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManagerInterface.

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...
124
125
        $subIdService = $this->getSubjectIdentifierService();
126
        $subIdService->expects($this->once())->method('getSubjectIdentifier')->willReturn($sub);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\OpenIDBundl...ubjectIdentifierService.

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
128
        $event = new OAuthEvent(new Person(), new Client(), true);
129
        $listener = new OAuthEventListener($em, $this->getScopeFinder(['openid']), $subIdService);
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager(...ization' => $authRepo)) on line 119 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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->getScopeFinder(array('openid')) targeting LoginCidadao\OAuthBundle...rTest::getScopeFinder() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<LoginCidadao\OAut...lper\ScopeFinderHelper>, 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 $subIdService defined by $this->getSubjectIdentifierService() on line 125 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<LoginCidadao\Open...bjectIdentifierService>, 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...
130
        $listener->onPostAuthorizationProcess($event);
131
    }
132
133
    public function testOnPostAuthorizationProcessMergeAuth()
134
    {
135
        $client = new Client();
136
        $person = $this->getPerson();
137
        $personRepo = $this->getPersonRepository();
138
        $personRepo->expects($this->once())->method('findOneBy')->willReturn($person);
139
140
        $auth = new Authorization();
141
        $auth->setPerson($person);
0 ignored issues
show
Bug introduced by
It seems like $person defined by $this->getPerson() on line 136 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\CoreBundle\...horization::setPerson() does only seem to accept null|object<LoginCidadao...\Model\PersonInterface>, 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...
142
        $auth->setClient($client);
143
        $auth->setScope(['scope1', 'scope2']);
144
145
        $authRepo = $this->getAuthorizationRepository();
146
        $authRepo->expects($this->once())->method('findOneBy')->willReturn($auth);
147
148
        $em = $this->getEntityManager([
149
            'LoginCidadaoCoreBundle:Person' => $personRepo,
150
            'LoginCidadaoCoreBundle:Authorization' => $authRepo,
151
        ]);
152
153
        $subIdService = $this->getSubjectIdentifierService();
154
        $subIdService->expects($this->once())->method('isSubjectIdentifierPersisted')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\OpenIDBundl...ubjectIdentifierService.

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...
155
156
        $event = new OAuthEvent($person, $client, true);
157
        $listener = new OAuthEventListener($em, $this->getScopeFinder(['scope1', 'scope2', 'scope3']), $subIdService);
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager(...ization' => $authRepo)) on line 148 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, 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->getScopeFinder(ar...', 'scope2', 'scope3')) targeting LoginCidadao\OAuthBundle...rTest::getScopeFinder() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<LoginCidadao\OAut...lper\ScopeFinderHelper>, 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 $subIdService defined by $this->getSubjectIdentifierService() on line 153 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OAuthBundle...Listener::__construct() does only seem to accept object<LoginCidadao\Open...bjectIdentifierService>, 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...
158
        $listener->onPostAuthorizationProcess($event);
159
160
        $this->assertContains('scope3', $auth->getScope());
161
    }
162
163
    /**
164
     * @param array $repos
165
     * @return EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
166
     */
167
    private function getEntityManager(array $repos = [])
168
    {
169
        $em = $this->getMock('Doctrine\ORM\EntityManagerInterface');
170
171
        if (count($repos) > 0) {
172
            $em->expects($this->atLeastOnce())->method('getRepository')->with($this->isType('string'))
173
                ->willReturnCallback(function ($key) use ($repos) {
174
                    return $repos[$key];
175
                });
176
        }
177
178
        return $em;
179
    }
180
181
    private function getPersonRepository()
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...
182
    {
183
        return $this->getMockBuilder('LoginCidadao\CoreBundle\Entity\PersonRepository')
184
            ->disableOriginalConstructor()->getMock();
185
    }
186
187
    private function getAuthorizationRepository()
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...
188
    {
189
        return $this->getMockBuilder('LoginCidadao\CoreBundle\Entity\AuthorizationRepository')
190
            ->disableOriginalConstructor()->getMock();
191
    }
192
193
    /**
194
     * @return SubjectIdentifierService|\PHPUnit_Framework_MockObject_MockObject
195
     */
196
    private function getSubjectIdentifierService()
197
    {
198
        return $this->getMockBuilder('LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService')
199
            ->disableOriginalConstructor()->getMock();
200
    }
201
202
    /**
203
     * @return PersonInterface|\PHPUnit_Framework_MockObject_MockObject
204
     */
205
    private function getPerson()
206
    {
207
        return $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface');
208
    }
209
210
    /**
211
     * @param array|null $scope
212
     * @return ScopeFinderHelper|\PHPUnit_Framework_MockObject_MockObject
213
     */
214
    private function getScopeFinder(array $scope = null)
215
    {
216
        $helper = $this->getMockBuilder('LoginCidadao\OAuthBundle\Helper\ScopeFinderHelper')
217
            ->disableOriginalConstructor()->getMock();
218
219
        if ($scope) {
220
            $helper->expects($this->any())->method('getScope')->willReturn($scope);
221
        }
222
223
        return $helper;
224
    }
225
}
226