Passed
Push — master ( 9d7ef7...af08db )
by Andrea Marco
01:42 queued 11s
created

DtoPropertyValueProcessor::__construct()   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 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 282
    public function __construct(DtoProperty $property)
24
    {
25 282
        $this->property = $property;
26 282
    }
27
28
    /**
29
     * Retrieve the processed property value
30
     *
31
     * @return mixed
32
     */
33 255
    public function process()
34
    {
35 255
        if (is_null($rawValue = $this->property->getRawValue())) {
36 30
            return null;
37
        }
38
39 225
        if ($this->property->getTypes()->expectCollection) {
40 6
            return $this->processCollection($rawValue);
41
        }
42
43 219
        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 6
    protected function processCollection(iterable $collection): array
53
    {
54 6
        $processed = [];
55
56 6
        foreach ($collection as $value) {
57 6
            $processed[] = $this->processValue($value);
58
        }
59
60 6
        return $processed;
61
    }
62
63
    /**
64
     * Retrieve the processed value
65
     *
66
     * @param mixed $value
67
     * @return mixed
68
     */
69 225
    protected function processValue($value)
70
    {
71 225
        $types = $this->property->getTypes();
72
73 225
        if ($converter = $types->expectedConverter) {
74 6
            return $converter->toDto($value);
75 219
        } elseif ($types->expectedDto) {
76 57
            return $this->castValueIntoDto($value);
77 207
        } elseif (($this->property->getFlags() & CAST_PRIMITIVES) && $type = $types->expectedPrimitive) {
78 3
            settype($value, $type);
79
        }
80
81 207
        return $value;
82
    }
83
84
    /**
85
     * Retrieve the given value casted into a DTO
86
     *
87
     * @param mixed $value
88
     * @return Dto
89
     */
90 57
    protected function castValueIntoDto($value)
91
    {
92 57
        $dto = $this->property->getTypes()->expectedDto;
93
94 57
        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...
95
    }
96
}
97