1 | <?php |
||||||
2 | |||||||
3 | namespace Cerbero\Dto\Traits; |
||||||
4 | |||||||
5 | use Cerbero\Dto\Manipulators\ArrayConverter; |
||||||
6 | |||||||
7 | use const Cerbero\Dto\CAMEL_CASE_ARRAY; |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
8 | use const Cerbero\Dto\MUTABLE; |
||||||
0 ignored issues
–
show
|
|||||||
9 | use const Cerbero\Dto\NONE; |
||||||
0 ignored issues
–
show
|
|||||||
10 | use const Cerbero\Dto\PARTIAL; |
||||||
0 ignored issues
–
show
|
|||||||
11 | |||||||
12 | /** |
||||||
13 | * Trait to manipulate a DTO data. |
||||||
14 | * |
||||||
15 | */ |
||||||
16 | trait ManipulatesData |
||||||
17 | { |
||||||
18 | /** |
||||||
19 | * Merge the given data in the DTO |
||||||
20 | * |
||||||
21 | * @param iterable $data |
||||||
22 | * @param int $flags |
||||||
23 | * @return self |
||||||
24 | */ |
||||||
25 | 3 | public function merge(iterable $data, int $flags = NONE): self |
|||||
26 | { |
||||||
27 | 3 | $mergedFlags = $this->getFlags() | $flags; |
|||||
0 ignored issues
–
show
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
![]() |
|||||||
28 | 3 | $snakeCase = !($mergedFlags & CAMEL_CASE_ARRAY); |
|||||
29 | 3 | $replacements = ArrayConverter::instance()->convert($data, $snakeCase); |
|||||
30 | 3 | $mergedData = array_replace_recursive($this->toArray(), $replacements); |
|||||
0 ignored issues
–
show
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
![]() It seems like
$replacements can also be of type iterable ; however, parameter $replacements of array_replace_recursive() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
31 | |||||||
32 | 3 | if (!($this->getFlags() & MUTABLE)) { |
|||||
33 | 2 | return new static($mergedData, $mergedFlags); |
|||||
0 ignored issues
–
show
The call to
Cerbero\Dto\Traits\ManipulatesData::__construct() has too many arguments starting with $mergedData .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||||
34 | } |
||||||
35 | |||||||
36 | 1 | $this->flags = $mergedFlags; |
|||||
0 ignored issues
–
show
|
|||||||
37 | 1 | $this->propertiesMap = $this->mapData($mergedData); |
|||||
0 ignored issues
–
show
It seems like
mapData() 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
![]() |
|||||||
38 | |||||||
39 | 1 | return $this; |
|||||
40 | } |
||||||
41 | |||||||
42 | /** |
||||||
43 | * Retrieve the DTO including only the given properties |
||||||
44 | * |
||||||
45 | * @param array $properties |
||||||
46 | * @param int $flags |
||||||
47 | * @return self |
||||||
48 | */ |
||||||
49 | 4 | public function only(array $properties, int $flags = NONE): self |
|||||
50 | { |
||||||
51 | 4 | $data = []; |
|||||
52 | 4 | $isMutable = $this->getFlags() & MUTABLE; |
|||||
53 | 4 | $mergedFlags = $this->getFlags() | $flags | PARTIAL; |
|||||
54 | |||||||
55 | 4 | foreach ($this->getPropertiesMap() as $name => $property) { |
|||||
0 ignored issues
–
show
It seems like
getPropertiesMap() 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
![]() |
|||||||
56 | 4 | if (in_array($name, $properties) && !$isMutable) { |
|||||
57 | 2 | $data[$name] = $property->value(); |
|||||
58 | 4 | } elseif (!in_array($name, $properties) && $isMutable) { |
|||||
59 | 2 | unset($this->propertiesMap[$name]); |
|||||
60 | } |
||||||
61 | } |
||||||
62 | |||||||
63 | 4 | if ($isMutable) { |
|||||
64 | 2 | $this->flags = $mergedFlags; |
|||||
0 ignored issues
–
show
|
|||||||
65 | 2 | return $this; |
|||||
66 | } |
||||||
67 | |||||||
68 | 2 | return new static($data, $mergedFlags); |
|||||
0 ignored issues
–
show
The call to
Cerbero\Dto\Traits\ManipulatesData::__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
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. ![]() |
|||||||
69 | } |
||||||
70 | |||||||
71 | /** |
||||||
72 | * Retrieve the DTO excluding the given properties |
||||||
73 | * |
||||||
74 | * @param array $properties |
||||||
75 | * @param int $flags |
||||||
76 | * @return self |
||||||
77 | */ |
||||||
78 | 2 | public function except(array $properties, int $flags = NONE): self |
|||||
79 | { |
||||||
80 | 2 | $propertiesToKeep = array_diff($this->getPropertyNames(), $properties); |
|||||
0 ignored issues
–
show
It seems like
getPropertyNames() 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
![]() |
|||||||
81 | |||||||
82 | 2 | return $this->only($propertiesToKeep, $flags); |
|||||
83 | } |
||||||
84 | |||||||
85 | /** |
||||||
86 | * Make the DTO mutable while calling the given callback |
||||||
87 | * |
||||||
88 | * @param callable $callback |
||||||
89 | * @return self |
||||||
90 | */ |
||||||
91 | 2 | public function mutate(callable $callback): self |
|||||
92 | { |
||||||
93 | 2 | $wasMutable = $this->getFlags() & MUTABLE; |
|||||
94 | 2 | $this->flags |= MUTABLE; |
|||||
95 | |||||||
96 | 2 | $callback($this); |
|||||
97 | |||||||
98 | 2 | if (!$wasMutable) { |
|||||
99 | 1 | $this->flags ^= MUTABLE; |
|||||
100 | } |
||||||
101 | |||||||
102 | 2 | return $this; |
|||||
103 | } |
||||||
104 | } |
||||||
105 |