Completed
Push — master ( 2c2ab2...58b6ad )
by Tobias
01:06
created

TransformerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 38.18 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 21
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testOutputArray() 0 28 1
A testTransformerNotFoundExceptionClass() 7 7 1
A testTransformerNotFoundExceptionInteger() 7 7 1
A testTransformerNotFoundExceptionEnvelope() 7 7 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
declare(strict_types=1);
4
5
namespace Tests\Happyr\MessageSerializer\Transformer;
6
7
use Happyr\MessageSerializer\Transformer\Exception\TransformerNotFoundException;
8
use Happyr\MessageSerializer\Transformer\Transformer;
9
use Happyr\MessageSerializer\Transformer\TransformerInterface;
10
use PHPUnit\Framework\TestCase;
11
use Symfony\Component\Messenger\Envelope;
12
13
class TransformerTest extends TestCase
14
{
15
    public function testOutputArray()
16
    {
17
        $fooTransformer = $this->getMockBuilder(TransformerInterface::class)
18
            ->setMethods(['getVersion', 'getIdentifier', 'getPayload', 'supportsTransform'])
19
            ->getMock();
20
21
        $version = 2;
22
        $identifier = 'foobar';
23
        $payload = ['foo' => 'bar'];
24
25
        $fooTransformer->method('getVersion')->willReturn($version);
26
        $fooTransformer->method('getIdentifier')->willReturn($identifier);
27
        $fooTransformer->method('supportsTransform')->willReturn(true);
28
        $fooTransformer->method('getPayload')->willReturn($payload);
29
30
        $transformer = new Transformer([$fooTransformer]);
31
        $output = $transformer->toArray(new \stdClass());
32
33
        $this->assertArrayHasKey('version', $output);
34
        $this->assertArrayHasKey('identifier', $output);
35
        $this->assertArrayHasKey('timestamp', $output);
36
        $this->assertArrayHasKey('payload', $output);
37
38
        $this->assertEquals($output['version'], $version);
39
        $this->assertEquals($output['identifier'], $identifier);
40
        $this->assertEquals($output['payload'], $payload);
41
        $this->assertEqualsWithDelta($output['timestamp'], time(), 3);
42
    }
43
44 View Code Duplication
    public function testTransformerNotFoundExceptionClass()
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...
45
    {
46
        $transformer = new Transformer([]);
47
        $this->expectException(TransformerNotFoundException::class);
48
        $this->expectExceptionMessage('No transformer found for "stdClass"');
49
        $output = $transformer->toArray(new \stdClass());
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
    }
51
52 View Code Duplication
    public function testTransformerNotFoundExceptionInteger()
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...
53
    {
54
        $transformer = new Transformer([]);
55
        $this->expectException(TransformerNotFoundException::class);
56
        $this->expectExceptionMessage('No transformer found for "integer"');
57
        $output = $transformer->toArray(4711);
0 ignored issues
show
Documentation introduced by
4711 is of type integer, but the function expects a object.

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...
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
58
    }
59
60 View Code Duplication
    public function testTransformerNotFoundExceptionEnvelope()
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...
61
    {
62
        $transformer = new Transformer([]);
63
        $this->expectException(TransformerNotFoundException::class);
64
        $this->expectExceptionMessage('No transformer found for "Envelope<stdClass>"');
65
        $output = $transformer->toArray(new Envelope(new \stdClass()));
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
    }
67
}
68