HasValues   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 37
c 2
b 0
f 0
dl 0
loc 151
ccs 40
cts 40
cp 1
rs 10
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultValues() 0 3 1
A getListener() 0 3 1
A get() 0 5 1
A __isset() 0 3 1
A __set() 0 3 1
A __get() 0 3 1
A unset() 0 23 4
A has() 0 6 2
A set() 0 15 4
1
<?php
2
3
namespace Cerbero\Dto\Traits;
4
5
use Cerbero\Dto\Exceptions\UnknownDtoPropertyException;
6
use Cerbero\Dto\Exceptions\UnsetDtoPropertyException;
7
use Cerbero\Dto\Manipulators\Listener;
8
9
use const Cerbero\Dto\IGNORE_UNKNOWN_PROPERTIES;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\IGNORE_UNKNOWN_PROPERTIES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
10
use const Cerbero\Dto\MUTABLE;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\MUTABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
11
use const Cerbero\Dto\PARTIAL;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\PARTIAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
12
13
/**
14
 * Trait to interact with values.
15
 *
16
 */
17
trait HasValues
18
{
19
    /**
20
     * The default values.
21
     *
22
     * @var array
23
     */
24
    protected static $defaultValues = [];
25
26
    /**
27
     * Retrieve the default values
28
     *
29
     * @return array
30
     */
31 81
    public static function getDefaultValues(): array
32
    {
33 81
        return static::$defaultValues;
34
    }
35
36
    /**
37
     * Determine whether the given property has a value (return FALSE if the value is NULL)
38
     *
39
     * @param string $property
40
     * @return bool
41
     */
42 10
    public function has(string $property): bool
43
    {
44
        try {
45 10
            return $this->get($property) !== null;
46 3
        } catch (UnknownDtoPropertyException $e) {
47 3
            return false;
48
        }
49
    }
50
51
    /**
52
     * Retrieve the given property value
53
     *
54
     * @param string $property
55
     * @return mixed
56
     * @throws UnknownDtoPropertyException
57
     */
58 33
    public function get(string $property)
59
    {
60 33
        $value = $this->getProperty($property)->value();
0 ignored issues
show
Bug introduced by
It seems like getProperty() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        $value = $this->/** @scrutinizer ignore-call */ getProperty($property)->value();
Loading history...
61
62 26
        return $this->getListener()->getting(static::class, $property, $value);
63
    }
64
65
    /**
66
     * Retrieve the listener instance
67
     *
68
     * @return Listener
69
     */
70 31
    protected function getListener(): Listener
71
    {
72 31
        return Listener::instance();
73
    }
74
75
    /**
76
     * Set the given property to the provided value
77
     *
78
     * @param string $property
79
     * @param mixed $value
80
     * @return self
81
     * @throws UnknownDtoPropertyException
82
     */
83 14
    public function set(string $property, $value): self
84
    {
85 14
        $flags = $this->getFlags();
0 ignored issues
show
Bug introduced by
It seems like getFlags() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        /** @scrutinizer ignore-call */ 
86
        $flags = $this->getFlags();
Loading history...
86 14
        $dto = ($flags & MUTABLE) ? $this : $this->clone();
0 ignored issues
show
Bug introduced by
It seems like clone() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        $dto = ($flags & MUTABLE) ? $this : $this->/** @scrutinizer ignore-call */ clone();
Loading history...
87
88
        try {
89 14
            $value = $this->getListener()->setting(static::class, $property, $value);
90 14
            $dto->setPropertyValueOrMap($property, $value);
0 ignored issues
show
Bug introduced by
It seems like setPropertyValueOrMap() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
            $dto->/** @scrutinizer ignore-call */ 
91
                  setPropertyValueOrMap($property, $value);
Loading history...
91 1
        } catch (UnknownDtoPropertyException $e) {
92 1
            if (!($flags & IGNORE_UNKNOWN_PROPERTIES)) {
93 1
                throw $e;
94
            }
95
        }
96
97 13
        return $dto;
98
    }
99
100
    /**
101
     * Unset the given property
102
     *
103
     * @param string $property
104
     * @return self
105
     * @throws UnsetDtoPropertyException
106
     * @throws UnknownDtoPropertyException
107
     */
108 8
    public function unset(string $property): self
109
    {
110 8
        $flags = $this->getFlags();
111
112 8
        if (!($flags & PARTIAL)) {
113 2
            throw new UnsetDtoPropertyException(static::class, $property);
114
        }
115
116 6
        if (strpos($property, '.') !== false) {
117 3
            [$property, $nestedProperty] = explode('.', $property, 2);
118 3
            $unsetDto = $this->get($property)->unset($nestedProperty);
119 2
            return $this->set($property, $unsetDto);
120
        }
121
122 5
        if ($flags & MUTABLE) {
123 3
            unset($this->propertiesMap[$property]);
0 ignored issues
show
Bug Best Practice introduced by
The property propertiesMap does not exist on Cerbero\Dto\Traits\HasValues. Since you implemented __get, consider adding a @property annotation.
Loading history...
124 3
            return $this;
125
        }
126
127 2
        $data = $this->toArray();
0 ignored issues
show
Bug introduced by
It seems like toArray() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
        /** @scrutinizer ignore-call */ 
128
        $data = $this->toArray();
Loading history...
128 2
        unset($data[$property]);
129
130 2
        return new static($data, $flags);
0 ignored issues
show
Unused Code introduced by
The call to Cerbero\Dto\Traits\HasValues::__construct() has too many arguments starting with $data. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
        return /** @scrutinizer ignore-call */ new static($data, $flags);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
131
    }
132
133
    /**
134
     * Determine whether a given property has a value
135
     *
136
     * @param string $property
137
     * @return bool
138
     */
139 3
    public function __isset(string $property): bool
140
    {
141 3
        return $this->offsetExists($property);
0 ignored issues
show
Bug introduced by
It seems like offsetExists() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
        return $this->/** @scrutinizer ignore-call */ offsetExists($property);
Loading history...
142
    }
143
144
    /**
145
     * Retrieve the given property value
146
     *
147
     * @param string $property
148
     * @return mixed
149
     * @throws UnknownDtoPropertyException
150
     */
151 9
    public function &__get(string $property)
152
    {
153 9
        return $this->offsetGet($property);
0 ignored issues
show
Bug introduced by
It seems like offsetGet() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
        return $this->/** @scrutinizer ignore-call */ offsetGet($property);
Loading history...
154
    }
155
156
    /**
157
     * Set the given property to the provided value
158
     *
159
     * @param string $property
160
     * @param mixed $value
161
     * @return void
162
     * @throws \Cerbero\Dto\Exceptions\ImmutableDtoException
163
     * @throws UnknownDtoPropertyException
164
     */
165 4
    public function __set(string $property, $value): void
166
    {
167 4
        $this->offsetSet($property, $value);
0 ignored issues
show
Bug introduced by
It seems like offsetSet() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
        $this->/** @scrutinizer ignore-call */ 
168
               offsetSet($property, $value);
Loading history...
168 3
    }
169
}
170