MultiSelect::filterValue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 12
ccs 7
cts 8
cp 0.875
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
crap 3.0175
1
<?php
2
namespace Sirius\Input\Element\Input;
3
4
use Sirius\Input\Specs;
5
6
class MultiSelect extends Select
7
{
8
9 3
    protected function getDefaultSpecs()
10
    {
11
        return array(
0 ignored issues
show
Best Practice introduced by
The expression return array(\Sirius\Inp... array('size' => '5')); seems to be an array, but some of its elements' types (array<string,string>) are incompatible with the return type of the parent method Sirius\Input\Element\Input\Select::getDefaultSpecs of type array<*,string>.

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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
12 3
            Specs::WIDGET     => 'multiselect',
13 3
            Specs::ATTRIBUTES => array(
14
                'size' => '5'
15 3
            )
16 3
        );
17
    }
18
19
    /**
20
     * Filters out the input value if it is not in the options set
21
     *
22
     * @param mixed $value
23
     * @param null|string $valueIdentifier
24
     *
25
     * @return mixed
26
     */
27 1
    public function filterValue($value, $valueIdentifier = null)
28
    {
29 1
        if (!$value) {
30
            return null;
31
        }
32 1
        if (!is_array($value)) {
33 1
            $value = (array) $value;
34 1
        }
35 1
        $allowedValues = array_keys($this->getChoices());
36
37 1
        return array_intersect($value, $allowedValues);
38
    }
39
}
40