ClassAndIdPropertyTraitTest::buildMock()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
namespace FwlibTest\Html\Helper;
3
4
use Fwlib\Html\Helper\ClassAndIdPropertyTrait;
5
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase;
6
use PHPUnit_Framework_MockObject_MockObject as MockObject;
7
8
/**
9
 * @copyright   Copyright 2015 Fwolf
10
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
11
 */
12
class ClassAndIdPropertyTraitTest extends PHPUnitTestCase
13
{
14
    /**
15
     * @param   string[]    $methods
16
     * @return  MockObject|ClassAndIdPropertyTrait
17
     */
18
    protected function buildMock(array $methods = null)
19
    {
20
        $mock = $this->getMockBuilder(
21
            ClassAndIdPropertyTrait::class
22
        )
23
            ->setMethods($methods)
24
            ->getMockForTrait();
25
26
        return $mock;
27
    }
28
29
30
    public function testAccessors()
31
    {
32
        $trait = $this->buildMock();
33
34
        $trait->setClass('foo')
0 ignored issues
show
Bug introduced by
The method setClass does only exist in Fwlib\Html\Helper\ClassAndIdPropertyTrait, 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...
35
            ->setId('bar');
36
        $this->assertEquals('foo', $this->reflectionCall($trait, 'getClass'));
37
        $this->assertEquals('bar', $this->reflectionCall($trait, 'getId'));
38
39
        $trait->setClass('')
40
            ->setId('');
41
        $this->assertEquals(
42
            '',
43
            $this->reflectionCall($trait, 'getClass', ['__foo'])
44
        );
45
        $this->assertEquals(
46
            '',
47
            $this->reflectionCall($trait, 'getId', ['__bar'])
48
        );
49
50
        $trait->setClass('foo')
51
            ->setId('bar');
52
        $this->assertEquals(
53
            'foo__foo',
54
            $this->reflectionCall($trait, 'getClass', ['__foo'])
55
        );
56
        $this->assertEquals(
57
            'bar__bar',
58
            $this->reflectionCall($trait, 'getId', ['__bar'])
59
        );
60
    }
61
}
62