Completed
Push — master ( 885dd6...caa292 )
by Jonathan André
03:41
created

AttributesTrait::getMethod()   A

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 1
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
                    return $this->$attr;
27
                },
28
                $attributes
29
            )
30
        );
31
    }
32
33
    public function getMethod($attr)
34
    {
35
        return sprintf('get%s', ucfirst($attr));
36
    }
37
38
    public function setMethod($attr)
39
    {
40
        return sprintf('set%s', ucfirst($attr));
41
    }
42
43 View Code Duplication
    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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45
        $attributes = $this->getAttributes();
46
47
        $getMethod = $this->getMethod($attr);
48
        if (method_exists($this, $getMethod)) {
49
            return $this->$getMethod();
50
        }
51
52
        trigger_error(
53
            sprintf(
54
                'The class "%s" don\'t have a method "%s"',
55
                get_class($this),
56
                $getMethod
57
            ),
58
            E_USER_WARNING
59
        );
60
61
        if (array_key_exists($attr, $attributes)) {
62
            return $attributes[$attr];
63
        }
64
65
        throw new InvalidArgumentException(
66
            sprintf(
67
                'The class "%s" don\'t have an attribute "%s"',
68
                get_class($this),
69
                $attr
70
            )
71
        );
72
    }
73
74 View Code Duplication
    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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $setMethod = $this->setMethod($attr);
77
78
        if (method_exists($this, $setMethod)) {
79
            return $this->$setMethod($value);
80
        }
81
82
        trigger_error(
83
            sprintf(
84
                'The class "%s" don\'t have a method "%s"',
85
                get_class($this),
86
                $setMethod
87
            ),
88
            E_USER_WARNING
89
        );
90
91
        $attributes = $this->getAttributes();
92
93
        if (in_array($attr, $attributes)) {
94
            $this->$attr = $value;
95
        }
96
97
        throw new InvalidArgumentException(
98
            sprintf(
99
                'The class "%s" don\'t have an attribute "%s"',
100
                get_class($this),
101
                $attr
102
            )
103
        );
104
    }
105
106
    public function toArray()
107
    {
108
        return array_map(function ($attr) {
109
            if (method_exists($attr, 'toArray')) {
110
                return $attr->toArray();
111
            }
112
113
            if ($attr instanceof ArrayAccess) {
114
                $newParam = [];
115
                foreach ($attr as $p) {
116
                    if (method_exists($p, 'toArray')) {
117
                        $newParam[] = $p->toArray();
118
                        continue;
119
                    }
120
121
                    $newParam[] = $p;
122
                }
123
                return $newParam;
124
            }
125
126
            return $attr;
127
        }, $this->getAttributes());
128
    }
129
130
    public function toJson()
131
    {
132
        return json_encode($this->toArray());
133
    }
134
135
    public function toString()
136
    {
137
        return print_r($this, true);
138
    }
139
140
    public function __toString()
141
    {
142
        return $this->toString();
143
    }
144
145
    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...
146
    {
147
        return $this->get($attr);
148
    }
149
150
    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...
151
    {
152
        return $this->set($attr, $value);
153
    }
154
155
    public function __isset($attr)
156
    {
157
        return isset($this->$attr);
158
    }
159
160
    public function __unset($attr)
161
    {
162
        unset($this->$attr);
163
    }
164
}
165