Passed
Pull Request — master (#2)
by Bernhard
02:00
created

PropertyTrait::setProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3
1
<?php
2
3
namespace GloBee\PaymentApi\Models;
4
5
trait PropertyTrait
6
{
7
    /**
8
     * @param $key
9
     * @param $value
10
     */
11 4
    protected function setProperty($name, $value)
12
    {
13 4
        $methodName = 'set'.$this->strToStudlyCase($name);
14 4
        if (method_exists($this, $methodName)) {
15 2
            $this->{$methodName}($value);
16
17 2
            return;
18
        }
19
20 4
        if (property_exists($this, $name)) {
21 4
            $this->{$name} = $value;
22
23 4
            return;
24
        }
25
    }
26
27 6
    public function __call($name, $arguments)
28
    {
29 6
        $mutator = substr($name, 0, 3);
30 6
        if ($mutator === 'get') {
31 6
            return $this->__get(lcfirst(substr($name, 3)));
32
        }
33
34 1
        if ($mutator === 'set') {
35 1
            return $this->__set(lcfirst(substr($name, 3)), $arguments[0]);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->__set(lcfirst(sub...me, 3)), $arguments[0]) targeting GloBee\PaymentApi\Models\PropertyTrait::__set() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
36
        }
37
    }
38
39 10
    public function __get($name)
40
    {
41 10
        $methodName = 'get'.$this->strToStudlyCase($name);
42 10
        if (method_exists($this, $methodName)) {
43
            return $this->{$methodName}();
44
        }
45
46 10
        if (property_exists($this, $name)) {
47 10
            return $this->{$name};
48
        }
49
    }
50
51 4
    public function __set($name, $value)
52
    {
53 4
        $this->setProperty($name, $value);
54 4
    }
55
56
    /**
57
     * @param $key
58
     *
59
     * @return mixed
60
     */
61 10
    protected function strToStudlyCase($key)
62
    {
63 10
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
64
    }
65
}
66