SeedCollectionTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 200
Duplicated Lines 34 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 68
loc 200
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A attachAttachesNewItemIfItNotExist() 0 6 1
A attachAttachesOnlyOneSeedIfSeedsAreIdentical() 0 10 1
A mockSeed() 0 9 1
A getReturnsArrayWithTwoKeysWhenClassNameFooBar() 23 23 1
A getReturnsArrayWithOneKeyWhenClassNameFoo() 22 22 1
A getReturnsArrayWithOneKeyWhenClassNameFooBar() 23 23 1
A clearSeedCollectionRemovesAllSeeds() 0 14 1
A clearSeedCollectionSetCounterToZero() 0 12 1
A toArrayReturnsCorrectArray() 0 19 1
A tearDown() 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
namespace TildBJ\Seeder\Tests\Unit\Collection;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2018 Dennis Römmich <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
use TildBJ\Seeder\Collection\SeedCollection;
28
use Nimut\TestingFramework\TestCase\UnitTestCase;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * SeedCollectionTest
33
 *
34
 * @author Dennis Römmich<[email protected]>
35
 * @copyright Copyright belongs to the respective authors
36
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
37
 */
38
class SeedCollectionTest extends UnitTestCase
39
{
40
    /**
41
     * @var \TildBJ\Seeder\SeedCollection
42
     */
43
    protected $subject;
44
45
    public function setUp()
46
    {
47
        $this->subject = new SeedCollection();
48
    }
49
50
    /**
51
     * @method attach
52
     * @test
53
     */
54
    public function attachAttachesNewItemIfItNotExist()
55
    {
56
        $seed = $this->createMock(\TildBJ\Seeder\Seed::class);
57
        $this->subject->attach($seed);
0 ignored issues
show
Documentation introduced by
$seed is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<TildBJ\Seeder\Seed>.

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...
58
        $this->assertSame($seed, $this->subject->current()['NEW1']);
59
    }
60
61
    /**
62
     * @method attach
63
     * @test
64
     */
65
    public function attachAttachesOnlyOneSeedIfSeedsAreIdentical()
66
    {
67
        $seed = $this->createMock(\TildBJ\Seeder\Seed::class);
68
        $this->subject->attach($seed);
0 ignored issues
show
Documentation introduced by
$seed is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<TildBJ\Seeder\Seed>.

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...
69
        $this->subject->attach($seed);
0 ignored issues
show
Documentation introduced by
$seed is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<TildBJ\Seeder\Seed>.

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...
70
        $this->assertSame([
71
            0 => 'NEW1',
72
            1 => 'NEW2',
73
        ], array_keys($this->subject->current()));
74
    }
75
76
    /**
77
     * @param $name
78
     * @return \PHPUnit_Framework_MockObject_MockObject
79
     */
80
    protected function mockSeed($name)
81
    {
82
        $seed = $this->createMock(\TildBJ\Seeder\Seed::class);
83
        $seed->method('getTitle')->willReturn($name);
84
        $seed->method('getTarget')->willReturn(strtolower(preg_replace('/\B([A-Z])/', '_$1', $name)));
85
        $seed->method('getProperties')->willReturn(['pid' => 123]);
86
87
        return $seed;
88
    }
89
90
    /**
91
     * @test
92
     * @method get
93
     */
94 View Code Duplication
    public function getReturnsArrayWithTwoKeysWhenClassNameFooBar()
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...
95
    {
96
        $seeder = $this->createMock(\TildBJ\Seeder\Seeder::class);
97
        $seeder->method('getClass')->willReturn('FooBar');
98
        $fooBarSeed = $this->mockSeed('FooBar');
99
        $fooSeed = $this->mockSeed('Foo');
100
        $barSeed = $this->mockSeed('Bar');
101
102
        // NEW1
103
        $this->subject->attach($fooBarSeed);
104
        // NEW2
105
        $this->subject->attach($fooSeed);
106
        // NEW3
107
        $this->subject->attach(clone $fooBarSeed);
108
        // NEW4
109
        $this->subject->attach($barSeed);
110
111
        $this->subject->amount = 4;
0 ignored issues
show
Bug introduced by
Accessing amount on the interface TildBJ\Seeder\SeedCollection suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
112
        $this->assertSame([
113
            0 => 'NEW1',
114
            1 => 'NEW3',
115
        ], array_keys($this->subject->get($seeder)));
0 ignored issues
show
Documentation introduced by
$seeder is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<TildBJ\Seeder\Seeder>.

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...
116
    }
117
118
    /**
119
     * @test
120
     * @method get
121
     */
122 View Code Duplication
    public function getReturnsArrayWithOneKeyWhenClassNameFoo()
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...
123
    {
124
        $seeder = $this->createMock(\TildBJ\Seeder\Seeder::class);
125
        $seeder->method('getClass')->willReturn('Foo');
126
        $fooBarSeed = $this->mockSeed('FooBar');
127
        $fooSeed = $this->mockSeed('Foo');
128
        $barSeed = $this->mockSeed('Bar');
129
130
        // NEW1
131
        $this->subject->attach($fooBarSeed);
132
        // NEW2
133
        $this->subject->attach($fooSeed);
134
        // NEW3
135
        $this->subject->attach($fooBarSeed);
136
        // NEW4
137
        $this->subject->attach($barSeed);
138
139
        $this->subject->amount = 4;
0 ignored issues
show
Bug introduced by
Accessing amount on the interface TildBJ\Seeder\SeedCollection suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
140
        $this->assertSame([
141
            0 => 'NEW2',
142
        ], array_keys($this->subject->get($seeder)));
0 ignored issues
show
Documentation introduced by
$seeder is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<TildBJ\Seeder\Seeder>.

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...
143
    }
144
145
    /**
146
     * @test
147
     * @method get
148
     */
149 View Code Duplication
    public function getReturnsArrayWithOneKeyWhenClassNameFooBar()
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...
150
    {
151
        $seeder = $this->createMock(\TildBJ\Seeder\Seeder::class);
152
        $seeder->method('getClass')->willReturn('FooBar');
153
        $fooBarSeed = $this->mockSeed('FooBar');
154
        $fooSeed = $this->mockSeed('Foo');
155
        $barSeed = $this->mockSeed('Bar');
156
157
        // NEW1
158
        $this->subject->attach($fooBarSeed);
159
        // NEW2
160
        $this->subject->attach($fooSeed);
161
        // NEW3
162
        $this->subject->attach($fooBarSeed);
163
        // NEW4
164
        $this->subject->attach($barSeed);
165
166
        $this->subject->amount = 4;
0 ignored issues
show
Bug introduced by
Accessing amount on the interface TildBJ\Seeder\SeedCollection suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
167
        $this->assertSame([
168
            0 => 'NEW1',
169
            1 => 'NEW3',
170
        ], array_keys($this->subject->get($seeder)));
0 ignored issues
show
Documentation introduced by
$seeder is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<TildBJ\Seeder\Seeder>.

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...
171
    }
172
173
    /**
174
     * @method clear
175
     * @test
176
     */
177
    public function clearSeedCollectionRemovesAllSeeds()
178
    {
179
        $seed = $this->createMock(\TildBJ\Seeder\Seed::class);
180
        $this->subject->attach($seed);
0 ignored issues
show
Documentation introduced by
$seed is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<TildBJ\Seeder\Seed>.

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...
181
        $this->subject->clear();
182
183
        $this->assertSame([], $this->subject->toArray());
184
        $this->assertSame(0, $this->subject->count());
185
        $this->assertSame(false, $this->subject->current());
186
        $this->assertSame(false, $this->subject->valid());
187
        $this->assertSame(false, $this->subject->next());
188
        $this->assertSame(null, $this->subject->key());
189
        $this->assertSame(false, $this->subject->rewind());
190
    }
191
192
    /**
193
     * @method clear
194
     * @test
195
     */
196
    public function clearSeedCollectionSetCounterToZero()
197
    {
198
        $seed = $this->createMock(\TildBJ\Seeder\Seed::class);
199
        $this->subject->attach($seed);
0 ignored issues
show
Documentation introduced by
$seed is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<TildBJ\Seeder\Seed>.

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...
200
        $this->subject->clear();
201
        $this->subject->attach($seed);
0 ignored issues
show
Documentation introduced by
$seed is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<TildBJ\Seeder\Seed>.

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...
202
        $this->subject->attach(clone $seed);
0 ignored issues
show
Documentation introduced by
clone $seed is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<TildBJ\Seeder\Seed>.

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...
203
        $this->assertSame([
204
            0 => 'NEW1',
205
            1 => 'NEW2',
206
        ], array_keys($this->subject->current()));
207
    }
208
209
    /**
210
     * @method toArray
211
     * @test
212
     */
213
    public function toArrayReturnsCorrectArray()
214
    {
215
        $seed = $this->createMock(\TildBJ\Seeder\Seed::class);
216
        $seed->method('getTitle')->willReturn('FooBar');
217
        $seed->method('getTarget')->willReturn('foo_bar');
218
        $seed->method('getProperties')->willReturn(['pid' => 123]);
219
        $this->subject->attach($seed);
0 ignored issues
show
Documentation introduced by
$seed is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<TildBJ\Seeder\Seed>.

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...
220
        $this->subject->attach(clone $seed);
0 ignored issues
show
Documentation introduced by
clone $seed is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<TildBJ\Seeder\Seed>.

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...
221
        $this->assertSame([
222
            'foo_bar' => [
223
                'NEW1' => [
224
                    'pid' => 123
225
                ],
226
                'NEW2' => [
227
                    'pid' => 123
228
                ],
229
            ],
230
        ], $this->subject->toArray());
231
    }
232
233
    public function tearDown()
234
    {
235
        $this->subject->clear();
236
    }
237
}
238