UserResourceTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 76
Duplicated Lines 28.95 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
B itReturnsAListOfUsers() 0 28 1
A itReturnsASingleUser() 22 22 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Bunq\Tests\Resource;
4
5
use Bunq\BunqClient;
6
use Bunq\Resource\UserResource;
7
use PHPUnit\Framework\TestCase;
8
use Prophecy\Prophecy\ObjectProphecy;
9
10
final class UserResourceTest extends TestCase
11
{
12
    /**
13
     * @var BunqClient|ObjectProphecy
14
     */
15
    private $bunqClient;
16
17
    /**
18
     * @var UserResource
19
     */
20
    private $resource;
21
22
    public function setUp()
23
    {
24
        $this->bunqClient = $this->prophesize(BunqClient::class);
25
        $this->resource   = new UserResource($this->bunqClient->reveal());
26
    }
27
28
    /**
29
     * @test
30
     */
31
    public function itReturnsAListOfUsers()
32
    {
33
        $this->bunqClient->get('/v1/user')->willReturn(
0 ignored issues
show
Bug introduced by
The method get does only exist in Bunq\BunqClient, but not in Prophecy\Prophecy\ObjectProphecy.

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...
34
            [
35
                'Response' => [
36
                    [
37
                        'UserCompany' => [
38
                            'id' => 1,
39
                        ],
40
                    ],
41
                ],
42
            ]
43
        );
44
45
        $result = $this->resource->listUsers();
46
47
        $expectedResult = [
48
            'Response' => [
49
                [
50
                    'UserCompany' => [
51
                        'id' => 1,
52
                    ],
53
                ],
54
            ],
55
        ];
56
57
        $this->assertEquals($expectedResult, $result);
58
    }
59
60
    /**
61
     * @test
62
     */
63 View Code Duplication
    public function itReturnsASingleUser()
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...
64
    {
65
        $this->bunqClient->get('/v1/user/1')->willReturn(
0 ignored issues
show
Bug introduced by
The method get does only exist in Bunq\BunqClient, but not in Prophecy\Prophecy\ObjectProphecy.

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...
66
            [
67
                'Response' => [
68
                    [
69
                        'UserCompany' => [
70
                            'id' => 1,
71
                        ],
72
                    ],
73
                ],
74
            ]
75
        );
76
77
        $result = $this->resource->getUser(1);
78
79
        $expectedResult = [
80
            'id' => 1,
81
        ];
82
83
        $this->assertEquals($expectedResult, $result);
84
    }
85
}
86
87