Test Failed
Push — master ( 591538...755e43 )
by Kirill
02:28
created

DateTimeScalar::parse()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 9.6666
cc 4
nc 4
nop 1
crap 20
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Reflection\Stdlib\Scalar;
11
12
use Railt\Reflection\Definition\ScalarDefinition;
13
use Railt\Reflection\Document;
14
use Railt\Reflection\Exception\TypeConflictException;
15
16
/**
17
 * Class DateTimeScalar
18
 */
19
class DateTimeScalar extends ScalarDefinition
20
{
21
    /**
22
     * @var string
23
     */
24
    public const TYPE_NAME = 'DateTime';
25
26
    /**
27
     * @var string
28
     */
29
    public const TYPE_DESCRIPTION = 'The DateTime scalar conforms to the RFC 3339 profile of the ISO 8601 standard.';
30
31
    /**
32
     * BooleanScalar constructor.
33
     * @param Document $document
34 9
     */
35
    public function __construct(Document $document)
36 9
    {
37
        parent::__construct($document, self::TYPE_NAME);
38 9
39 9
        $this->withDescription(self::TYPE_DESCRIPTION);
40
    }
41
42
    /**
43
     * @param mixed $value
44
     * @return \DateTimeInterface
45
     * @throws TypeConflictException
46
     */
47
    public function parse($value): \DateTimeInterface
48
    {
49
        if ($value instanceof \DateTimeInterface) {
50
            return $value;
51
        }
52
53
        if (! \is_scalar($value)) {
54
            throw new TypeConflictException(\sprintf('Could not parse %s type', \gettype($value)));
55
        }
56
57
        $value = (string)parent::parse($value);
58
59
        try {
60
            return new \DateTime($value);
61
        } catch (\Throwable $e) {
62
            throw new TypeConflictException($e->getMessage(), $e->getCode());
63
        }
64
    }
65
66
    /**
67
     * @param mixed $value
68
     * @return string
69
     * @throws TypeConflictException
70
     */
71
    public function serialize($value): string
72
    {
73
        if ($value instanceof \DateTimeInterface) {
74
            return $value->format(\DateTime::RFC3339);
75
        }
76
77
        if (! \is_scalar($value)) {
78
            throw new TypeConflictException(\sprintf('Could not serialize %s type', \gettype($value)));
79
        }
80
81
        $value = parent::parse($value);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (parse() instead of serialize()). Are you sure this is correct? If so, you might want to change this to $this->parse().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
82
83
        try {
84
            return (new \DateTime((string)$value))->format(\DateTime::RFC3339);
85
        } catch (\Throwable $e) {
86
            throw new TypeConflictException($e->getMessage(), $e->getCode());
87
        }
88
    }
89
90
    /**
91
     * @return int
92
     */
93
    public function getLine(): int
94
    {
95
        return 31;
96
    }
97
}
98