Issues (48)

src/Traits/ManipulatesData.php (15 issues)

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
The constant Cerbero\Dto\CAMEL_CASE_ARRAY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
8
use const Cerbero\Dto\MUTABLE;
0 ignored issues
show
The constant Cerbero\Dto\MUTABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
9
use const Cerbero\Dto\NONE;
0 ignored issues
show
The constant Cerbero\Dto\NONE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
10
use const Cerbero\Dto\PARTIAL;
0 ignored issues
show
The constant Cerbero\Dto\PARTIAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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 ignore-call  annotation

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

30
        $mergedData = array_replace_recursive($this->/** @scrutinizer ignore-call */ toArray(), $replacements);
Loading history...
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 ignore-type  annotation

30
        $mergedData = array_replace_recursive($this->toArray(), /** @scrutinizer ignore-type */ $replacements);
Loading history...
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 ignore-call  annotation

33
            return /** @scrutinizer ignore-call */ new static($mergedData, $mergedFlags);

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...
34
        }
35
36 1
        $this->flags = $mergedFlags;
0 ignored issues
show
Bug Best Practice introduced by
The property flags does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37 1
        $this->propertiesMap = $this->mapData($mergedData);
0 ignored issues
show
Bug Best Practice introduced by
The property propertiesMap does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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 ignore-call  annotation

37
        /** @scrutinizer ignore-call */ 
38
        $this->propertiesMap = $this->mapData($mergedData);
Loading history...
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 ignore-call  annotation

55
        foreach ($this->/** @scrutinizer ignore-call */ getPropertiesMap() as $name => $property) {
Loading history...
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
Bug Best Practice introduced by
The property flags does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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 ignore-call  annotation

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

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...
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 ignore-call  annotation

80
        $propertiesToKeep = array_diff($this->/** @scrutinizer ignore-call */ getPropertyNames(), $properties);
Loading history...
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