GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b864c9...ed9166 )
by butschster
12:41
created

Range::apply()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 41
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 31
nc 11
nop 5
dl 0
loc 41
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Display\Column\Filter;
4
5
use SleepingOwl\Admin\Contracts\Display\Extension\ColumnFilterInterface;
6
7
class Range extends BaseColumnFilter
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $view = 'column.filter.range';
13
14
    /**
15
     * @var ColumnFilterInterface
16
     */
17
    protected $from;
18
19
    /**
20
     * @var ColumnFilterInterface
21
     */
22
    protected $to;
23
24
    /**
25
     * Initialize column filter.
26
     */
27
    public function initialize()
28
    {
29
        parent::initialize();
30
31
        $this->setHtmlAttribute('data-type', 'range');
32
        $this->setHtmlAttribute('class', 'column-filter');
33
34
        $this->getFrom()->initialize();
35
        $this->getTo()->initialize();
36
37
        $this->getFrom()->removeHtmlAttribute('data-type');
38
        $this->getTo()->removeHtmlAttribute('data-type');
39
    }
40
41
    /**
42
     * @return ColumnFilterInterface
43
     */
44
    public function getFrom()
45
    {
46
        return $this->from;
47
    }
48
49
    /**
50
     * @param ColumnFilterInterface $from
51
     *
52
     * @return $this
53
     */
54
    public function setFrom(ColumnFilterInterface $from)
55
    {
56
        $this->from = $from;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @return ColumnFilterInterface
63
     */
64
    public function getTo()
65
    {
66
        return $this->to;
67
    }
68
69
    /**
70
     * @param ColumnFilterInterface $to
71
     *
72
     * @return $this
73
     */
74
    public function setTo(ColumnFilterInterface $to)
75
    {
76
        $this->to = $to;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    public function toArray()
85
    {
86
        return parent::toArray() + [
87
            'from' => $this->getFrom(),
88
            'to'   => $this->getTo(),
89
        ];
90
    }
91
92
    /**
93
     * @param string $range
94
     *
95
     * @return string
96
     */
97
    public function parseValue($range)
98
    {
99
        if (strpos($range, '::') === false) {
100
            return;
101
        }
102
103
        list($from, $to) = explode('::', $range, 2);
104
        $from = $this->from->parseValue($from);
105
        $to = $this->to->parseValue($to);
106
107
        if (! empty($from) && ! empty($to)) {
108
            $this->setOperator('between');
109
110
            return [$from, $to];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array($from, $to); (array) is incompatible with the return type documented by SleepingOwl\Admin\Displa...ilter\Range::parseValue of type 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 '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...
111
        } elseif (! empty($from)) {
112
            $this->setOperator('greater_or_equal');
113
114
            return $from;
115
        } elseif (! empty($to)) {
116
            $this->setOperator('less_or_equal');
117
118
            return $to;
119
        }
120
    }
121
}
122