Passed
Push — master ( d26101...1eb4af )
by Gabor
03:11
created

CheckboxElement   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 36
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1
ccs 10
cts 10
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 10 3
A getValue() 0 8 2
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
namespace WebHemi\Form\Element\Web;
13
14
/**
15
 * Class RadioElement.
16
 */
17
class CheckboxElement extends RadioElement
18
{
19
    /** @var string */
20
    protected $type = 'checkbox';
21
22
    /**
23
     * Sets element value.
24
     *
25
     * @param mixed $value
26
     * @return RadioElement
27
     */
28 3
    public function setValue($value)
29
    {
30 3
        if (empty($this->options)) {
31 1
            $this->value = boolval($value) ? 1 : 0;
32 1
        } else {
33 2
            parent::setValue($value);
34
        }
35
36 3
        return $this;
37
    }
38
39
    /**
40
     * Returns element value.
41
     *
42
     * @return mixed
43
     */
44 3
    public function getValue()
45
    {
46 3
        if (empty($this->options)) {
47 1
            return (int)$this->value;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (int) $this->value; (integer) is incompatible with the return type of the parent method WebHemi\Form\Element\Web\RadioElement::getValue of type 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...
48
        }
49
50 2
        return parent::getValue();
51
    }
52
}
53