CollectionAsserter   B
last analyzed

Complexity

Total Complexity 43

Size/Duplication

Total Lines 246
Duplicated Lines 17.07 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 43
lcom 1
cbo 4
dl 42
loc 246
rs 8.3157
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
B __get() 0 21 8
A size() 0 7 1
A isEmpty() 10 10 3
A isNotEmpty() 0 10 3
A hasAllElements() 0 6 1
A hasNoElements() 0 6 1
A setWith() 0 14 3
A thatMatchToCriteria() 0 13 3
B checkMatchResult() 8 15 5
A isSorted() 0 4 1
A isSortedUsing() 13 13 3
A isNotSorted() 0 4 1
A isNotSortedUsing() 11 11 3
A checkIsSorted() 0 12 4
A valueIsSet() 0 4 1
A valueAsCollection() 0 9 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like CollectionAsserter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CollectionAsserter, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Core\Collections\Tests\Asserters;
13
14
use Cubiche\Core\Collections\CollectionInterface;
15
use Cubiche\Core\Comparable\Comparator;
16
use Cubiche\Core\Comparable\ComparatorInterface;
17
use Cubiche\Core\Specification\SpecificationInterface;
18
use mageekguy\atoum\asserters\phpObject as ObjectAsserter;
19
use mageekguy\atoum\exceptions\logic as LogicException;
20
21
/**
22
 * CollectionAsserter class.
23
 *
24
 * @author Ivannis Suárez Jerez <[email protected]>
25
 */
26
class CollectionAsserter extends ObjectAsserter
27
{
28
    /**
29
     * @var bool
30
     */
31
    protected $assertAll;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function __get($asserter)
37
    {
38
        switch (strtolower($asserter)) {
39
            case 'size':
40
                return $this->size();
41
            case 'isempty':
42
                return $this->isEmpty();
43
            case 'isnotempty':
44
                return $this->isNotEmpty();
45
            case 'hasallelements':
46
                return $this->hasAllElements();
47
            case 'hasnoelements':
48
                return $this->hasNoElements();
49
            case 'issorted':
50
                return $this->isSorted();
51
            case 'isnotsorted':
52
                return $this->isNotSorted();
53
            default:
54
                return parent::__get($asserter);
55
        }
56
    }
57
58
    /**
59
     * @return \mageekguy\atoum\stubs\asserters\integer
60
     */
61
    public function size()
62
    {
63
        return $this->generator->__call(
64
            'integer',
65
            array($this->valueAsCollection()->count())
66
        );
67
    }
68
69
    /**
70
     * @param string $failMessage
71
     *
72
     * @return $this
73
     */
74 View Code Duplication
    public function isEmpty($failMessage = null)
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
        if (($actual = $this->valueAsCollection()->isEmpty()) === true) {
77
            $this->pass();
78
        } else {
79
            $this->fail($failMessage ?: $this->getLocale()->_('%s is not empty', $this, $actual));
80
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param string $failMessage
87
     *
88
     * @return $this
89
     */
90
    public function isNotEmpty($failMessage = null)
91
    {
92
        if (!$this->valueAsCollection()->isEmpty()) {
93
            $this->pass();
94
        } else {
95
            $this->fail($failMessage ?: $this->_('%s is empty', $this));
96
        }
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return $this
103
     */
104
    public function hasAllElements()
105
    {
106
        $this->assertAll = true;
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return $this
113
     */
114
    public function hasNoElements()
115
    {
116
        $this->assertAll = false;
117
118
        return $this;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function setWith($value, $checkType = true)
125
    {
126
        parent::setWith($value, $checkType);
127
128
        if ($checkType === true) {
129
            if ($this->value instanceof CollectionInterface) {
130
                $this->pass();
131
            } else {
132
                $this->fail($this->getLocale()->_('%s is not a collection', $this));
133
            }
134
        }
135
136
        return $this;
137
    }
138
139
    /**
140
     * @param SpecificationInterface $criteria
141
     *
142
     * @return $this
143
     */
144
    public function thatMatchToCriteria(SpecificationInterface $criteria)
145
    {
146
        $collection = $this->valueAsCollection();
147
        foreach ($collection as $item) {
148
            if (!$this->checkMatchResult($criteria->evaluate($item))) {
149
                return $this;
150
            }
151
        }
152
153
        $this->pass();
154
155
        return $this;
156
    }
157
158
    /**
159
     * @param bool $result
160
     *
161
     * @return bool
162
     */
163
    private function checkMatchResult($result)
164
    {
165 View Code Duplication
        if ($result === true && $this->assertAll === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
166
            $this->fail($this->_('At least one items that match with the given criteria'));
167
168
            return false;
169
        }
170 View Code Duplication
        if ($result === false && $this->assertAll === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
171
            $this->fail($this->_('At least one items that not match with the given criteria'));
172
173
            return false;
174
        }
175
176
        return true;
177
    }
178
179
    /**
180
     * @return $this
181
     */
182
    public function isSorted()
183
    {
184
        return $this->isSortedUsing(new Comparator());
185
    }
186
187
    /**
188
     * @param ComparatorInterface $comparator
189
     *
190
     * @return $this
191
     */
192 View Code Duplication
    public function isSortedUsing(ComparatorInterface $comparator)
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...
193
    {
194
        list($item1, $item2) = $this->checkIsSorted($comparator);
195
        if ($item1 !== null && $item2 !== null) {
196
            $this->fail(
197
                $this->_("There are items [%s, %s] that aren't ordered in the given collection", $item1, $item2)
198
            );
199
        } else {
200
            $this->pass();
201
        }
202
203
        return $this;
204
    }
205
206
    /**
207
     * @return $this
208
     */
209
    public function isNotSorted()
210
    {
211
        return $this->isNotSortedUsing(new Comparator());
212
    }
213
214
    /**
215
     * @param ComparatorInterface $comparator
216
     *
217
     * @return $this
218
     */
219 View Code Duplication
    public function isNotSortedUsing(ComparatorInterface $comparator)
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...
220
    {
221
        list($item1, $item2) = $this->checkIsSorted($comparator);
222
        if ($item1 !== null && $item2 !== null) {
223
            $this->fail($this->_('The given collection is sorted'));
224
        } else {
225
            $this->pass();
226
        }
227
228
        return $this;
229
    }
230
231
    /**
232
     * @param ComparatorInterface $comparator
233
     *
234
     * @return array
235
     */
236
    private function checkIsSorted(ComparatorInterface $comparator)
237
    {
238
        $collection = $this->valueAsCollection();
239
        $last = null;
240
        foreach ($collection as $item) {
241
            if ($last !== null && $comparator->compare($last, $item) > 0) {
242
                return array($last, $item);
243
            }
244
        }
245
246
        return array(null, null);
247
    }
248
249
    /**
250
     * {@inheritdoc}
251
     */
252
    protected function valueIsSet($message = 'Collection is undefined')
253
    {
254
        return parent::valueIsSet($message);
255
    }
256
257
    /**
258
     * @throws LogicException
259
     *
260
     * @return \Cubiche\Core\Collections\CollectionInterface
261
     */
262
    protected function valueAsCollection()
263
    {
264
        $value = $this->valueIsSet()->getValue();
265
        if ($value instanceof CollectionInterface) {
266
            return $value;
267
        }
268
269
        throw new LogicException('Collection is undefined');
270
    }
271
}
272