DtoPropertyValueProcessor   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 86
ccs 26
cts 26
cp 1
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A processCollection() 0 9 2
A processValue() 0 13 5
A castValueIntoDto() 0 5 2
A process() 0 11 3
A __construct() 0 3 1
1
<?php
2
3
namespace Cerbero\Dto;
4
5
/**
6
 * The processor for DTO property values.
7
 *
8
 */
9
class DtoPropertyValueProcessor
10
{
11
    /**
12
     * The property to process the value of.
13
     *
14
     * @var DtoProperty
15
     */
16
    protected $property;
17
18
    /**
19
     * Instantiate the class.
20
     *
21
     * @param DtoProperty $property
22
     */
23 91
    public function __construct(DtoProperty $property)
24
    {
25 91
        $this->property = $property;
26 91
    }
27
28
    /**
29
     * Retrieve the processed property value
30
     *
31
     * @return mixed
32
     */
33 85
    public function process()
34
    {
35 85
        if (is_null($rawValue = $this->property->getRawValue())) {
36 11
            return null;
37
        }
38
39 76
        if ($this->property->getTypes()->expectCollection) {
40 2
            return $this->processCollection($rawValue);
41
        }
42
43 74
        return $this->processValue($rawValue);
44
    }
45
46
    /**
47
     * Retrieve the processed value as a collection
48
     *
49
     * @param iterable $collection
50
     * @return array
51
     */
52 2
    protected function processCollection(iterable $collection): array
53
    {
54 2
        $processed = [];
55
56 2
        foreach ($collection as $value) {
57 2
            $processed[] = $this->processValue($value);
58
        }
59
60 2
        return $processed;
61
    }
62
63
    /**
64
     * Retrieve the processed value
65
     *
66
     * @param mixed $value
67
     * @return mixed
68
     */
69 76
    protected function processValue($value)
70
    {
71 76
        $types = $this->property->getTypes();
72
73 76
        if ($converter = $types->expectedConverter) {
74 2
            return $converter->toDto($value);
75 74
        } elseif ($types->expectedDto) {
76 19
            return $this->castValueIntoDto($value);
77 70
        } elseif (($this->property->getFlags() & CAST_PRIMITIVES) && $type = $types->expectedPrimitive) {
78 4
            settype($value, $type);
79
        }
80
81 70
        return $value;
82
    }
83
84
    /**
85
     * Retrieve the given value casted into a DTO
86
     *
87
     * @param mixed $value
88
     * @return Dto
89
     */
90 19
    protected function castValueIntoDto($value): Dto
91
    {
92 19
        $dto = $this->property->getTypes()->expectedDto;
93
94 19
        return is_a($value, $dto) ? $value : $dto::make($value, $this->property->getFlags());
0 ignored issues
show
Bug introduced by
The method make() does not exist on null. ( Ignorable by Annotation )

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

94
        return is_a($value, $dto) ? $value : $dto::/** @scrutinizer ignore-call */ make($value, $this->property->getFlags());

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...
Bug introduced by
It seems like $dto can also be of type null; however, parameter $class of is_a() does only seem to accept string, 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

94
        return is_a($value, /** @scrutinizer ignore-type */ $dto) ? $value : $dto::make($value, $this->property->getFlags());
Loading history...
95
    }
96
}
97