RetailerFilterIterator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 51
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 4
A accept() 0 18 3
1
<?php
2
namespace Germania\Retailers;
3
4
class RetailerFilterIterator extends \FilterIterator
5
{
6
7
    /**
8
     * @var array
9
     */
10
    public $retailer_filter;
11
12
13
    /**
14
     * @param \Traversable $collection      Collecrtion of RetailerNumberProviderInterface
15
     * @param array|int    $retailer_filter The Salesman ID to filter for
16
     */
17 225
    public function __construct( \Traversable $collection, $retailer_filter )
18
    {
19
        // Allow for getRetailerNumber
20 225
        $this->retailer_filter = $retailer_filter instanceOf RetailerNumberProviderInterface
21 105
        ? $retailer_filter->getRetailerNumber()
22 135
        : $retailer_filter;
23
24
        // Cast to array
25 225
        if (!is_array($this->retailer_filter)) {
26 120
            $this->retailer_filter = array( $this->retailer_filter );
27 24
        }
28
29
        // Take care of Traversable's two faces,
30
        // since both IteratorAggregate and Iterator implement Traversable
31 225
        parent::__construct( $collection instanceOf \IteratorAggregate ? $collection->getIterator() : $collection );
32
33 225
    }
34
35
36 225
    public function accept()
37
    {
38 225
        $item = $this->getInnerIterator()->current();
39
40
        // Disclose items not implementing RetailerNumberProviderInterface
41 225
        if (!$item instanceOf RetailerNumberProviderInterface) {
42 75
            return false;
43
        }
44
45
        // Cast to array
46 150
        $item_retailer_number = $item->getRetailerNumber();
47 150
        if (!is_array($item_retailer_number)) {
48 150
            $item_retailer_number = array( $item_retailer_number );
49 30
        }
50
51
        // Check intersection
52 150
        return array_intersect($this->retailer_filter, $item_retailer_number);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_intersect($...$item_retailer_number); (array) is incompatible with the return type declared by the abstract method FilterIterator::accept of type boolean.

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...
53
    }
54
}
55