Completed
Push — master ( 897b5a...5205fa )
by Shcherbak
44:54 queued 29:47
created

Checkbox::setLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
  namespace Fiv\Form\Element;
4
5
  use Fiv\Form\Element;
6
7
  /**
8
   * Generate <input type="submit" /> element
9
   *
10
   * @author Ivan Shcherbak <[email protected]>
11
   * @package Fiv\Form
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
     * Value in checkbox can be true or false
35
     *
36
     * @param int $value
37
     * @return $this
38
     */
39
    public function setValue($value) {
40
      if ($value !== 1 and $value !== 0) {
41
        throw new \InvalidArgumentException('Expect int: 1 or 0');
42
      }
43
44
      if ($value === 1) {
45
        return $this->check();
46
      } else {
47
        return $this->unCheck();
48
      }
49
50
    }
51
52
53
    /**
54
     * @return $this
55
     */
56
    public function check() {
57
      $this->setAttribute('checked', 'checked');
58
      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...
59
      return $this;
60
    }
61
62
63
    /**
64
     * Set value to 0
65
     * @return $this
66
     */
67
    public function unCheck() {
68
      $this->removeAttribute('checked');
69
      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...
70
      return $this;
71
    }
72
73
74
    /**
75
     * @return bool
76
     */
77
    public function isChecked() {
78
      return $this->getValue() === 1;
79
    }
80
81
82
    /**
83
     * @param string $text
84
     * @return $this
85
     */
86
    public function setLabel($text) {
87
      $this->label = $text;
88
      return $this;
89
    }
90
91
92
    /**
93
     * @return string
94
     */
95
    public function render() {
96
      return '<label>' . parent::render() . $this->label . '</label>';
97
    }
98
99
  }