Completed
Pull Request — master (#9)
by Bernhard
02:13
created

PropertyTrait::getProperty()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 8
cp 0.875
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
crap 4.0312
1
<?php
2
3
namespace GloBee\PaymentApi\Models;
4
5
trait PropertyTrait
6
{
7
    /**
8
     * @param $name
9
     *
10
     * @return mixed
11
     */
12 21
    public function getProperty($name)
13
    {
14 21
        $methodName = 'get'.$this->strToStudlyCase($name);
15 21
        if (method_exists($this, $methodName)) {
16
            return $this->{$methodName}();
17
        }
18
19 21
        if (array_key_exists($name, $this->properties)) {
20 17
            return $this->properties[$name];
21
        }
22
23 9
        if (array_key_exists($name, $this->readonlyProperties)) {
0 ignored issues
show
Bug introduced by
It seems like $this->readonlyProperties can also be of type null; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

23
        if (array_key_exists($name, /** @scrutinizer ignore-type */ $this->readonlyProperties)) {
Loading history...
Bug Best Practice introduced by
The property readonlyProperties does not exist on GloBee\PaymentApi\Models\PropertyTrait. Since you implemented __get, consider adding a @property annotation.
Loading history...
24 8
            return $this->readonlyProperties[$name];
25
        }
26 3
    }
27
28
    /**
29
     * @param $name
30
     * @param $value
31
     */
32 19
    protected function setProperty($name, $value)
33
    {
34 19
        $methodName = 'set'.$this->strToStudlyCase($name);
35 19
        if (method_exists($this, $methodName)) {
36 18
            $this->{$methodName}($value);
37
38 8
            return;
39
        }
40
41 4
        if (array_key_exists($name, $this->properties)) {
42 4
            $this->properties[$name] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property properties does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
44 4
            return;
45
        }
46
    }
47
48 21
    public function __get($name)
49
    {
50 21
        return $this->getProperty($name);
51
    }
52
53 19
    public function __set($name, $value)
54
    {
55 19
        $this->setProperty($name, $value);
56 9
    }
57
58
    /**
59
     * @param $key
60
     *
61
     * @return mixed
62
     */
63 31
    protected function strToStudlyCase($key)
64
    {
65 31
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
66
    }
67
}
68