Completed
Pull Request — master (#86)
by Krzysztof
02:32
created

CriteriaCollection::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 4
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 29
    public function __construct($providedCriteria = [])
22
    {
23 29
        $this->criteria = [];
24 29
        $this->checkType($providedCriteria);
25 19
        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 19
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 3
    public function getApplicableCriteria()
36
    {
37 3
        return new self(array_filter(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new self(array_fi...shouldBeApplied(); })); (KGzocha\Searcher\Criteri...tion\CriteriaCollection) is incompatible with the return type declared by the interface KGzocha\Searcher\Criteri...::getApplicableCriteria of type KGzocha\Searcher\Criteria\CriteriaInterface[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
38 3
            $this->getCriteria(),
39 3
            function(CriteriaInterface $criteria) {
40 3
                return $criteria->shouldBeApplied();
41 3
            }
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 7
    public function addCriteria(CriteriaInterface $criteria)
57
    {
58 7
        $this->criteria[] = $criteria;
59
60 7
        return $this;
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66 1
    public function getIterator()
67
    {
68 1
        return new \ArrayIterator($this->criteria);
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74 2
    public function count()
75
    {
76 2
        return count($this->criteria);
77
    }
78
79
    /**
80
     * Will ensure that provided criteria are array or traversable object.
81
     *
82
     * @param mixed $criteria
83
     */
84 29
    private function checkType($criteria)
85
    {
86 29
        if (is_array($criteria)
87 29
            || $criteria instanceof \Traversable) {
88 19
            return;
89
        }
90
91 10
        throw new \InvalidArgumentException(sprintf(
92 10
            'Provided criteria should be an array or \Traversable object. Given: %s',
93 10
            is_object($criteria) ? get_class($criteria) : gettype($criteria)
94
        ));
95
    }
96
}
97