checkedTrait::getChecked()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 2
eloc 2
nc 2
nop 0
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
     * @return $this
13
     */
14
    public function checked($value = null, $oKey = null) {
15
16
        if (is_null($value)) {
17
            return $this;
18
        }
19
20
        //Checkboxy $this->value powinny mieć wyłącznie jako "string"
21
        if (is_array($this->value)) {
22
            $this->value = $this->value[0];
0 ignored issues
show
Bug introduced by
The property value does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
        }
24
25
        $this->checked = false;
26
27
        if (is_array($value)) {
28
            if (isAssoc($value)) {
29
                //W przypadku tablicy asocjacyjnej ($key=>$val) jeśli $val jest typu bool to znaczy że $key == $this->value
30
                if (is_bool(array_values($value)[0])) {
31
                    if (array_key_exists($this->value, $value)) {
32
                        $this->checked = $value[$this->value];
33
                        return $this;
34
                    }
35
                } else {
36
                    //W przeciwnym przypdaku uznajemy że tablica przechowuje pary klucz => wartosc odpowiadające $this->name => $this->value
37
                    if (array_key_exists($this->name, $value)) {
38
                        $this->checked = $value[$this->name] == $this->value;
0 ignored issues
show
Bug introduced by
The property name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
                        return $this;
40
                    }
41
                }
42
            } else {
43
                $this->checked = in_array($this->value, $value);
44
                return $this;
45
            }
46
47
            return $this;
48
        }
49
50
        //Jeżeli jest kolekcją obiektów to obowiązkowo należy podać drugi parametr jakim jest nazwa pola
51 View Code Duplication
        if (is_object($value) && $oKey) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $oKey of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
            foreach($value as $v)
53
            {
54
                if (property_exists($v, $oKey))
55
                {
56
                    $prop = $v->$oKey;
57
                    if (is_bool($prop))
58
                    {
59
                        $this->checked = $prop;
60
                    } else
61
                    {
62
                        $this->checked = $prop == $this->value;
63
                    }
64
                }
65
            }
66
            return $this;
67
        }
68
69
        //Jeżeli jest obiektem
70 View Code Duplication
        if (is_object($value) && $oKey) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $oKey of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
            if (property_exists($value, $oKey))
72
            {
73
                $prop = $value->$oKey;
74
                if (is_bool($prop))
75
                {
76
                    $this->checked = $prop;
77
                } else
78
                {
79
                    $this->checked = $prop == $this->value;
80
                }
81
            }
82
        }
83
84
        if (is_bool($value)) {
85
            $this->checked = $value;
86
            return $this;
87
        }
88
89
        $this->checked = $this->value == $value;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Pobiera atrybut checked dla html
96
     * @return string
97
     */
98
    public function getChecked() {
99
        return $this->checked ? 'checked' : '';
100
    }
101
102
    /**
103
     * Dla pól checkbox value powinno ustawiać checked na true lub false a nie zastepowac warość
104
     * @param null $value
105
     * @param null $key
106
     * @return null|string
107
     */
108
    public function value($value = null, $key = null) {
109
        if (is_null($value)) {
110
            return parent::value();
111
        }
112
        return $this->checked($value, $key);
113
    }
114
115
    /**
116
     * Jeżeli chcemy zastąpić wartość
117
     * @param null $value
118
     * @param null $key
119
     */
120
    public function setValue($value = null, $key = null) {
121
        return 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...
122
    }
123
124
}