Passed
Push — master ( d32d3d...0995a0 )
by Andrea Marco
02:44 queued 12s
created

HasValues::set()   A

Complexity

Conditions 4
Paths 10

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 4
rs 9.9666
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
     * Determine whether the given property has a value (return FALSE if the value is NULL)
21
     *
22
     * @param string $property
23
     * @return bool
24
     */
25 30
    public function has(string $property): bool
26
    {
27
        try {
28 30
            return $this->get($property) !== null;
29 9
        } catch (UnknownDtoPropertyException $e) {
30 9
            return false;
31
        }
32
    }
33
34
    /**
35
     * Retrieve the given property value
36
     *
37
     * @param string $property
38
     * @return mixed
39
     * @throws UnknownDtoPropertyException
40
     */
41 90
    public function get(string $property)
42
    {
43 90
        $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

43
        $value = $this->/** @scrutinizer ignore-call */ getProperty($property)->value();
Loading history...
44
45 69
        return Listener::instance()->getting(static::class, $property, $value);
46
    }
47
48
    /**
49
     * Set the given property to the provided value
50
     *
51
     * @param string $property
52
     * @param mixed $value
53
     * @return self
54
     * @throws UnknownDtoPropertyException
55
     */
56 27
    public function set(string $property, $value): self
57
    {
58 27
        $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

58
        /** @scrutinizer ignore-call */ 
59
        $flags = $this->getFlags();
Loading history...
59 27
        $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

59
        $dto = ($flags & MUTABLE) ? $this : $this->/** @scrutinizer ignore-call */ clone();
Loading history...
60
61
        try {
62 27
            $value = Listener::instance()->setting(static::class, $property, $value);
63 27
            $dto->getProperty($property)->setValue($value, $flags);
64 6
        } catch (UnknownDtoPropertyException $e) {
65 6
            if (!($flags & IGNORE_UNKNOWN_PROPERTIES)) {
66 3
                throw $e;
67
            }
68
        }
69
70 24
        return $dto;
71
    }
72
73
    /**
74
     * Unset the given property
75
     *
76
     * @param string $property
77
     * @return self
78
     * @throws UnsetDtoPropertyException
79
     * @throws UnknownDtoPropertyException
80
     */
81 24
    public function unset(string $property): self
82
    {
83 24
        $flags = $this->getFlags();
84
85 24
        if (!($flags & PARTIAL)) {
86 6
            throw new UnsetDtoPropertyException(static::class, $property);
87
        }
88
89 18
        if (strpos($property, '.') !== false) {
90 9
            [$property, $nestedProperty] = explode('.', $property, 2);
91 9
            $unsetDto = $this->get($property)->unset($nestedProperty);
92 6
            return $this->set($property, $unsetDto);
93
        }
94
95 15
        if ($flags & MUTABLE) {
96 9
            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...
97 9
            return $this;
98
        }
99
100 6
        $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

100
        /** @scrutinizer ignore-call */ 
101
        $data = $this->toArray();
Loading history...
101 6
        unset($data[$property]);
102
103 6
        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

103
        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...
104
    }
105
106
    /**
107
     * Determine whether a given property has a value
108
     *
109
     * @param string $property
110
     * @return bool
111
     */
112 9
    public function __isset(string $property): bool
113
    {
114 9
        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

114
        return $this->/** @scrutinizer ignore-call */ offsetExists($property);
Loading history...
115
    }
116
117
    /**
118
     * Retrieve the given property value
119
     *
120
     * @param string $property
121
     * @return mixed
122
     * @throws UnknownDtoPropertyException
123
     */
124 27
    public function &__get(string $property)
125
    {
126 27
        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

126
        return $this->/** @scrutinizer ignore-call */ offsetGet($property);
Loading history...
127
    }
128
129
    /**
130
     * Set the given property to the provided value
131
     *
132
     * @param string $property
133
     * @param mixed $value
134
     * @return void
135
     * @throws \Cerbero\Dto\Exceptions\ImmutableDtoException
136
     * @throws UnknownDtoPropertyException
137
     */
138 6
    public function __set(string $property, $value): void
139
    {
140 6
        $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

140
        $this->/** @scrutinizer ignore-call */ 
141
               offsetSet($property, $value);
Loading history...
141 3
    }
142
}
143