Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

CloneListenerTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 86.67 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 5
dl 26
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testOnDeepCloneAndSaveWithAbstractEntity() 14 14 1
A testOnDeepCloneAndSaveWithDeepCloneInterface() 12 12 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 Kunstmaan\AdminBundle\Tests\EventListener;
4
5
use Kunstmaan\AdminBundle\Entity\AbstractEntity;
6
use Kunstmaan\AdminBundle\Entity\DeepCloneInterface;
7
use Kunstmaan\AdminBundle\Event\DeepCloneAndSaveEvent;
8
use Kunstmaan\AdminBundle\EventListener\CloneListener;
9
use PHPUnit\Framework\TestCase;
10
11
class CloneListenerTest extends TestCase
12
{
13 View Code Duplication
    public function testOnDeepCloneAndSaveWithAbstractEntity()
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...
14
    {
15
        $listener = new CloneListener();
16
17
        /** @var AbstractEntity $entityMock */
18
        $entityMock = $this->getMockForAbstractClass(AbstractEntity::class);
19
        $entityMock->setId(747);
20
21
        $event = $this->createMock(DeepCloneAndSaveEvent::class);
22
        $event->expects($this->any())->method('getClonedEntity')->willReturn($entityMock);
23
24
        $listener->onDeepCloneAndSave($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\AdminBu...\DeepCloneAndSaveEvent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
25
        $this->assertNull($entityMock->getId());
26
    }
27
28 View Code Duplication
    public function testOnDeepCloneAndSaveWithDeepCloneInterface()
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...
29
    {
30
        $listener = new CloneListener();
31
32
        $deepCloneInterfaceMock = $this->createMock(DeepCloneInterface::class);
33
        $deepCloneInterfaceMock->expects($this->once())->method('deepClone');
34
35
        $event = $this->createMock(DeepCloneAndSaveEvent::class);
36
        $event->expects($this->any())->method('getClonedEntity')->willReturn($deepCloneInterfaceMock);
37
38
        $listener->onDeepCloneAndSave($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\AdminBu...\DeepCloneAndSaveEvent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
    }
40
}
41