Completed
Push — master ( 58c520...4309e0 )
by Krzysztof
03:13
created

CriteriaBuilderCollection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 1
dl 87
loc 87
ccs 28
cts 28
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 2
A addCriteriaBuilder() 6 6 1
A getCriteriaBuilders() 4 4 1
A getCriteriaBuildersForContext() 10 10 1
A getIterator() 4 4 1
A count() 4 4 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\CriteriaBuilder\Collection;
4
5
use KGzocha\Searcher\Context\SearchingContextInterface;
6
use KGzocha\Searcher\CriteriaBuilder\CriteriaBuilderInterface;
7
8
/**
9
 * @author Krzysztof Gzocha <[email protected]>
10
 */
11 View Code Duplication
class CriteriaBuilderCollection implements CriteriaBuilderCollectionInterface
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 CriteriaBuilderInterface[]
15
     */
16
    private $criteriaBuilders;
17
18
    /**
19
     * @param CriteriaBuilderInterface[] $builders array or \Traversable object
20
     */
21 9
    public function __construct($builders = [])
22
    {
23 9
        $this->criteriaBuilders = [];
24 9
        $this->checkType($builders);
25 4
        foreach ($builders as $builder) {
26
            // In this way we will ensure that
27
            // every element in array has correct type
28 3
            $this->addCriteriaBuilder($builder);
29
        }
30 4
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 4
    public function addCriteriaBuilder(CriteriaBuilderInterface $criteriaBuilder)
36
    {
37 4
        $this->criteriaBuilders[] = $criteriaBuilder;
38
39 4
        return $this;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function getCriteriaBuilders()
46
    {
47 3
        return $this->criteriaBuilders;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    public function getCriteriaBuildersForContext(
54
        SearchingContextInterface $searchingContext
55
    ) {
56 2
        return new self(array_filter(
57 2
            $this->getCriteriaBuilders(),
58 2
            function(CriteriaBuilderInterface $criteriaBuilder) use ($searchingContext) {
59 2
                return $criteriaBuilder->supportsSearchingContext($searchingContext);
60 2
            }
61
        ));
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67 1
    public function getIterator()
68
    {
69 1
        return new \ArrayIterator($this->criteriaBuilders);
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75 1
    public function count()
76
    {
77 1
        return count($this->criteriaBuilders);
78
    }
79
80
    /**
81
     * Will ensure that provided criteria are array or traversable object.
82
     *
83
     * @param mixed $criteriaBuilders
84
     */
85 9
    private function checkType($criteriaBuilders)
86
    {
87 9
        if (is_array($criteriaBuilders)
88 9
            || $criteriaBuilders instanceof \Traversable) {
89 4
            return;
90
        }
91
92 5
        throw new \InvalidArgumentException(sprintf(
93 5
            'Provided criteria builders should be an array or \Traversable object. Given: %s',
94 5
            is_object($criteriaBuilders) ? get_class($criteriaBuilders) : gettype($criteriaBuilders)
95
        ));
96
    }
97
}
98