Completed
Push — master ( fc7b1e...f39e81 )
by Benedikt
08:37
created

NumericalPlayerIdTest::testId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Created by PhpStorm.
5
 * User: benedikt
6
 * Date: 10/1/17
7
 * Time: 1:11 PM
8
 */
9
10
namespace Tfboe\FmLib\Tests\Unit\Entity\Helpers;
11
12
use PHPUnit\Framework\MockObject\MockObject;
13
use Tfboe\FmLib\Entity\Helpers\NumericalPlayerId;
14
use Tfboe\FmLib\Tests\Helpers\UnitTestCase;
15
16
/**
17
 * Class PlayerTest
18
 * @package Tfboe\FmLib\Tests\Unit\Entity
19
 */
20 View Code Duplication
class NumericalPlayerIdTest extends UnitTestCase
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
{
22
//<editor-fold desc="Public Methods">
23
  /**
24
   * @covers \Tfboe\FmLib\Entity\Helpers\NumericalPlayerId::getPlayerId
25
   */
26
  public function testId()
27
  {
28
    $player = $this->player();
29
    /** @noinspection PhpUnhandledExceptionInspection */
30
    $idProperty = self::getProperty(get_class($player), 'playerId');
31
    $idProperty->setValue($player, 0);
32
    self::assertEquals(0, $player->getPlayerId());
0 ignored issues
show
Bug introduced by
The method getPlayerId does only exist in Tfboe\FmLib\Entity\Helpers\NumericalPlayerId, 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...
33
  }
34
//</editor-fold desc="Public Methods">
35
36
//<editor-fold desc="Private Methods">
37
  /**
38
   * @return NumericalPlayerId|MockObject a new player
39
   */
40
  private function player(): MockObject
41
  {
42
    return $this->getMockForTrait(NumericalPlayerId::class);
43
  }
44
//</editor-fold desc="Private Methods">
45
}