Completed
Push — master ( 11675b...1688ee )
by A
04:37
created

checkedTrait::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
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];
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...
24
        }
25
26
        $this->checked = false;
27
28
        if (is_array($value)) {
29
            if (isAssoc($value)) {
30
                //W przypadku tablicy asocjacyjnej ($key=>$val) jeśli $val jest typu bool to znaczy że $key == $this->value
31
                if (is_bool(array_values($value)[0])) {
32
                    if (array_key_exists($this->value, $value)) {
33
                        $this->checked = $value[$this->value];
34
                        return $this;
35
                    }
36
                } else {
37
                    //W przeciwnym przypdaku uznajemy że tablica przechowuje pary klucz => wartosc odpowiadające $this->name => $this->value
38
                    if (array_key_exists($this->name, $value)) {
39
                        $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...
40
                        return $this;
41
                    }
42
                }
43
            } else {
44
                $this->checked = in_array($this->value, $value);
45
                return $this;
46
            }
47
48
            return $this;
49
        }
50
51
        //Jeżeli jest obiektem to obowiązkowo należy podać drugi parametr jakim jest nazwa pola
52
        if (is_object($value) && $oKey && $oName) {
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...
Bug Best Practice introduced by
The expression $oName 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...
53
            foreach($value as $v)
54
            {
55 View Code Duplication
                if (property_exists($v, $oKey))
0 ignored issues
show
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...
56
                {
57
                    $prop = $v->$oKey;
58
                    if (is_bool($prop))
59
                    {
60
                        $this->checked = $prop;
61
                    } else
62
                    {
63
                        $this->checked = $prop == $this->value;
64
                    }
65
                }
66
            }
67
            return $this;
68
        }
69
70
71 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...
72
            if (property_exists($value, $oKey))
73
            {
74
                $prop = $value->$oKey;
75
                if (is_bool($prop))
76
                {
77
                    $this->checked = $prop;
78
                } else
79
                {
80
                    $this->checked = $prop == $this->value;
81
                }
82
            }
83
        }
84
85
        if (is_bool($value)) {
86
            $this->checked = $value;
87
            return $this;
88
        }
89
90
        $this->checked = $this->value == $value;
91
92
        return $this;
93
    }
94
95
    /**
96
     * Pobiera atrybut checked dla html
97
     * @return string
98
     */
99
    public function getChecked() {
100
        return $this->checked ? 'checked' : '';
101
    }
102
103
    /**
104
     * Dla pól checkbox value powinno ustawiać checked na true lub false a nie zastepowac warość
105
     * @param null $value
106
     * @param null $key
107
     * @return null|string
108
     */
109
    public function value($value = null, $key = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
        if (is_null($value)) {
111
            return parent::value();
112
        }
113
        $this->checked($value);
114
    }
115
116
    /**
117
     * Jeżeli chcemy zastąpić wartość
118
     * @param null $value
119
     * @param null $key
120
     */
121
    public function setValue($value = null, $key = null) {
122
        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...
123
    }
124
125
}