testSetStringOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
namespace FwlibTest\Config;
3
4
use Fwlib\Config\StringOptionsAwareTrait;
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 StringOptionsAwareTraitTest extends PHPUnitTestCase
13
{
14
    /**
15
     * @var array
16
     */
17
    protected static $setConfigs = [];
18
19
20
    /**
21
     * @param   string[]    $methods
22
     * @return  MockObject|StringOptionsAwareTrait
23
     */
24
    protected function buildMock(array $methods = null)
25
    {
26
        $mock = $this->getMockBuilder(StringOptionsAwareTrait::class)
27
            ->setMethods($methods)
28
            ->getMockForTrait();
29
30
        return $mock;
31
    }
32
33
34
    public function testSetStringOptions()
35
    {
36
        $trait = $this->buildMock(['setConfigs']);
37
        $trait->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Fwlib\Config\StringOptionsAwareTrait.

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...
38
            ->method('setConfigs')
39
            ->willReturnCallback(function ($configs) {
40
                self::$setConfigs = $configs;
41
            });
42
43
        $trait->setStringOptions('foo, bar=42');
0 ignored issues
show
Bug introduced by
The method setStringOptions does only exist in Fwlib\Config\StringOptionsAwareTrait, 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...
44
        $this->assertEquals(
45
            ['foo' => true, 'bar' => 42],
46
            self::$setConfigs
47
        );
48
    }
49
}
50