Passed
Push — master ( 26975a...e2e428 )
by Maciej
01:16 queued 11s
created

ObjectArrayTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 110
Duplicated Lines 29.09 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 2
dl 32
loc 110
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testOperationsOnArray() 0 37 1
A testAddingWrongTypeViaConstructor() 8 8 1
A testAddingWrongTypeViaOffsetSet() 8 8 1
A testAddingWrongTypeViaAdd() 8 8 1
A testAddingWrongTypeViaArrayAccess() 8 8 1
A provideDifferentIterables() 0 11 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
declare(strict_types=1);
3
4
/**
5
 * File:ObjectArrayTest.php
6
 *
7
 * @author      Maciej Sławik <[email protected]>
8
 * Github:      https://github.com/maciejslawik
9
 */
10
11
namespace MSlwk\TypeSafeArray\Test\Unit;
12
13
use ArrayIterator;
14
use DateTime;
15
use DateTimeImmutable;
16
use InvalidArgumentException;
17
use MSlwk\TypeSafeArray\ObjectArray;
18
use PHPUnit\Framework\TestCase;
19
20
/**
21
 * Class ObjectArrayTest
22
 * @package MSlwk\TypeSafeArray\Test\Unit
23
 */
24
class ObjectArrayTest extends TestCase
25
{
26
    /**
27
     * @dataProvider provideDifferentIterables
28
     * @param iterable $iterable
29
     * @return void
30
     */
31
    public function testOperationsOnArray(iterable $iterable): void
32
    {
33
        $object1 = new DateTime();
34
        $object2 = new DateTime();
35
        $object3 = new DateTime();
36
        $object4 = new DateTime();
37
        $iterable[] = $object2;
38
        $objectArray = new ObjectArray(DateTime::class, $iterable);
39
        $objectArray[1] = $object3;
40
        $objectArray->offsetSet(2, $object4);
41
        $objectArray->add($object1);
42
43
        $this->assertSame($object2, $objectArray->offsetGet(0));
44
        $this->assertSame($object3, $objectArray[1]);
45
        $this->assertSame($object4, $objectArray[2]);
46
        $this->assertSame($object1, $objectArray[3]);
47
48
        $this->assertSame($object2, $objectArray->current());
49
        $objectArray->next();
50
        $objectArray->next();
51
        $this->assertSame($object4, $objectArray->current());
52
        $objectArray->rewind();
53
        $this->assertSame($object2, $objectArray->current());
54
55
        $this->assertEquals(4, $objectArray->count());
56
        $objectArray->offsetUnset(2);
57
        $this->assertEquals(3, $objectArray->count());
58
59
        $this->assertTrue($objectArray->offsetExists(1));
60
        $this->assertFalse($objectArray->offsetExists(2));
61
62
        $this->assertTrue($objectArray->valid());
63
64
        $this->assertEquals(0, $objectArray->key());
65
        $objectArray->next();
66
        $this->assertEquals(1, $objectArray->key());
67
    }
68
69
    /**
70
     * @dataProvider provideDifferentIterables
71
     * @param iterable $iterable
72
     * @return void
73
     */
74 View Code Duplication
    public function testAddingWrongTypeViaConstructor(iterable $iterable): 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...
75
    {
76
        $this->expectException(InvalidArgumentException::class);
77
78
        $object = new DateTimeImmutable();
79
        $iterable[] = $object;
80
        new ObjectArray(DateTime::class, $iterable);
81
    }
82
83
    /**
84
     * @return void
85
     */
86 View Code Duplication
    public function testAddingWrongTypeViaOffsetSet(): 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...
87
    {
88
        $this->expectException(InvalidArgumentException::class);
89
90
        $object = new DateTimeImmutable();
91
        $objectArray = new ObjectArray(DateTime::class);
92
        $objectArray->offsetSet(0, $object);
93
    }
94
95
    /**
96
     * @return void
97
     */
98 View Code Duplication
    public function testAddingWrongTypeViaAdd(): 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...
99
    {
100
        $this->expectException(InvalidArgumentException::class);
101
102
        $object = new DateTimeImmutable();
103
        $objectArray = new ObjectArray(DateTime::class);
104
        $objectArray->add($object);
105
    }
106
107
    /**
108
     * @return void
109
     */
110 View Code Duplication
    public function testAddingWrongTypeViaArrayAccess(): 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...
111
    {
112
        $this->expectException(InvalidArgumentException::class);
113
114
        $object = new DateTimeImmutable();
115
        $objectArray = new ObjectArray(DateTime::class);
116
        $objectArray[0] = $object;
117
    }
118
119
    /**
120
     * @return array
121
     */
122
    public function provideDifferentIterables(): array
123
    {
124
        return [
125
            'plain_array' => [
126
                []
127
            ],
128
            'iterator' => [
129
                new ArrayIterator()
130
            ]
131
        ];
132
    }
133
}
134