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

SettersBag   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 34
c 1
b 0
f 0
dl 0
loc 68
ccs 30
cts 39
cp 0.7692
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isSetterAllowed() 0 3 1
A setSettersBag() 0 6 2
A isGetter() 0 3 1
A equals() 0 10 4
A isSetter() 0 3 1
B __call() 0 27 6
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