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

SettersBag::equals()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 10
cc 4
nc 4
nop 1
crap 4.074
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