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

ManipulatesData::only()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 12
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 20
ccs 13
cts 13
cp 1
crap 7
rs 8.8333
1
<?php
2
3
namespace Cerbero\Dto\Traits;
4
5
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...
6
use const Cerbero\Dto\NONE;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\NONE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
7
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...
8
9
/**
10
 * Trait to manipulate a DTO data.
11
 *
12
 */
13
trait ManipulatesData
14
{
15
    /**
16
     * Merge the given data in the DTO
17
     *
18
     * @param iterable $data
19
     * @param int $flags
20
     * @return self
21
     */
22 9
    public function merge(iterable $data, int $flags = NONE): self
23
    {
24 9
        $replacements = static::getArrayConverter()->convert($data);
25 9
        $mergedData = array_replace_recursive($this->toArray(), $replacements);
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

25
        $mergedData = array_replace_recursive($this->/** @scrutinizer ignore-call */ toArray(), $replacements);
Loading history...
26 9
        $mergedFlags = $this->mergeFlags($this->getFlags(), $flags);
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

26
        $mergedFlags = $this->mergeFlags($this->/** @scrutinizer ignore-call */ getFlags(), $flags);
Loading history...
Bug introduced by
The method mergeFlags() does not exist on Cerbero\Dto\Traits\ManipulatesData. Did you maybe mean merge()? ( Ignorable by Annotation )

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

26
        /** @scrutinizer ignore-call */ 
27
        $mergedFlags = $this->mergeFlags($this->getFlags(), $flags);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
27
28 9
        if (!($this->getFlags() & MUTABLE)) {
29 6
            return new static($mergedData, $mergedFlags);
0 ignored issues
show
Unused Code introduced by
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

29
            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...
30
        }
31
32 3
        $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...
33 3
        $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...
Bug introduced by
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

33
        /** @scrutinizer ignore-call */ 
34
        $this->propertiesMap = $this->mapData($mergedData);
Loading history...
34
35 3
        return $this;
36
    }
37
38
    /**
39
     * Retrieve the DTO including only the given properties
40
     *
41
     * @param array $properties
42
     * @param int $flags
43
     * @return self
44
     */
45 12
    public function only(array $properties, int $flags = NONE): self
46
    {
47 12
        $data = [];
48 12
        $isMutable = $this->getFlags() & MUTABLE;
49 12
        $mergedFlags = $this->mergeFlags($this->getFlagsWithoutDefaults(), $flags | PARTIAL);
0 ignored issues
show
Bug introduced by
It seems like getFlagsWithoutDefaults() 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

49
        $mergedFlags = $this->mergeFlags($this->/** @scrutinizer ignore-call */ getFlagsWithoutDefaults(), $flags | PARTIAL);
Loading history...
50
51 12
        foreach ($this->getPropertiesMap() as $name => $property) {
0 ignored issues
show
Bug introduced by
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

51
        foreach ($this->/** @scrutinizer ignore-call */ getPropertiesMap() as $name => $property) {
Loading history...
52 12
            if (in_array($name, $properties) && !$isMutable) {
53 6
                $data[$name] = $property->value();
54 12
            } elseif (!in_array($name, $properties) && $isMutable) {
55 10
                unset($this->propertiesMap[$name]);
56
            }
57
        }
58
59 12
        if ($isMutable) {
60 6
            $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...
61 6
            return $this;
62
        }
63
64 6
        return new static($data, $mergedFlags);
0 ignored issues
show
Unused Code introduced by
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

64
        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...
65
    }
66
67
    /**
68
     * Retrieve the DTO excluding the given properties
69
     *
70
     * @param array $properties
71
     * @param int $flags
72
     * @return self
73
     */
74 6
    public function except(array $properties, int $flags = NONE): self
75
    {
76 6
        $propertiesToKeep = array_diff($this->getPropertyNames(), $properties);
0 ignored issues
show
Bug introduced by
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

76
        $propertiesToKeep = array_diff($this->/** @scrutinizer ignore-call */ getPropertyNames(), $properties);
Loading history...
77
78 6
        return $this->only($propertiesToKeep, $flags);
79
    }
80
}
81