Completed
Push — master ( 3cc3b2...768def )
by Vitaliy
01:53
created

Checkbox::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
  namespace Fiv\Form\Element;
4
5
  use Fiv\Form\Element;
6
  use Fiv\Form\FormData;
7
8
  /**
9
   * Generate <input type="submit" /> element
10
   *
11
   * @author Ivan Shcherbak <[email protected]>
12
   */
13
  class Checkbox extends Element\Input {
14
15
    /**
16
     * @var int
17
     */
18
    protected $value = 0;
19
20
    /**
21
     * @var string
22
     */
23
    protected $label = '';
24
25
    /**
26
     * @var array
27
     */
28
    protected $attributes = [
29
      'type' => 'checkbox',
30
    ];
31
32
33
    /**
34
     * @inheritdoc
35
     */
36 7
    public function handle(FormData $data) {
37 7
      $value = $data->get($this->getName());
38 7
      $value = ($value === null) ? 0 : (int) $value;
39 7
      $this->setValue($value);
40 7
      return $this;
41
    }
42
43
44
    /**
45
     * Value in checkbox can be true or false
46
     *
47
     * @param int $value
48
     * @return $this
49
     */
50 8
    public function setValue($value) {
51 8
      if ($value !== 1 and $value !== 0) {
52 1
        throw new \InvalidArgumentException('Expect int: 1 or 0');
53
      }
54
55 7
      if ($value === 1) {
56 5
        return $this->check();
57
      } else {
58 5
        return $this->unCheck();
59
      }
60
61
    }
62
63
64
    /**
65
     * @return $this
66
     */
67 5
    public function check() {
68 5
      $this->setAttribute('checked', 'checked');
69 5
      parent::setValue(1);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setValue() instead of check()). Are you sure this is correct? If so, you might want to change this to $this->setValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
70 5
      return $this;
71
    }
72
73
74
    /**
75
     * Set value to 0
76
     * @return $this
77
     */
78 7
    public function unCheck() {
79 7
      $this->removeAttribute('checked');
80 7
      parent::setValue(0);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setValue() instead of unCheck()). Are you sure this is correct? If so, you might want to change this to $this->setValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
81 7
      return $this;
82
    }
83
84
85
    /**
86
     * @return bool
87
     */
88 4
    public function isChecked() {
89 4
      return $this->getValue() === 1;
90
    }
91
92
93
    /**
94
     * @param string $text
95
     * @return $this
96
     */
97 6
    public function setLabel($text) {
98 6
      $this->label = $text;
99 6
      return $this;
100
    }
101
102
103
    /**
104
     * @return string
105
     */
106 1
    public function render() {
107 1
      return '<label>' . parent::render() . $this->label . '</label>';
108
    }
109
110
  }