Test Failed
Push — master ( 7b15c4...7a5412 )
by Dominik
01:48
created

DateFieldNormalizer::normalizeField()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 8
cts 8
cp 1
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 13
nc 6
nop 4
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer;
6
7
final class DateFieldNormalizer implements FieldNormalizerInterface
8
{
9
    /**
10
     * @var FieldNormalizerInterface
11
     */
12
    private $fieldNormalizer;
13
14
    /**
15
     * @var string
16
     */
17
    private $format;
18
19
    /**
20
     * @param FieldNormalizerInterface $fieldNormalizer
21
     * @param string                   $format
22
     */
23 4
    public function __construct(FieldNormalizerInterface $fieldNormalizer, string $format = 'c')
24
    {
25 4
        $this->fieldNormalizer = $fieldNormalizer;
26 4
        $this->format = $format;
27 4
    }
28
29
    /**
30
     * @param string                     $path
31
     * @param object                     $object
32
     * @param mixed                      $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. 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...
33
     * @param NormalizerContextInterface $context
34
     * @param NormalizerInterface|null   $normalizer
35
     *
36
     * @return mixed
37
     */
38 4
    public function normalizeField(
39
        string $path,
40
        $object,
41
        NormalizerContextInterface $context,
42
        NormalizerInterface $normalizer = null
43
    ) {
44 4
        $value = $this->fieldNormalizer->normalizeField($path, $object, $context, $normalizer);
45
46 4
        if (is_string($value)) {
47
            try {
48 2
                $value = new \DateTime($value);
49 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...
50
            }
51
        }
52
53 4
        if (!$value instanceof \DateTimeInterface) {
54 2
            return $value;
55
        }
56
57 2
        return $value->format($this->format);
58
    }
59
}
60