Completed
Pull Request — master (#28)
by Vitaliy
04:03
created

Checkbox   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 10
c 3
b 1
f 0
lcom 2
cbo 2
dl 0
loc 95
ccs 24
cts 24
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 3 1
A setValue() 0 12 4
A check() 0 5 1
A unCheck() 0 5 1
A isChecked() 0 3 1
A setLabel() 0 4 1
A render() 0 3 1
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
   * @package Fiv\Form
13
   */
14
  class Checkbox extends Element\Input {
15
16
    /**
17
     * @var int
18
     */
19
    protected $value = 0;
20
21
    /**
22
     * @var string
23
     */
24
    protected $label = '';
25
26
    /**
27
     * @var array
28
     */
29
    protected $attributes = [
30
      'type' => 'checkbox',
31
    ];
32
33
34
    /**
35
     * @inheritdoc
36
     */
37 3
    public function handle(FormData $data) {
38 3
      $this->setValue($data->get($this->getName(), 0));
39 3
    }
40
41
42
    /**
43
     * Value in checkbox can be true or false
44
     *
45
     * @param int $value
46
     * @return $this
47
     */
48 4
    public function setValue($value) {
49 4
      if ($value !== 1 and $value !== 0) {
50 1
        throw new \InvalidArgumentException('Expect int: 1 or 0');
51
      }
52
53 3
      if ($value === 1) {
54 3
        return $this->check();
55
      } else {
56 3
        return $this->unCheck();
57
      }
58
59
    }
60
61
62
    /**
63
     * @return $this
64
     */
65 3
    public function check() {
66 3
      $this->setAttribute('checked', 'checked');
67 3
      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...
68 3
      return $this;
69
    }
70
71
72
    /**
73
     * Set value to 0
74
     * @return $this
75
     */
76 3
    public function unCheck() {
77 3
      $this->removeAttribute('checked');
78 3
      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...
79 3
      return $this;
80
    }
81
82
83
    /**
84
     * @return bool
85
     */
86 4
    public function isChecked() {
87 4
      return $this->getValue() === 1;
88
    }
89
90
91
    /**
92
     * @param string $text
93
     * @return $this
94
     */
95 2
    public function setLabel($text) {
96 2
      $this->label = $text;
97 2
      return $this;
98
    }
99
100
101
    /**
102
     * @return string
103
     */
104 1
    public function render() {
105 1
      return '<label>' . parent::render() . $this->label . '</label>';
106
    }
107
108
  }