Passed
Push — main ( 3fc6af...0545d9 )
by Proyecto
08:13
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
use phpDocumentor\Reflection\Types\True_;
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Reflection\Types\True_ was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
trait SettersBag
8
{
9
    private $setters = [];
10
11 31
    protected function setSettersBag($attributes)
12
    {
13 31
        foreach ($attributes as $value) {
14 31
            $v = ucwords(strtolower($value));
15 31
            $this->setters[] = 'set' . $v;
16 31
            $this->setters[] = 'get' . $v;
17
        }
18 31
    }
19
20 39
    protected function isSetterAllowed($name): bool
21
    {
22 39
        return in_array($name, $this->setters, true);
23
    }
24
25 39
    public function __call($name, $arguments)
26
    {
27 39
        if( ! $this->isSetterAllowed($name) ) {
28
            $trace = debug_backtrace();
29
            trigger_error(
30
                'Disallowed getter/setter ' . $name .
31
                ' in ' . $trace[0]['file'] .
32
                ' on line ' . $trace[0]['line'],
33
                E_USER_NOTICE);
34
            return null;
35
        }
36
37 39
        $attribute = strtolower(substr($name, 3));
38 39
        if($this->isSetter($name)){
39 31
            $this->$attribute = $arguments[0];
40 36
        } elseif( $this->isGetter($name) ) {
41 36
            return $this->$attribute;
42
        } else {
43
            $name(...$arguments);
44
        }
45 31
    }
46
47 19
    public function equals($o): bool
48
    {
49 19
        foreach ($this->setters as $getter){
50 19
            if( $this->isGetter($getter) )
51
            {
52 19
                if( $this->$getter() != $o->$getter() )
53
                    return false;
54
            }
55
        }
56 19
        return true;
57
    }
58
59 39
    private function isSetter($name): bool
60
    {
61 39
        return substr($name, 0, 3) === 'set';
62
    }
63
64 36
    private function isGetter($name): bool
65
    {
66 36
        return substr($name, 0, 3) === 'get';
67
    }
68
}
69