Completed
Pull Request — master (#14)
by Pavel
03:24
created

ProxyTestAbstract   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 72
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 24
loc 72
c 0
b 0
f 0
wmc 3
lcom 1
cbo 4
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B testLazyProxy() 0 39 1
B testSimpleProxy() 24 24 1
A getClientNames() 0 4 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 Bankiru\Api\Doctrine\Tests;
4
5
use Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity;
6
use Doctrine\Common\Proxy\Proxy;
7
8
class ProxyTestAbstract extends AbstractEntityManagerTest
9
{
10
    public function testLazyProxy()
11
    {
12
        $manager = $this->getManager();
13
14
        $this->getClient()->push(
15
            $this->getResponseMock(
16
                true,
17
                (object)[
18
                    'id'          => 2,
19
                    'payload'     => 'test-payload',
20
                    'sub-payload' => 'sub-payload',
21
                ]
22
            )
23
        );
24
25
        /** @var SubEntity|Proxy $entity */
26
        $entity = $manager->getReference(SubEntity::class, 2);
27
28
        //Test that entity is a proxy and request was not send
29
        self::assertInstanceOf(Proxy::class, $entity);
30
        self::assertFalse($entity->__isInitialized());
0 ignored issues
show
Bug introduced by
The method __isInitialized does only exist in Doctrine\Common\Proxy\Proxy, but not in Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity.

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...
31
        self::assertGreaterThan(0, $this->getClient()->count());
32
33
        //Test that we can obtain ID and request was still not sent
34
        self::assertEquals(2, $entity->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity, but not in Doctrine\Common\Proxy\Proxy.

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
        self::assertInstanceOf(Proxy::class, $entity);
36
        self::assertFalse($entity->__isInitialized());
37
        self::assertGreaterThan(0, $this->getClient()->count());
38
39
        //Test that we can obtain data and request was sent
40
        self::assertInstanceOf(SubEntity::class, $entity);
41
        self::assertEquals('test-payload', $entity->getPayload());
0 ignored issues
show
Bug introduced by
The method getPayload does only exist in Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity, but not in Doctrine\Common\Proxy\Proxy.

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...
42
        self::assertEquals('sub-payload', $entity->getSubPayload());
0 ignored issues
show
Bug introduced by
The method getSubPayload does only exist in Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity, but not in Doctrine\Common\Proxy\Proxy.

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...
43
44
        //Test that we are still a Proxy object
45
        self::assertInstanceOf(Proxy::class, $entity);
46
        self::assertTrue($entity->__isInitialized());
47
        self::assertEquals(0, $this->getClient()->count());
48
    }
49
50 View Code Duplication
    public function testSimpleProxy()
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...
51
    {
52
        $repository = $this->getManager()->getRepository(SubEntity::class);
53
54
        $this->getClient()->push(
55
            $this->getResponseMock(
56
                true,
57
                (object)[
58
                    'id'          => 2,
59
                    'payload'     => 'test-payload',
60
                    'sub-payload' => 'sub-payload',
61
                ]
62
            )
63
        );
64
65
        /** @var SubEntity|Proxy $entity */
66
        $entity = $repository->find(2);
67
68
        //Test that we can obtain data and request was sent
69
        self::assertInstanceOf(SubEntity::class, $entity);
70
        self::assertEquals(2, $entity->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity, but not in Doctrine\Common\Proxy\Proxy.

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...
71
        self::assertEquals('test-payload', $entity->getPayload());
0 ignored issues
show
Bug introduced by
The method getPayload does only exist in Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity, but not in Doctrine\Common\Proxy\Proxy.

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...
72
        self::assertEquals('sub-payload', $entity->getSubPayload());
0 ignored issues
show
Bug introduced by
The method getSubPayload does only exist in Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity, but not in Doctrine\Common\Proxy\Proxy.

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...
73
    }
74
75
    protected function getClientNames()
76
    {
77
        return [self::DEFAULT_CLIENT, 'test-reference-client'];
78
    }
79
}
80