Completed
Push — master ( 3a35d5...02819f )
by Gabriel
08:58
created

Nip_Form_Element_RadioGroup   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 58.81%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 3
dl 0
loc 53
ccs 10
cts 17
cp 0.5881
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getNewElement() 0 7 1
A setValue() 0 12 3
A autoSelectFirst() 0 4 1
A isAutoSelectFirst() 0 8 3
1
<?php
2
3
class Nip_Form_Element_RadioGroup extends Nip_Form_Element_Input_Group
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    protected $_type = 'radioGroup';
6
7
    /**
8
     * @return Nip_Form_Element_Abstract
9
     */
10 3
    public function getNewElement()
11
    {
12 3
        $element = $this->getForm()->getNewElement('radio');
13 3
        $element->setName($this->getName());
14
15 3
        return $element;
16
    }
17
18
    /**
19
     * @param $value
20
     * @return $this
21
     */
22
    public function setValue($value)
23
    {
24
        $elements = $this->getElements();
25
        foreach ($elements as $element) {
26
            if ($element->getValue() == $value) {
27
                $element->setChecked(true);
28
                break;
29
            }
30
        }
31
32
        return parent::setValue($value);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::setValue($value); (Nip_Form_Element_RadioGroup) is incompatible with the return type of the parent method Nip\Form\Elements\AbstractElement::setValue of type Nip\Form\Elements\Traits\HasAttributesTrait.

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...
33
    }
34
35
    /**
36
     * @param $boolean
37
     * @return Nip_Form_Element_RadioGroup
38
     */
39 2
    public function autoSelectFirst($boolean = true)
40
    {
41 2
        return $this->setOption('autoSelectFirst', $boolean === true);
42
    }
43
44
    /**
45
     * @return bool
46
     */
47 5
    public function isAutoSelectFirst()
48
    {
49 5
        if ($this->hasOption('autoSelectFirst') && $this->getOption('autoSelectFirst') == false) {
50 1
            return false;
51
        }
52
53 4
        return true;
54
    }
55
}
56