Passed
Push — master ( af08db...88890e )
by Andrea Marco
02:57 queued 10s
created

HasValues::getListener()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 99
    public function get(string $property)
42
    {
43 99
        $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 78
        return $this->getListener()->getting(static::class, $property, $value);
46
    }
47
48
    /**
49
     * Retrieve the listener instance
50
     *
51
     * @return Listener
52
     */
53 93
    protected function getListener(): Listener
54
    {
55 93
        return Listener::instance();
56
    }
57
58
    /**
59
     * Set the given property to the provided value
60
     *
61
     * @param string $property
62
     * @param mixed $value
63
     * @return self
64
     * @throws UnknownDtoPropertyException
65
     */
66 42
    public function set(string $property, $value): self
67
    {
68 42
        $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

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

69
        $dto = ($flags & MUTABLE) ? $this : $this->/** @scrutinizer ignore-call */ clone();
Loading history...
70
71
        try {
72 42
            $value = $this->getListener()->setting(static::class, $property, $value);
73 42
            $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

73
            $dto->/** @scrutinizer ignore-call */ 
74
                  setPropertyValueOrMap($property, $value);
Loading history...
74 3
        } catch (UnknownDtoPropertyException $e) {
75 3
            if (!($flags & IGNORE_UNKNOWN_PROPERTIES)) {
76 3
                throw $e;
77
            }
78
        }
79
80 39
        return $dto;
81
    }
82
83
    /**
84
     * Unset the given property
85
     *
86
     * @param string $property
87
     * @return self
88
     * @throws UnsetDtoPropertyException
89
     * @throws UnknownDtoPropertyException
90
     */
91 24
    public function unset(string $property): self
92
    {
93 24
        $flags = $this->getFlags();
94
95 24
        if (!($flags & PARTIAL)) {
96 6
            throw new UnsetDtoPropertyException(static::class, $property);
97
        }
98
99 18
        if (strpos($property, '.') !== false) {
100 9
            [$property, $nestedProperty] = explode('.', $property, 2);
101 9
            $unsetDto = $this->get($property)->unset($nestedProperty);
102 6
            return $this->set($property, $unsetDto);
103
        }
104
105 15
        if ($flags & MUTABLE) {
106 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...
107 9
            return $this;
108
        }
109
110 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

110
        /** @scrutinizer ignore-call */ 
111
        $data = $this->toArray();
Loading history...
111 6
        unset($data[$property]);
112
113 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

113
        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...
114
    }
115
116
    /**
117
     * Determine whether a given property has a value
118
     *
119
     * @param string $property
120
     * @return bool
121
     */
122 9
    public function __isset(string $property): bool
123
    {
124 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

124
        return $this->/** @scrutinizer ignore-call */ offsetExists($property);
Loading history...
125
    }
126
127
    /**
128
     * Retrieve the given property value
129
     *
130
     * @param string $property
131
     * @return mixed
132
     * @throws UnknownDtoPropertyException
133
     */
134 27
    public function &__get(string $property)
135
    {
136 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

136
        return $this->/** @scrutinizer ignore-call */ offsetGet($property);
Loading history...
137
    }
138
139
    /**
140
     * Set the given property to the provided value
141
     *
142
     * @param string $property
143
     * @param mixed $value
144
     * @return void
145
     * @throws \Cerbero\Dto\Exceptions\ImmutableDtoException
146
     * @throws UnknownDtoPropertyException
147
     */
148 12
    public function __set(string $property, $value): void
149
    {
150 12
        $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

150
        $this->/** @scrutinizer ignore-call */ 
151
               offsetSet($property, $value);
Loading history...
151 9
    }
152
}
153