SetAsserter   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setWith() 14 14 3
A contains() 11 11 2
A containsValues() 8 8 2
A notContains() 11 11 2
A valueAsCollection() 9 9 2

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
/**
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
namespace Cubiche\Core\Collections\Tests\Asserters;
12
13
use Cubiche\Core\Collections\SetInterface;
14
use Cubiche\Core\Specification\Criteria;
15
use mageekguy\atoum\exceptions\logic as LogicException;
16
17
/**
18
 * SetAsserter class.
19
 *
20
 * @author Ivannis Suárez Jerez <[email protected]>
21
 */
22 View Code Duplication
class SetAsserter extends CollectionAsserter
0 ignored issues
show
Duplication introduced by
This class 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...
23
{
24
    /**
25
     * @var bool
26
     */
27
    protected $assertAll;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function setWith($value, $checkType = true)
33
    {
34
        parent::setWith($value, $checkType);
35
36
        if ($checkType === true) {
37
            if ($this->value instanceof SetInterface) {
38
                $this->pass();
39
            } else {
40
                $this->fail($this->getLocale()->_('%s is not a list', $this));
41
            }
42
        }
43
44
        return $this;
45
    }
46
47
    /**
48
     * @param mixed $value
49
     *
50
     * @return $this
51
     */
52
    public function contains($value)
53
    {
54
        $collection = $this->valueAsCollection();
55
        if ($collection->findOne(Criteria::eq($value)) !== null) {
56
            $this->pass();
57
        } else {
58
            $this->fail($this->getLocale()->_('The set not contain the value %s', $value));
59
        }
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param array|\Traversable $values
66
     *
67
     * @return $this
68
     */
69
    public function containsValues($values)
70
    {
71
        foreach ($values as $value) {
72
            $this->contains($value);
73
        }
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param mixed $value
80
     *
81
     * @return $this
82
     */
83
    public function notContains($value)
84
    {
85
        $collection = $this->valueAsCollection();
86
        if ($collection->findOne(Criteria::eq($value)) !== null) {
87
            $this->fail($this->getLocale()->_('The set contain an element with this value %s', $value));
88
        } else {
89
            $this->pass();
90
        }
91
92
        return $this;
93
    }
94
95
    /**
96
     * @throws LogicException
97
     *
98
     * @return \Cubiche\Core\Collections\SetInterface
99
     */
100
    protected function valueAsCollection()
101
    {
102
        $value = $this->valueIsSet()->getValue();
103
        if ($value instanceof SetInterface) {
104
            return $value;
105
        }
106
107
        throw new LogicException('Set is undefined');
108
    }
109
}
110