CarbonNormalizer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 63
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsDenormalization() 0 3 2
A denormalize() 0 11 3
A __construct() 0 11 2
A hasCacheableSupportsMethod() 0 3 1
1
<?php
2
3
4
namespace Apie\CarbonPlugin\Normalizers;
5
6
use Carbon\Carbon;
7
use Carbon\CarbonImmutable;
8
use Carbon\CarbonInterface;
9
use DateTime;
10
use DateTimeImmutable;
11
use DateTimeInterface;
12
use DateTimeZone;
13
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
14
15
/**
16
 * Extension on DateTimeNormalizer to use Carbon over the regular DateTime class instances.
17
 */
18
class CarbonNormalizer extends DateTimeNormalizer
19
{
20
    private $allowedTypes = [
21
        Carbon::class,
22
        CarbonInterface::class
23
    ];
24
25
    private $before = [
26
        CarbonInterface::class => DateTimeInterface::class,
27
        Carbon::class => DateTime::class,
28
    ];
29
30
    private $after = [
31
        DateTime::class => Carbon::class,
32
        DateTimeInterface::class => Carbon::class,
33
        Carbon::class => Carbon::class,
34
        CarbonInterface::class => Carbon::class,
35
    ];
36
37
    public function __construct($defaultContext = [], DateTimeZone $timezone = null)
38
    {
39
        parent::__construct($defaultContext, $timezone);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Component\Serial...rmalizer::__construct() has too many arguments starting with $timezone. ( Ignorable by Annotation )

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

39
        parent::/** @scrutinizer ignore-call */ 
40
                __construct($defaultContext, $timezone);

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. Please note the @ignore annotation hint above.

Loading history...
40
        // carbon 2 support.
41
        if (class_exists(CarbonImmutable::class)) {
42
            $this->allowedTypes[] = CarbonImmutable::class;
43
            $this->before[CarbonImmutable::class] = DateTimeImmutable::class;
44
            $this->after[DateTimeInterface::class] = CarbonImmutable::class;
45
            $this->after[CarbonInterface::class] = CarbonImmutable::class;
46
            $this->after[CarbonImmutable::class] = CarbonImmutable::class;
47
            $this->after[DateTimeImmutable::class] = CarbonImmutable::class;
48
        }
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function denormalize($data, $type, $format = null, array $context = [])
55
    {
56
        $internalType = $type;
57
        if (isset($this->before[$type])) {
58
            $internalType = $this->before[$type];
59
        }
60
        $result = parent::denormalize($data, $internalType, $format, $context);
61
        if (isset($this->after[$type])) {
62
            return $this->after[$type]::make($result);
63
        }
64
        return $result;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function supportsDenormalization($data, $type, $format = null)
71
    {
72
        return in_array($type, $this->allowedTypes) || parent::supportsDenormalization($data, $type, $format);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function hasCacheableSupportsMethod(): bool
79
    {
80
        return true;
81
    }
82
}
83