SessionHandlerAwareTraitTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildMock() 0 8 1
A buildSessionHandlerMock() 0 9 1
A testSetGet() 0 11 1
1
<?php
2
namespace FwlibTest\Auth;
3
4
use Fwlib\Auth\SessionHandlerAwareTrait;
5
use Fwlib\Auth\SessionHandlerInterface;
6
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase;
7
use PHPUnit_Framework_MockObject_MockObject as MockObject;
8
9
/**
10
 * @copyright   Copyright 2015 Fwolf
11
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
12
 */
13
class SessionHandlerAwareTraitTest extends PHPUnitTestCase
14
{
15
    /**
16
     * @return MockObject|SessionHandlerAwareTrait
17
     */
18
    protected function buildMock()
19
    {
20
        $mock = $this->getMockBuilder(SessionHandlerAwareTrait::class)
21
            ->setMethods(null)
22
            ->getMockForTrait();
23
24
        return $mock;
25
    }
26
27
28
    /**
29
     * @return MockObject|SessionHandlerInterface
30
     */
31
    protected function buildSessionHandlerMock()
32
    {
33
        $mock = $this->getMock(
34
            SessionHandlerInterface::class,
35
            []
36
        );
37
38
        return $mock;
39
    }
40
41
42
    public function testSetGet()
43
    {
44
        $handlerAware = $this->buildMock();
45
        $handler = $this->buildSessionHandlerMock();
46
47
        $handlerAware->setSessionHandler($handler);
0 ignored issues
show
Bug introduced by
It seems like $handler defined by $this->buildSessionHandlerMock() on line 45 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Fwlib\Auth\SessionHandle...it::setSessionHandler() does only seem to accept object<Fwlib\Auth\SessionHandlerInterface>, 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
The method setSessionHandler does only exist in Fwlib\Auth\SessionHandlerAwareTrait, but not in PHPUnit_Framework_MockObject_MockObject.

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...
48
        $this->assertInstanceOf(
49
            SessionHandlerInterface::class,
50
            $handlerAware->getSessionHandler()
0 ignored issues
show
Bug introduced by
The method getSessionHandler does only exist in Fwlib\Auth\SessionHandlerAwareTrait, but not in PHPUnit_Framework_MockObject_MockObject.

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...
51
        );
52
    }
53
}
54