Completed
Push — master ( 51b7cd...11675b )
by A
07:50
created

checkedTrait   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 125
Duplicated Lines 19.2 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 27
c 4
b 1
f 1
lcom 1
cbo 0
dl 24
loc 125
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
D checked() 24 82 20
A getChecked() 0 3 2
A value() 0 6 2
A setValue() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Code4\Forms\Traits;
3
4
trait checkedTrait
5
{
6
    protected $checked = false;
7
8
    /**
9
     * Sprawdza czy pole powinno być zaznaczone
10
     * @param string|array $value
11
     * @param string $oKey
12
     * @param string $oName ????
13
     * @return $this
14
     */
15
    public function checked($value = null, $oKey = null, $oName = null) {
16
17
        if (is_null($value)) {
18
            return $this->getChecked();
19
        }
20
21
        //Checkboxy $this->value powinny mieć wyłącznie jako "string"
22
        if (is_array($this->value)) {
23
            $this->value = $this->value[0];
24
        }
25
26
        $this->checked = false;
27
28
        //Sprawdzamy typ przekazanych danych.
29
        if (is_string($value)) {
30
            $this->checked = $this->value == $value;
31
            return $this;
32
        }
33
34
        if (is_array($value)) {
35
            if (isAssoc($value)) {
36
                //W przypadku tablicy asocjacyjnej ($key=>$val) jeśli $val jest typu bool to znaczy że $key == $this->value
37
                if (is_bool(array_values($value)[0])) {
38
                    if (array_key_exists($this->value, $value)) {
39
                        $this->checked = $value[$this->value];
40
                        return $this;
41
                    }
42
                } else {
43
                    //W przeciwnym przypdaku uznajemy że tablica przechowuje pary klucz => wartosc odpowiadające $this->name => $this->value
44
                    if (array_key_exists($this->name, $value)) {
45
                        $this->checked = $value[$this->name] == $this->value;
46
                        return $this;
47
                    }
48
                }
49
            } else {
50
                $this->checked = in_array($this->value, $value);
51
                return $this;
52
            }
53
54
            return $this;
55
        }
56
57
        //Jeżeli jest obiektem to obowiązkowo należy podać drugi parametr jakim jest nazwa pola
58
        if (is_object($value) && $oKey && $oName) {
59
            foreach($value as $v)
60
            {
61 View Code Duplication
                if (property_exists($v, $oKey))
62
                {
63
                    $prop = $v->$oKey;
64
                    if (is_bool($prop))
65
                    {
66
                        $this->checked = $prop;
67
                    } else
68
                    {
69
                        $this->checked = $prop == $this->value;
70
                    }
71
                }
72
            }
73
            return $this;
74
        }
75
76
77 View Code Duplication
        if (is_object($value) && $oKey) {
78
            if (property_exists($value, $oKey))
79
            {
80
                $prop = $value->$oKey;
81
                if (is_bool($prop))
82
                {
83
                    $this->checked = $prop;
84
                } else
85
                {
86
                    $this->checked = $prop == $this->value;
87
                }
88
            }
89
        }
90
91
        if (is_bool($value)) {
92
            $this->checked = $value;
93
            return $this;
94
        }
95
        return $this;
96
    }
97
98
    /**
99
     * Pobiera atrybut checked dla html
100
     * @return string
101
     */
102
    public function getChecked() {
103
        return $this->checked ? 'checked' : '';
104
    }
105
106
    /**
107
     * Dla pól checkbox value powinno ustawiać checked na true lub false a nie zastepowac warość
108
     * @param null $value
109
     * @param null $key
110
     * @return null|string
111
     */
112
    public function value($value = null, $key = null) {
113
        if (is_null($value)) {
114
            return parent::value($value, $key);
115
        }
116
        $this->checked($value);
117
    }
118
119
    /**
120
     * Jeżeli chcemy zastąpić wartość
121
     * @param null $value
122
     * @param null $key
123
     */
124
    public function setValue($value = null, $key = null) {
125
        parent::value($value, $key);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (value() instead of setValue()). Are you sure this is correct? If so, you might want to change this to $this->value().

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...
126
    }
127
128
}