HandlerAggregate   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 56
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addHandler() 0 6 1
B process() 0 20 7
A getIterator() 0 4 1
1
<?php
2
declare(strict_types=1);
3
namespace Sirius\Upload;
4
5
use Sirius\Upload\Result\Collection;
6
use Sirius\Upload\Util\Helper;
7
use Sirius\Validation\Util\Arr;
8
9
class HandlerAggregate implements \IteratorAggregate
10
{
11
    protected $handlers = [];
12
13
    /**
14
     * Adds a handler on the aggregate
15
     *
16
     * @param  string  $selector
17
     * @param  Handler $handler
18
     * @return $this
19 2
     */
20
    public function addHandler($selector, Handler $handler)
21 2
    {
22
        $this->handlers[$selector] = $handler;
23 2
24
        return $this;
25
    }
26
27
    /**
28
     * Processes
29
     * @param $files
30
     * @return Collection
31 1
     */
32
    public function process($files)
33 1
    {
34 1
        $result = new Collection();
35
        foreach ($this->handlers as $selector => $handler) {
36 1
            /* @var $handler Handler */
37
            $selectedFiles = Arr::getBySelector($files, $selector);
38 1
39
            if (!$selectedFiles || !is_array($selectedFiles) || empty($selectedFiles)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $selectedFiles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40
                continue;
41
            }
42 1
43 1
            foreach ($selectedFiles as $path => $file) {
44 1
                if (is_array($file)) {
45 1
                    $result[$path] = $handler->process($file);
46 1
                }
47 1
            }
48
        }
49 1
50
        return $result;
51
    }
52
53
    /**
54
     * Retrieve an external iterator
55
     *
56
     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
57
     * @return \Traversable An instance of an object implementing <b>Iterator</b> or
58
     *                      <b>Traversable</b>
59
     */
60 1
    public function getIterator()
61
    {
62 1
        return $this->handlers;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->handlers; (array) is incompatible with the return type declared by the interface IteratorAggregate::getIterator of type Traversable.

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...
63
    }
64
}
65