CheckboxList::render()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 0
cts 18
cp 0
rs 9.2
cc 3
eloc 15
nc 3
nop 0
crap 12
1
<?php
2
3
  namespace Fiv\Form\Element;
4
5
  use Fiv\Form\FormData;
6
7
8
  /**
9
   * Generate
10
   * ```html
11
   * <input type="checkbox" name="languages[]" value="en">
12
   * <input type="checkbox" name="languages[]" value="ru">
13
   * ```
14
   * html tags
15
   *
16
   * @author Ivan Shcherbak <[email protected]>
17
   */
18
  class CheckboxList extends Multiple {
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function handle(FormData $data) {
24
      $values = $data->get($this->getName());
25
      if ($values === null) {
26
        $values = [];
27
      } else {
28
        $values = (array) $values;
29
      }
30
31
      $this->setValue($values);
32
      return $this;
33
    }
34
35
36
    /**
37
     * @param mixed $data
38
     * @return $this
39
     */
40
    public function setValue($data) {
41
      if (!is_array($data)) {
42
        $data = (array) $data;
43
      }
44
45
      foreach ($data as $i => $value) {
46
        # remove invalid values
47
        if (!isset($this->options[$value])) {
48
          unset($data[$i]);
49
        }
50
      }
51
52
      $this->value = array_values($data);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values($data) of type array<integer,?> is incompatible with the declared type null|string of property $value.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
      return $this;
54
    }
55
56
57
    /**
58
     * @return array
59
     */
60
    public function getValue() {
61
      return (array) parent::getValue();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (array) parent::getValue(); (array) is incompatible with the return type of the parent method Fiv\Form\Element\BaseElement::getValue of type null|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...
62
    }
63
64
65
    /**
66
     * @param string $optionKey
67
     * @return bool
68
     */
69
    public function isChecked($optionKey) {
70
      return in_array($optionKey, $this->getValue());
71
    }
72
73
74
    /**
75
     * Alias of setValue
76
     *
77
     * @param mixed $values
78
     * @return $this
79
     */
80
    public function setChecked($values) {
81
      return $this->setValue($values);
82
    }
83
84
85
    /**
86
     * @return string
87
     */
88
    public function render() {
89
      $html = '';
90
      $currentValues = $this->getValue();
91
      $attributes = $this->attributes;
92
      $attributes['type'] = 'checkbox';
93
94
      $name = $this->getName();
95
96
      foreach ($this->options as $value => $text) {
97
        $attributes['value'] = $value;
98
        $attributes['name'] = $name . '[]';
99
        if (in_array($value, $currentValues)) {
100
          $attributes['checked'] = 'checked';
101
        } else {
102
          unset($attributes['checked']);
103
        }
104
105
        $html .= '<label>' . static::tag('input', $attributes) . $text . '</label>';
106
      }
107
108
      return $html;
109
    }
110
  }