Test Failed
Push — master ( 4bdf7b...97fe68 )
by Kirill
02:15
created

IdScalar   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 70
ccs 5
cts 15
cp 0.3333
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A parse() 0 8 2
A serialize() 0 8 2
A isBuiltin() 0 4 1
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 IdScalar
18
 */
19
final class IdScalar extends ScalarDefinition
20
{
21
    /**
22
     * @var string
23
     */
24
    public const TYPE_NAME = 'ID';
25
26
    /**
27
     * @var string
28
     */
29
    public const TYPE_DESCRIPTION = <<<Description
30
The ID scalar type represents a unique identifier, often used to refetch 
31
an object or as the key for a cache. The ID type is serialized in the 
32
same way as a String; however, defining it as an ID signifies that it 
33
is not intended to be human‐readable.
34
Description;
35
36
    /**
37
     * @var int
38
     */
39
    private const DEFINITION_LINE = 25;
40
41
    /**
42
     * BooleanScalar constructor.
43
     * @param Document $document
44
     */
45 9
    public function __construct(Document $document)
46
    {
47 9
        parent::__construct($document, self::TYPE_NAME);
48
49 9
        $this->withDescription(self::TYPE_DESCRIPTION);
50 9
        $this->withLine(self::DEFINITION_LINE);
51 9
    }
52
53
    /**
54
     * @param mixed $value
55
     * @return string
56
     * @throws TypeConflictException
57
     */
58
    public function parse($value): string
59
    {
60
        if (! \is_scalar($value)) {
61
            throw new TypeConflictException(\sprintf('Could not parse %s type', \gettype($value)));
62
        }
63
64
        return (string)parent::parse($value);
65
    }
66
67
    /**
68
     * @param mixed $value
69
     * @return string
70
     * @throws TypeConflictException
71
     */
72
    public function serialize($value): string
73
    {
74
        if (! \is_scalar($value)) {
75
            throw new TypeConflictException(\sprintf('Could not serialize %s type', \gettype($value)));
76
        }
77
78
        return (string)parent::serialize($value);
79
    }
80
81
    /**
82
     * @return bool
83
     */
84
    public function isBuiltin(): bool
85
    {
86
        return true;
87
    }
88
}
89