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

PropertyTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 59
ccs 24
cts 28
cp 0.8571
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 9 3
A __set() 0 3 1
A setProperty() 0 13 3
A __get() 0 9 3
A strToStudlyCase() 0 3 1
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