AbstractListTest   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 124
Duplicated Lines 33.06 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 41
loc 124
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A construct_validConstructorParameters_newListCreatedWithNoExceptions() 0 13 2
A construct_invalidConstructorParameters_invalidTypeExceptionThrowed() 13 13 2
A add_validParameters_noExceptions() 0 14 3
A add_invalidParameters_noExceptions() 14 14 3
A set_validParameters_noExceptions() 0 15 3
A set_invalidParameters_noExceptions() 14 14 3
A createList() 0 6 1
getListClassName() 0 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
/** @noinspection PhpUnitUndefinedDataProviderInspection */
3
namespace Test\Vehsamrak\ListCollection;
4
5
use PHPUnit\Framework\TestCase;
6
use Vehsamrak\ListCollection\AbstractTypedList;
7
use Vehsamrak\ListCollection\Exceptions\InvalidTypeException;
8
9
abstract class AbstractListTest extends TestCase
10
{
11
    /**
12
     * @test
13
     * @dataProvider getValidParameters
14
     */
15
    public function construct_validConstructorParameters_newListCreatedWithNoExceptions(array $parameters): void
16
    {
17
        $integerList = null;
18
        $exception = null;
19
20
        try {
21
            $integerList = $this->createList($parameters);
22
        } catch (\Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
23
        }
24
25
        $this->assertInstanceOf($this->getListClassName(), $integerList);
26
        $this->assertNull($exception);
27
    }
28
29
    /**
30
     * @test
31
     * @dataProvider getInvalidParameters
32
     */
33 View Code Duplication
    public function construct_invalidConstructorParameters_invalidTypeExceptionThrowed(array $parameters): void
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...
34
    {
35
        $integerList = null;
36
        $exception = null;
37
38
        try {
39
            $integerList = $this->createList($parameters);
40
        } catch (\Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
41
        }
42
43
        $this->assertNull($integerList);
44
        $this->assertInstanceOf(InvalidTypeException::class, $exception);
45
    }
46
47
    /**
48
     * @test
49
     * @dataProvider getValidParameters
50
     */
51
    public function add_validParameters_noExceptions(array $parameters): void
52
    {
53
        $integerList = $this->createList();
54
        $exception = null;
55
56
        try {
57
            foreach ($parameters as $parameter) {
58
                $integerList->add($parameter);
59
            }
60
        } catch (\Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
61
        }
62
63
        $this->assertNull($exception);
64
    }
65
66
    /**
67
     * @test
68
     * @dataProvider getInvalidParameters
69
     */
70 View Code Duplication
    public function add_invalidParameters_noExceptions(array $parameters): void
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...
71
    {
72
        $integerList = $this->createList();
73
        $exception = null;
74
75
        try {
76
            foreach ($parameters as $parameter) {
77
                $integerList->add($parameter);
78
            }
79
        } catch (\Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
80
        }
81
82
        $this->assertInstanceOf(InvalidTypeException::class, $exception);
83
    }
84
85
    /**
86
     * @test
87
     * @dataProvider getValidParameters
88
     */
89
    public function set_validParameters_noExceptions(array $parameters): void
90
    {
91
        $integerList = $this->createList();
92
        $exception = null;
93
94
        try {
95
            foreach ($parameters as $parameter) {
96
                $integerList->set(0, $parameter);
97
            }
98
        } catch (\Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
99
        }
100
101
        $this->assertInstanceOf($this->getListClassName(), $integerList);
102
        $this->assertNull($exception);
103
    }
104
105
    /**
106
     * @test
107
     * @dataProvider getInvalidParameters
108
     */
109 View Code Duplication
    public function set_invalidParameters_noExceptions(array $parameters): void
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...
110
    {
111
        $integerList = $this->createList();
112
        $exception = null;
113
114
        try {
115
            foreach ($parameters as $parameter) {
116
                $integerList->set(0, $parameter);
117
            }
118
        } catch (\Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
119
        }
120
121
        $this->assertInstanceOf(InvalidTypeException::class, $exception);
122
    }
123
124
    protected function createList(array $parameters = []): AbstractTypedList
125
    {
126
        $className = $this->getListClassName();
127
128
        return new $className($parameters);
129
    }
130
131
    abstract protected function getListClassName(): string;
132
}
133