Test Failed
Push — master ( e3b5b4...43c430 )
by Kirill
04:55
created

ValueObject   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setAttributes() 0 6 2
A set() 0 4 1
A toArray() 0 6 2
A getAttributes() 0 4 1
A __get() 0 4 1
A __set() 0 4 1
A get() 0 4 1
A jsonSerialize() 0 4 1
1
<?php
2
/**
3
 * This file is part of Railt 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 Railt\SDL\Frontend\ValueObject;
11
12
/**
13
 * Class ValueObject
14
 */
15
class ValueObject extends BaseStruct
16
{
17
    /**
18
     * ValueObject constructor.
19
     * @param iterable $attributes
20
     */
21
    public function __construct(iterable $attributes = [])
22
    {
23
        $this->setAttributes($attributes);
0 ignored issues
show
Bug introduced by
It seems like $attributes defined by parameter $attributes on line 21 can also be of type array; however, Railt\SDL\Frontend\Value...Object::setAttributes() does only seem to accept object<Railt\SDL\Frontend\ValueObject\iterable>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
24
    }
25
26
    /**
27
     * @param iterable $attributes
28
     */
29
    public function setAttributes(iterable $attributes): void
30
    {
31
        foreach ($attributes as $attribute => $value) {
32
            $this->set($attribute, $value);
33
        }
34
    }
35
36
    /**
37
     * @param string|int $attribute
38
     * @param mixed $value
39
     * @return void
40
     */
41
    public function set($attribute, $value): void
42
    {
43
        $this->value[$attribute] = new ScalarValue($value);
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function toArray(): array
50
    {
51
        return \array_map(function (BaseStruct $value) {
52
            return $value instanceof ValueObject ? $value->toArray() : $value->getValue();
53
        }, $this->value);
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public function getAttributes(): array
60
    {
61
        return $this->value;
62
    }
63
64
    /**
65
     * @param string|int $attribute
66
     * @return mixed|null
67
     */
68
    public function __get($attribute)
69
    {
70
        return $this->get($attribute);
71
    }
72
73
    /**
74
     * @param string|int $attribute
75
     * @param mixed $value
76
     */
77
    public function __set($attribute, $value)
78
    {
79
        $this->set($attribute, $value);
80
    }
81
82
    /**
83
     * @param string|int $attribute
84
     * @return mixed|null
85
     */
86
    public function get($attribute)
87
    {
88
        return $this->value[$attribute] ?? new ScalarValue(null);
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function jsonSerialize(): array
95
    {
96
        return $this->toArray();
97
    }
98
}
99