Completed
Push — master ( 03b4a1...198db5 )
by Kirill
18:19
created

Attribute::setToAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * This file is part of Properties package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Serafim\Properties\Attribute;
11
12
/**
13
 * Class Attribute
14
 */
15
class Attribute implements AttributeInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    private $name;
21
22
    /**
23
     * @var int
24
     */
25
    private $type;
26
27
    /**
28
     * @var Matchable
29
     */
30
    private $matcher;
31
32
    /**
33
     * @var string
34
     */
35
    private $camel;
36
37
    /**
38
     * Attribute constructor.
39
     * @param string $name
40
     * @param int $type
41
     */
42
    public function __construct(string $name, int $type = self::TYPE_UNDEFINED)
43
    {
44
        $this->name  = $name;
45
        $this->type  = $type;
46
        $this->camel = $this->toCamelCase($name);
47
    }
48
49
    /**
50
     * @param string $name
51
     * @return string
52
     */
53
    private function toCamelCase(string $name): string
54
    {
55
        $name = \ucwords($name, '_');
56
        $name = \str_replace('_', '', $name);
57
58
        return $name;
59
    }
60
61
    /**
62
     * @param mixed|object $context
63
     * @return mixed
64
     */
65
    public function getValueFrom($context)
66
    {
67
        $value = $this->getFromAttribute($context);
68
69
        switch (true) {
70
            case \method_exists($context, $getter = $this->getPropertyGetter()):
71
                return $this->getUsingGetter($context, $getter, $value);
0 ignored issues
show
Bug introduced by
The variable $getter seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
72
73
            case \method_exists($context, $getter = $this->getBooleanGetter()):
74
                return $this->getUsingGetter($context, $getter, $value);
0 ignored issues
show
Bug introduced by
The variable $getter seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
75
76
            default:
77
                return $value;
78
        }
79
    }
80
81
    /**
82
     * @param mixed $context
83
     * @return mixed
84
     */
85
    private function getFromAttribute($context)
86
    {
87
        return (function (string $name) {
88
            return \property_exists($this, $name) ? $this->$name : null;
89
        })->call($context, $this->name);
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    private function getPropertyGetter(): string
96
    {
97
        return 'get' . $this->camel;
98
    }
99
100
    /**
101
     * @param object $context
102
     * @param string $getter
103
     * @param mixed $value
104
     * @return mixed
105
     */
106
    private function getUsingGetter($context, string $getter, $value)
107
    {
108
        return (function ($getter) use ($value) {
109
            return $this->$getter($value);
110
        })->call($context, $getter);
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    private function getBooleanGetter(): string
117
    {
118
        return 'is' . $this->camel;
119
    }
120
121
    /**
122
     * @param mixed|object $context
123
     * @param mixed $value
124
     * @return void
125
     */
126
    public function setValueTo($context, $value): void
127
    {
128
        if (\method_exists($context, $setter = $this->getPropertySetter())) {
129
            $this->setUsingSetter($context, $setter, $value);
130
131
            return;
132
        }
133
134
        $this->setToAttribute($context, $value);
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    private function getPropertySetter(): string
141
    {
142
        return 'set' . $this->camel;
143
    }
144
145
    /**
146
     * @param object $context
147
     * @param string $setter
148
     * @param mixed $value
149
     * @return void
150
     */
151
    private function setUsingSetter($context, string $setter, $value): void
152
    {
153
        (function () use ($setter, $value) {
154
            $this->$setter($value);
155
        })->call($context);
156
    }
157
158
    /**
159
     * @param object $context
160
     * @param mixed $value
161
     */
162
    private function setToAttribute($context, $value): void
163
    {
164
        (function (string $name) use ($value) {
165
            return $this->$name = $value;
166
        })->call($context, $this->name);
167
    }
168
169
    /**
170
     * @param Matchable $hint
171
     * @return Matchable
172
     */
173
    public function addMatcher(Matchable $hint): Matchable
174
    {
175
        $this->matcher = $hint;
176
177
        return $this;
178
    }
179
180
    /**
181
     * @return bool
182
     */
183
    public function isReadable(): bool
184
    {
185
        return $this->type === static::TYPE_READABLE || $this->type === static::TYPE_PROPERTY;
186
    }
187
188
    /**
189
     * @return bool
190
     */
191
    public function isWritable(): bool
192
    {
193
        return $this->type === static::TYPE_WRITABLE || $this->type === static::TYPE_PROPERTY;
194
    }
195
196
    /**
197
     * @param mixed $value
198
     * @return bool
199
     */
200
    public function match($value): bool
201
    {
202
        if ($this->matcher === null) {
203
            return true;
204
        }
205
206
        return $this->matcher->match($value);
207
    }
208
209
    /**
210
     * @return string
211
     */
212
    public function getName(): string
213
    {
214
        return $this->name;
215
    }
216
}
217