Completed
Pull Request — master (#1)
by Timothy
05:37
created

ExtractorChain   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addExtractor() 0 4 1
B extract() 0 32 6
1
<?php
2
3
namespace Abacaphiliac\Extractor;
4
5
use Abacaphiliac\Extractor\Exception\UnexpectedValueException;
6
use Zend\Stdlib\ArrayUtils;
7
8
class ExtractorChain implements ExtractionInterface
9
{
10
    /** @var ExtractionInterface[] */
11
    private $extractors = [];
12
    
13
    /** @var bool */
14
    private $mergeRecursively;
15
16
    /**
17
     * PendoOptionsProviderChain constructor.
18
     * @param ExtractionInterface[] $extractors
19
     * @param bool $mergeRecursively
20
     */
21
    public function __construct(array $extractors = [], $mergeRecursively = true)
22
    {
23
        array_walk($extractors, [$this, 'addExtractor']);
24
        $this->mergeRecursively = $mergeRecursively;
25
    }
26
27
    /**
28
     * @param ExtractionInterface $extractor
29
     */
30
    private function addExtractor(ExtractionInterface $extractor)
31
    {
32
        $this->extractors[] = $extractor;
33
    }
34
35
    /**
36
     * @return mixed[]
37
     * @throws \Abacaphiliac\Extractor\Exception\UnexpectedValueException
38
     */
39
    public function extract()
40
    {
41
        $dataSets = array_map(function (ExtractionInterface $extractor) {
42
            $data = $extractor->extract();
43
            
44
            if ($data instanceof \Traversable) {
45
                $data = iterator_to_array($data);
46
            }
47
            
48
            if (!is_array($data)) {
49
                throw new UnexpectedValueException(sprintf(
50
                    'Extractor type `%s` returned a `%s` instead of an `array`',
51
                    get_class($extractor),
52
                    is_object($data) ? get_class($data) : gettype($data)
53
                ));
54
            }
55
56
            return $data;
57
        }, $this->extractors);
58
        
59
        $result = [];
60
        
61
        if ($this->mergeRecursively) {
62
            foreach ($dataSets as $data) {
63
                $result = ArrayUtils::merge($result, $data);
64
            }
65
        } else {
66
            $result = array_merge(...$dataSets);
67
        }
68
        
69
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (array) is incompatible with the return type declared by the interface Abacaphiliac\Extractor\E...ctionInterface::extract of type Traversable|array.

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...
70
    }
71
}
72