Completed
Push — master ( 7a35bc...7eff79 )
by Krzysztof
05:49 queued 02:58
created

CriteriaCollection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 1
dl 70
loc 70
rs 10
ccs 24
cts 24
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 2
A getApplicableCriteria() 9 9 1
A getCriteria() 4 4 1
A addCriteria() 6 6 1
A checkType() 12 12 4

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 KGzocha\Searcher\Criteria\Collection;
4
5
use KGzocha\Searcher\Criteria\CriteriaInterface;
6
7
/**
8
 * @author Krzysztof Gzocha <[email protected]>
9
 * @author Daniel Ribeiro <[email protected]>
10
 */
11 View Code Duplication
class CriteriaCollection implements CriteriaCollectionInterface
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...
12
{
13
    /**
14
     * @var CriteriaInterface[]
15
     */
16
    protected $criteria;
17
18
    /**
19
     * @param CriteriaInterface[] $providedCriteria array or \Traversable object
20
     */
21 18
    public function __construct($providedCriteria = [])
22
    {
23 18
        $this->criteria = [];
24 18
        $this->checkType($providedCriteria);
25 8
        foreach ($providedCriteria as $criteria) {
26
            // In this way we will ensure that
27
            // every element in array has correct type
28 5
            $this->addCriteria($criteria);
29
        }
30 8
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 2
    public function getApplicableCriteria()
36
    {
37 2
        return array_filter(
38 2
            $this->getCriteria(),
39 2
            function (CriteriaInterface $criteria) {
40 2
                return $criteria->shouldBeApplied();
41 2
            }
42
        );
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 6
    public function getCriteria()
49
    {
50 6
        return $this->criteria;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 5
    public function addCriteria(CriteriaInterface $criteria)
57
    {
58 5
        $this->criteria[] = $criteria;
59
60 5
        return $this;
61
    }
62
63
    /**
64
     * Will ensure that provided criteria are array or traversable object.
65
     *
66
     * @param mixed $criteria
67
     */
68 18
    private function checkType($criteria)
69
    {
70 18
        if (is_array($criteria)
71 18
            || $criteria instanceof \Traversable) {
72 8
            return;
73
        }
74
75 10
        throw new \InvalidArgumentException(sprintf(
76 10
            'Provided criteria should be an array or \Traversable object. Given: %s',
77 10
            is_object($criteria) ? get_class($criteria) : gettype($criteria)
78
        ));
79
    }
80
}
81