CollectionSpec   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 216
Duplicated Lines 12.96 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 29
c 0
b 0
f 0
lcom 0
cbo 2
dl 28
loc 216
rs 10

28 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 59 2
A it_is_initializable() 0 4 1
A it_gets_results_where_key_is_equal_to_given_value() 0 4 1
A it_gets_results_where_key_is_equal_to_given_value_and_a_zero() 0 4 1
A it_gets_results_that_are_in_a_given_array() 0 4 1
A it_gets_results_between_two_given_values() 0 4 1
A it_gets_results_when_key_is_null() 0 4 1
A it_gets_results_where_key_is_similar_to_given_value() 0 4 1
A it_gets_results_where_key_is_not_equal_to_given_value() 0 4 1
A it_gets_results_that_are_not_in_a_given_array() 0 4 1
A it_gets_results_that_are_not_between_two_given_values() 0 4 1
A it_gets_results_when_the_key_is_not_null() 0 4 1
A it_gets_results_where_key_is_not_similar_to_given_value() 0 4 1
A it_gets_the_first_result_in_a_given_array() 7 7 1
A it_gets_the_last_result_in_a_given_array() 7 7 1
A it_taps_through_the_given_key_and_checks_the_given_value() 0 7 1
A it_taps_through_the_given_key_and_checks_in_array() 0 4 1
A it_taps_through_the_given_key_and_checks_between_two_given_values() 0 4 1
A it_taps_through_the_given_key_and_checks_for_null() 0 4 1
A it_taps_through_the_given_key_and_checks_for_similar() 0 4 1
A it_taps_through_the_given_key_and_checks_not_the_given_value() 0 4 1
A it_taps_through_the_given_key_and_checks_not_in_array() 0 4 1
A it_taps_through_the_given_key_and_checks_not_between_two_given_values() 0 4 1
A it_taps_through_the_given_key_and_checks_for_not_null() 0 4 1
A it_taps_through_the_given_key_and_checks_for_not_similar() 0 4 1
A it_taps_through_the_object_to_find_the_first_with_a_given_value() 7 7 1
A it_taps_through_the_object_to_find_the_last_with_a_given_value() 7 7 1
A it_taps_through_a_collection_and_retrieves_a_collection() 0 8 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 spec\NukaCode\Database;
4
5
use NukaCode\Database\Collection;
6
use PhpSpec\ObjectBehavior;
7
use stdClass;
8
9
class CollectionSpec extends ObjectBehavior {
10
11
    function let()
12
    {
13
        $testData =
14
        [
15
            [
16
                'name' => 'bob',
17
                'age' => 10,
18
                'kids' => [
19
                    'name' => 'zack',
20
                    'age' => 2
21
                ]
22
            ],
23
            [
24
                'name' => 'jeff',
25
                'age' => 15,
26
                'kids' => [
27
                    'name' => 'jess',
28
                    'age' => 3
29
                ]
30
            ],
31
            [
32
                'name' => 'chris',
33
                'age' => 20,
34
                'kids' => [
35
                    'name' => 'jr',
36
                    'age' => 4
37
                ]
38
            ],
39
            [
40
                'name' => 'dug',
41
                'age' => 25,
42
                'kids' => [
43
                    'name' => 'dan',
44
                    'age' => 5
45
                ]
46
            ],
47
            [
48
                'name' => 'sam',
49
                'age' => null,
50
                'kids' => [
51
                    'name' => 'jane',
52
                    'age' => null
53
                ]
54
            ]
55
        ];
56
57
        foreach ($testData as $data) {
58
            $newParent = new stdClass();
59
            $newParent->name = $data['name'];
60
            $newParent->age = $data['age'];
61
62
            $newParent->kids = new Collection();
63
            $newChild = new stdClass();
64
            $newChild->name = $data['kids']['name'];
65
            $newChild->age = $data['kids']['age'];
66
            $newParent->kids->add($newChild);
67
            $this->add($newParent);
68
        }
69
    }
70
71
    function it_is_initializable()
72
    {
73
        $this->shouldHaveType('NukaCode\Database\Collection');
74
    }
75
76
    public function it_gets_results_where_key_is_equal_to_given_value()
77
    {
78
        $this->getWhere('name', 'bob')->shouldHaveCount(1);
79
    }
80
81
    public function it_gets_results_where_key_is_equal_to_given_value_and_a_zero()
82
    {
83
        $this->getWhere('age', 0)->shouldHaveCount(1);
84
    }
85
86
    public function it_gets_results_that_are_in_a_given_array()
87
    {
88
        $this->getWhereIn('age', [10, 15])->shouldHaveCount(2);
89
    }
90
91
    public function it_gets_results_between_two_given_values()
92
    {
93
        $this->getWhereBetween('age', [10, 20])->shouldHaveCount(3);
94
    }
95
96
    public function it_gets_results_when_key_is_null()
97
    {
98
        $this->getWhereNull('age')->shouldHaveCount(1);
99
    }
100
101
    public function it_gets_results_where_key_is_similar_to_given_value()
102
    {
103
        $this->getWhereLike('name', 'hri')->shouldHaveCount(1);
104
    }
105
106
    public function it_gets_results_where_key_is_not_equal_to_given_value()
107
    {
108
        $this->getWhereNot('name', 'bob')->shouldHaveCount(4);
109
    }
110
111
    public function it_gets_results_that_are_not_in_a_given_array()
112
    {
113
        $this->getWhereNotIn('age', [10, 15])->shouldHaveCount(2);
114
    }
115
116
    public function it_gets_results_that_are_not_between_two_given_values()
117
    {
118
        $this->getWhereNotBetween('age', [10, 20])->shouldHaveCount(1);
119
    }
120
121
    public function it_gets_results_when_the_key_is_not_null()
122
    {
123
        $this->getWhereNotNull('age')->shouldHaveCount(4);
124
    }
125
126
    public function it_gets_results_where_key_is_not_similar_to_given_value()
127
    {
128
        $this->getWhereNotLike('name', 'hri')->shouldHaveCount(4);
129
    }
130
131 View Code Duplication
    public function it_gets_the_first_result_in_a_given_array()
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...
132
    {
133
        $data = $this->getWhereInFirst('age', [10, 15]);
134
135
        $data->shouldHaveType('stdClass');
136
        $data->age->shouldBe(10);
137
    }
138
139 View Code Duplication
    public function it_gets_the_last_result_in_a_given_array()
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...
140
    {
141
        $data = $this->getWhereInLast('age', [10, 15]);
142
143
        $data->shouldHaveType('stdClass');
144
        $data->age->shouldBe(15);
145
    }
146
147
    public function it_taps_through_the_given_key_and_checks_the_given_value()
148
    {
149
        $data = $this->getWhere('kids->name', 'jess');
150
151
        $data->shouldHaveCount(1);
152
        $data->first()->name->shouldBe('jeff');
153
    }
154
155
    public function it_taps_through_the_given_key_and_checks_in_array()
156
    {
157
        $this->getWhereIn('kids->age', [2, 4])->shouldHaveCount(2);
158
    }
159
160
    public function it_taps_through_the_given_key_and_checks_between_two_given_values()
161
    {
162
        $this->getWhereBetween('kids->age', [2, 4])->shouldHaveCount(3);
163
    }
164
165
    public function it_taps_through_the_given_key_and_checks_for_null()
166
    {
167
        $this->getWhereNull('kids->age')->shouldHaveCount(1);
168
    }
169
170
    public function it_taps_through_the_given_key_and_checks_for_similar()
171
    {
172
        $this->getWhereLike('kids->name', 'es')->shouldHaveCount(1);
173
    }
174
175
    public function it_taps_through_the_given_key_and_checks_not_the_given_value()
176
    {
177
        $this->getWhereNot('kids->name', 'dan')->shouldHaveCount(4);
178
    }
179
180
    public function it_taps_through_the_given_key_and_checks_not_in_array()
181
    {
182
        $this->getWhereNotIn('kids->age', [2, 4])->shouldHaveCount(2);
183
    }
184
185
    public function it_taps_through_the_given_key_and_checks_not_between_two_given_values()
186
    {
187
        $this->getWhereNotBetween('kids->age', [2, 4])->shouldHaveCount(1);
188
    }
189
190
    public function it_taps_through_the_given_key_and_checks_for_not_null()
191
    {
192
        $this->getWhereNotNull('kids->age')->shouldHaveCount(4);
193
    }
194
195
    public function it_taps_through_the_given_key_and_checks_for_not_similar()
196
    {
197
        $this->getWhereNotLike('kids->name', 'es')->shouldHaveCount(4);
198
    }
199
200 View Code Duplication
    public function it_taps_through_the_object_to_find_the_first_with_a_given_value()
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...
201
    {
202
        $data = $this->getWhereInFirst('kids->age', [2, 4]);
203
204
        $data->shouldHaveType('stdClass');
205
        $data->age->shouldBe(10);
206
    }
207
208 View Code Duplication
    public function it_taps_through_the_object_to_find_the_last_with_a_given_value()
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...
209
    {
210
        $data = $this->getWhereInLast('kids->age', [2, 4]);
211
212
        $data->shouldHaveType('stdClass');
213
        $data->age->shouldBe(20);
214
    }
215
216
    public function it_taps_through_a_collection_and_retrieves_a_collection()
217
    {
218
        $data = $this->first()->kids->name;
219
220
        $data->first()->shouldBe('zack');
221
        $data->shouldHaveCount(1);
222
        $data->shouldHaveType('NukaCode\Database\Collection');
223
    }
224
}
225