Passed
Push — master ( 714233...3f5928 )
by Dominik
01:39
created

DateTimeFieldNormalizer::__construct()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 21
cts 21
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 3
nop 2
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer;
6
7
use Chubbyphp\Serialization\Accessor\AccessorInterface;
8
9
final class DateTimeFieldNormalizer implements FieldNormalizerInterface
10
{
11
    /**
12
     * @var FieldNormalizerInterface
13
     */
14
    private $fieldNormalizer;
15
16
    /**
17
     * @var AccessorInterface
18
     */
19
    private $accessor;
20
21
    /**
22
     * @var string
23
     */
24
    private $format;
25
26
    /**
27
     * @param AccessorInterface|FieldNormalizerInterface $fieldNormalizer
0 ignored issues
show
Bug introduced by
There is no parameter named $fieldNormalizer. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
28
     * @param string                   $format
29
     */
30 6
    public function __construct($accessor, string $format = 'c')
31
    {
32
33 6
        $this->format = $format;
34
35 6
        if ($accessor instanceof AccessorInterface) {
36 4
            $this->accessor = $accessor;
37
38 4
            return;
39
        }
40
41 2
        if ($accessor instanceof FieldNormalizerInterface) {
42 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...
43 1
                sprintf(
44 1
                    'Use "%s" instead of "%s" as __construct argument',
45 1
                    AccessorInterface::class,
46 1
                    FieldNormalizerInterface::class
47
                ),
48 1
                E_USER_DEPRECATED
49
            );
50
51 1
            $this->fieldNormalizer = $accessor;
52
53 1
            return;
54
        }
55
56 1
        throw new \TypeError(
57 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...
58 1
                '%s::__construct() expects parameter 1 to be %s|%s, %s given',
59 1
                self::class,
60 1
                AccessorInterface::class,
61 1
                FieldNormalizerInterface::class,
62 1
                is_object($accessor) ? get_class($accessor) : gettype($accessor)
63
            )
64
        );
65
    }
66
67
    /**
68
     * @param string                     $path
69
     * @param object                     $object
70
     * @param NormalizerContextInterface $context
71
     * @param NormalizerInterface|null   $normalizer
72
     *
73
     * @return mixed
74
     */
75 5
    public function normalizeField(
76
        string $path,
77
        $object,
78
        NormalizerContextInterface $context,
79
        NormalizerInterface $normalizer = null
80
    ) {
81 5
        $value = $this->getValue($path, $object, $context, $normalizer);
82
83 5
        if (is_string($value)) {
84
            try {
85 2
                $value = new \DateTime($value);
86 1
            } catch (\Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
87
            }
88
        }
89
90 5
        if (!$value instanceof \DateTimeInterface) {
91 2
            return $value;
92
        }
93
94 3
        return $value->format($this->format);
95
    }
96
97
    /**
98
     * @param string $path
99
     * @param $object
100
     * @param NormalizerContextInterface $context
101
     * @param NormalizerInterface|null $normalizer
102
     * @return mixed
103
     */
104 5
    private function getValue(
105
        string $path,
106
        $object,
107
        NormalizerContextInterface $context,
108
        NormalizerInterface $normalizer = null
109
    ) {
110 5
        if (null !== $this->accessor) {
111 4
            return $this->accessor->getValue($object);
112
        }
113
114 1
        return $this->fieldNormalizer->normalizeField($path, $object, $context, $normalizer);
115
    }
116
}
117