Completed
Push — master ( 5ac91e...39483e )
by Kirill
38:30
created

BaseDefinition   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 139
ccs 22
cts 30
cp 0.7332
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getDocument() 0 4 1
A __toString() 0 4 1
A __debugInfo() 0 4 1
A jsonSerialize() 0 11 2
A __sleep() 0 15 1
A getName() 0 8 1
A getDescription() 0 4 1
A getUniqueId() 0 8 2
A getFileName() 0 4 1
A getDeclarationLine() 0 4 1
A getDeclarationColumn() 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\SDL\Base\Definitions;
11
12
use Railt\SDL\Base\Behavior\BaseDeprecations;
13
use Railt\SDL\Contracts\Definitions\Definition;
14
use Railt\SDL\Contracts\Document;
15
use Railt\SDL\Support;
16
17
/**
18
 * Class BaseTypeDefinition
19
 */
20
abstract class BaseDefinition implements Definition, \JsonSerializable
21
{
22
    use Support;
23
    use BaseDeprecations;
24
25
    /**
26
     * @var string
27
     */
28
    protected $name;
29
30
    /**
31
     * @var string
32
     */
33
    protected $description = '';
34
35
    /**
36
     * @var string|null
37
     */
38
    protected $id;
39
40
    /**
41
     * @var Document
42
     */
43
    protected $document;
44
45
    /**
46
     * @return Document
47
     */
48 319
    public function getDocument(): Document
49
    {
50 319
        return $this->document;
51
    }
52
53
    /**
54
     * @return string
55
     */
56 3645
    public function __toString(): string
57
    {
58 3645
        return $this->typeToString($this);
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function __debugInfo(): array
65
    {
66
        return $this->jsonSerialize();
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function jsonSerialize(): array
73
    {
74
        $data   = $this->__sleep();
75
        $result = [];
76
77
        foreach ($data as $fieldName) {
78
            $result[$fieldName] = $this->valueToScalar($this->$fieldName);
79
        }
80
81
        return $result;
82
    }
83
84
    /**
85
     * @return array
86
     */
87 1257
    public function __sleep(): array
88
    {
89
        return [
90
            // Self
91 1257
            'id',
92
            'document',
93
94
            // interface Nameable
95
            'name',
96
            'description',
97
98
            // trait BaseDeprecations
99
            'deprecationReason',
100
        ];
101
    }
102
103
    /**
104
     * @return string
105
     */
106 7333
    public function getName(): string
107
    {
108 7333
        \assert($this->name !== null, \vsprintf('Name of %s must be initialized.', [
109 7333
            \get_class($this),
110
        ]));
111
112 7333
        return (string)$this->name;
113
    }
114
115
    /**
116
     * @return string
117
     */
118 54
    public function getDescription(): string
119
    {
120 54
        return (string)$this->description;
121
    }
122
123
    /**
124
     * @return string
125
     */
126 7182
    public function getUniqueId(): string
127
    {
128 7182
        if ($this->id === null) {
129 7164
            $this->id = \spl_object_hash($this);
130
        }
131
132 7182
        return $this->id;
133
    }
134
135
    /**
136
     * @return string
137
     */
138 3651
    public function getFileName(): string
139
    {
140 3651
        return $this->getDocument()->getFileName();
141
    }
142
143
    /**
144
     * @return int
145
     */
146 36
    public function getDeclarationLine(): int
147
    {
148 36
        return 0;
149
    }
150
151
    /**
152
     * @return int
153
     */
154 36
    public function getDeclarationColumn(): int
155
    {
156 36
        return 0;
157
    }
158
}
159