Passed
Push — main ( 0545d9...29d231 )
by Proyecto
03:14
created

SettersBag::__call()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8.304

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 27
ccs 12
cts 20
cp 0.6
rs 8.9777
cc 6
nc 6
nop 2
crap 8.304
1
<?php
2
3
namespace ProyectoTAU\TAU\Common;
4
5
trait SettersBag
6
{
7
    private $setters = [];
8
9 38
    protected function setSettersBag($attributes)
10
    {
11 38
        foreach ($attributes as $value) {
12 38
            $v = ucwords(strtolower($value));
13 38
            $this->setters[] = 'set' . $v;
14 38
            $this->setters[] = 'get' . $v;
15
        }
16 38
    }
17
18 39
    protected function isSetterAllowed($name): bool
19
    {
20 39
        return in_array($name, $this->setters, true);
21
    }
22
23 39
    public function __call($name, $arguments)
24
    {
25
        //TODO: hack ;-(
26 39
        if( in_array($name, ['setPropertiesBag', 'setSettersBag'], true) ){
27 32
            $this->$name(...$arguments);
28 32
            return;
29 39
        }elseif( in_array($name, ['getPropertiesBag','getSettersBag'], true) ){
30 32
            return $this->$name(...$arguments);
31
        }
32
33 39
        if( ! $this->isSetterAllowed($name) ) {
34
            $trace = debug_backtrace();
35
            trigger_error(
36
                'Disallowed getter/setter ' . $name .
37
                ' in ' . $trace[0]['file'] .
38
                ' on line ' . $trace[0]['line'],
39
                E_USER_NOTICE);
40
            return null;
41
        }
42
43 39
        $attribute = strtolower(substr($name, 3));
44 39
        if($this->isSetter($name)){
45 38
            $this->$attribute = $arguments[0];
46 36
        } elseif( $this->isGetter($name) ) {
47 36
            return $this->$attribute;
48
        } else {
49
            $name(...$arguments);
50
        }
51 38
    }
52
53 15
    public function equals($o): bool
54
    {
55 15
        foreach ($this->setters as $getter){
56 15
            if( $this->isGetter($getter) )
57
            {
58 15
                if( $this->$getter() != $o->$getter() )
59
                    return false;
60
            }
61
        }
62 15
        return true;
63
    }
64
65 39
    private function isSetter($name): bool
66
    {
67 39
        return substr($name, 0, 3) === 'set';
68
    }
69
70 36
    private function isGetter($name): bool
71
    {
72 36
        return substr($name, 0, 3) === 'get';
73
    }
74
}
75