Completed
Push — master ( 7f572e...bf04a1 )
by Benedikt
02:39
created

NumericalIdTest::testEntityId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
rs 9.4285
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\NumericalId;
14
use Tfboe\FmLib\Tests\Helpers\UnitTestCase;
15
16
/**
17
 * Class PlayerTest
18
 * @package Tfboe\FmLib\Tests\Unit\Entity
19
 */
20
class NumericalIdTest extends UnitTestCase
21
{
22
//<editor-fold desc="Public Methods">
23
  /**
24
   * @covers \Tfboe\FmLib\Entity\Helpers\NumericalId::getId
25
   */
26 View Code Duplication
  public function testId()
0 ignored issues
show
Duplication introduced by
This method 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...
27
  {
28
    $player = $this->player();
29
    /** @noinspection PhpUnhandledExceptionInspection */
30
    $idProperty = self::getProperty(get_class($player), 'id');
31
    $idProperty->setValue($player, 0);
32
    self::assertEquals(0, $player->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Tfboe\FmLib\Entity\Helpers\NumericalId, 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
35
  /**
36
   * @covers \Tfboe\FmLib\Entity\Helpers\NumericalId::getEntityId
37
   */
38 View Code Duplication
  public function testEntityId()
0 ignored issues
show
Duplication introduced by
This method 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...
39
  {
40
    $player = $this->player();
41
    /** @noinspection PhpUnhandledExceptionInspection */
42
    $idProperty = self::getProperty(get_class($player), 'id');
43
    $idProperty->setValue($player, 1);
44
    self::assertEquals(1, $player->getEntityId());
0 ignored issues
show
Bug introduced by
The method getEntityId does only exist in Tfboe\FmLib\Entity\Helpers\NumericalId, 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...
45
  }
46
//</editor-fold desc="Public Methods">
47
48
//<editor-fold desc="Private Methods">
49
  /**
50
   * @return NumericalId|MockObject a new player
51
   */
52
  private function player(): MockObject
53
  {
54
    return $this->getMockForTrait(NumericalId::class);
55
  }
56
//</editor-fold desc="Private Methods">
57
}