Passed
Pull Request — master (#16)
by Dominik
05:26
created

DateTimeFieldDenormalizer::__construct()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 20
cts 20
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 20
nc 3
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Denormalizer;
6
7
use Chubbyphp\Deserialization\Accessor\AccessorInterface;
8
use Chubbyphp\Deserialization\DeserializerRuntimeException;
9
10
final class DateTimeFieldDenormalizer implements FieldDenormalizerInterface
11
{
12
    /**
13
     * @deprecated
14
     *
15
     * @var FieldDenormalizerInterface
16
     */
17
    private $fieldDenormalizer;
18
19
    /**
20
     * @var AccessorInterface
21
     */
22
    private $accessor;
23
24
    /**
25
     * @param AccessorInterface|FieldDenormalizerInterface $accessor
26
     */
27 14
    public function __construct($accessor)
28
    {
29 14
        if ($accessor instanceof AccessorInterface) {
30 12
            $this->accessor = $accessor;
31
32 12
            return;
33
        }
34
35 2
        if ($accessor instanceof FieldDenormalizerInterface) {
36 1
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
37 1
                sprintf(
38 1
                    'Use "%s" instead of "%s" as __construct argument',
39 1
                    AccessorInterface::class,
40 1
                    FieldDenormalizerInterface::class
41
                ),
42 1
                E_USER_DEPRECATED
43
            );
44
45 1
            $this->fieldDenormalizer = $accessor;
0 ignored issues
show
Deprecated Code introduced by
The property Chubbyphp\Deserializatio...zer::$fieldDenormalizer has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
46
47 1
            return;
48
        }
49
50 1
        throw new \TypeError(
51 1
            sprintf(
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with sprintf('%s::__construct...) : gettype($accessor)).

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
52 1
                '%s::__construct() expects parameter 1 to be %s|%s, %s given',
53 1
                self::class,
54 1
                AccessorInterface::class,
55 1
                FieldDenormalizerInterface::class,
56 1
                is_object($accessor) ? get_class($accessor) : gettype($accessor)
57
            )
58
        );
59
    }
60
61
    /**
62
     * @param string                       $path
63
     * @param object                       $object
64
     * @param mixed                        $value
65
     * @param DenormalizerContextInterface $context
66
     * @param DenormalizerInterface|null   $denormalizer
67
     *
68
     * @throws DeserializerRuntimeException
69
     */
70 13
    public function denormalizeField(
71
        string $path,
72
        $object,
73
        $value,
74
        DenormalizerContextInterface $context,
75
        DenormalizerInterface $denormalizer = null
76
    ) {
77 13
        if (!is_string($value) || '' === $trimmedValue = trim($value)) {
78 6
            $this->setValue($path, $object, $value, $context, $denormalizer);
79
80 6
            return;
81
        }
82
83
        try {
84 7
            $dateTime = new \DateTime($trimmedValue);
85 4
            $errors = \DateTime::getLastErrors();
86
87 4
            if (0 === $errors['warning_count'] && 0 === $errors['error_count']) {
88 4
                $value = $dateTime;
89
            }
90 3
        } catch (\Exception $exception) {
91 3
            error_clear_last();
92
        }
93
94 7
        $this->setValue($path, $object, $value, $context, $denormalizer);
95 7
    }
96
97
    /**
98
     * @param string                       $path
99
     * @param object                       $object
100
     * @param mixed                        $value
101
     * @param DenormalizerContextInterface $context
102
     * @param DenormalizerInterface|null   $denormalizer
103
     */
104 13
    private function setValue(
105
        string $path,
106
        $object,
107
        $value,
108
        DenormalizerContextInterface $context,
109
        DenormalizerInterface $denormalizer = null
110
    ) {
111 13
        if (null !== $this->accessor) {
112 12
            $this->accessor->setValue($object, $value);
113
114 12
            return;
115
        }
116
117 1
        $this->fieldDenormalizer->denormalizeField($path, $object, $value, $context, $denormalizer);
0 ignored issues
show
Deprecated Code introduced by
The property Chubbyphp\Deserializatio...zer::$fieldDenormalizer has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
118 1
    }
119
}
120