Completed
Pull Request — master (#2)
by Timothy
02:54
created

ExtractorChain   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addExtractor() 0 4 1
C extract() 0 37 7
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
        if (count($dataSets) < 1) {
60
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (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...
61
        }
62
        
63
        if (!$this->mergeRecursively) {
64
            return array_merge(...$dataSets);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_merge(...$dataSets); (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...
65
        }
66
        
67
        $result = [];
68
        foreach ($dataSets as $data) {
69
            // Zend's function will overwrite keys with new values upon collision, as opposed to PHP's native
70
            // array_merge_recursive function which will generate a new array of merged values for that key.
71
            $result = ArrayUtils::merge($result, $data);
72
        }
73
        
74
        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...
75
    }
76
}
77