AttributesTrait::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Bludata\Common\Traits;
4
5
use ArrayAccess;
6
use InvalidArgumentException;
7
8
trait AttributesTrait
9
{
10
    public function getAttributes()
11
    {
12
        $attributes = array_keys(
13
            get_class_vars(
14
                get_class($this)
15
            )
16
        );
17
18
        return array_combine(
19
            $attributes,
20
            array_map(
21
                function ($attr) {
22
                    $getMethod = $this->getMethod($attr);
23
                    if (method_exists($this, $getMethod)) {
24
                        return $this->$getMethod();
25
                    }
26
27
                    return $this->$attr;
28
                },
29
                $attributes
30
            )
31
        );
32
    }
33
34
    public function getMethod($attr)
35
    {
36
        return sprintf('get%s', ucfirst($attr));
37
    }
38
39
    public function setMethod($attr)
40
    {
41
        return sprintf('set%s', ucfirst($attr));
42
    }
43
44
    public function get($attr)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
45
    {
46
        $attributes = $this->getAttributes();
0 ignored issues
show
Unused Code introduced by
$attributes is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
47
48
        $getMethod = $this->getMethod($attr);
49
        if (method_exists($this, $getMethod)) {
50
            return $this->$getMethod();
51
        }
52
53
        throw new InvalidArgumentException(
54
            sprintf(
55
                'The class "%s" don\'t have a method "%s"',
56
                get_class($this),
57
                $getMethod
58
            )
59
        );
60
    }
61
62
    public function set($attr, $value)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
63
    {
64
        $setMethod = $this->setMethod($attr);
65
66
        if (method_exists($this, $setMethod)) {
67
            return $this->$setMethod($value);
68
        }
69
70
        throw new InvalidArgumentException(
71
            sprintf(
72
                'The class "%s" don\'t have a method "%s"',
73
                get_class($this),
74
                $setMethod
75
            )
76
        );
77
    }
78
79
    public function toArray()
80
    {
81
        return array_map(function ($attr) {
82
            if (method_exists($attr, 'toArray')) {
83
                return $attr->toArray();
84
            }
85
86
            if ($attr instanceof ArrayAccess) {
87
                $newParam = [];
88
                foreach ($attr as $p) {
89
                    if (method_exists($p, 'toArray')) {
90
                        $newParam[] = $p->toArray();
91
                        continue;
92
                    }
93
94
                    $newParam[] = $p;
95
                }
96
97
                return $newParam;
98
            }
99
100
            return $attr;
101
        }, $this->getAttributes());
102
    }
103
104
    public function toJson()
105
    {
106
        return json_encode($this->toArray());
107
    }
108
109
    public function toString()
110
    {
111
        return print_r($this, true);
112
    }
113
114
    public function __toString()
115
    {
116
        return $this->toString();
117
    }
118
119
    public function __get($attr)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
120
    {
121
        return $this->get($attr);
122
    }
123
124
    public function __set($attr, $value)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
125
    {
126
        return $this->set($attr, $value);
127
    }
128
129
    public function __isset($attr)
130
    {
131
        return isset($this->$attr);
132
    }
133
134
    public function __unset($attr)
135
    {
136
        unset($this->$attr);
137
    }
138
}
139