Test Failed
Push — master ( dbe410...b8c007 )
by Kirill
02:19
created

Document::getDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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;
11
12
use Railt\Io\File;
13
use Railt\Io\Readable;
14
use Railt\Reflection\Common\Serializable;
15
use Railt\Reflection\Contracts\Definition\Behaviour\ProvidesTypeDefinitions;
16
use Railt\Reflection\Contracts\Definition\TypeDefinition;
17
use Railt\Reflection\Contracts\Dictionary;
18
use Railt\Reflection\Contracts\Document as DocumentInterface;
19
use Railt\Reflection\Contracts\Reflection as ReflectionInterface;
20
use Railt\Reflection\Contracts\Type as TypeInterface;
21
use Railt\Reflection\Definition\Behaviour\HasDefinitions;
22
use Railt\Reflection\Invocation\Behaviour\HasDirectives;
23
24
/**
25
 * Class Document
26
 */
27
class Document extends AbstractDefinition implements DocumentInterface
28
{
29
    use Serializable;
30
    use HasDirectives;
31
    use HasDefinitions {
32
        withDefinition as private __withDefinition;
33
    }
34
35
    /**
36
     * @var Readable
37
     */
38
    protected $file;
39
40
    /**
41
     * @var Reflection
42
     */
43
    protected $dictionary;
44
45
    /**
46
     * DocumentDefinition constructor.
47
     * @param Reflection|ReflectionInterface $parent
48
     * @param Readable|null $file
49
     */
50 9
    public function __construct(Reflection $parent, Readable $file = null)
51
    {
52 9
        $this->file = $file ?? File::fromSources('');
53 9
        $this->dictionary = $parent;
54
55 9
        parent::__construct($this);
56
57 9
        $parent->addDocument($this);
58 9
    }
59
60
    /**
61
     * @return TypeInterface
62
     * @throws \Railt\Io\Exception\ExternalFileException
63
     */
64
    public static function getType(): TypeInterface
65
    {
66
        return Type::of(Type::DOCUMENT);
67
    }
68
69
    /**
70
     * @return Readable
71
     */
72 58
    public function getFile(): Readable
73
    {
74 58
        return $this->file;
75
    }
76
77
    /**
78
     * @return ReflectionInterface|Reflection|Dictionary
79
     */
80 17
    public function getDictionary(): Dictionary
81
    {
82 17
        return $this->dictionary;
83
    }
84
85
    /**
86
     * @return bool
87
     */
88 1
    public function isDeprecated(): bool
89
    {
90 1
        return false;
91
    }
92
93
    /**
94
     * @return string
95
     */
96 1
    public function getDeprecationReason(): string
97
    {
98 1
        return '';
99
    }
100
101
    /**
102
     * @return string
103
     */
104 17
    public function getName(): string
105
    {
106 17
        return $this->file->isFile() ? $this->file->getPathname() : $this->file->getHash();
107
    }
108
109
    /**
110
     * @param string|TypeDefinition $type
111
     * @return ProvidesTypeDefinitions
112
     */
113 17
    public function withDefinition($type): ProvidesTypeDefinitions
114
    {
115 17
        if ($type instanceof TypeDefinition) {
116 17
            $this->dictionary->add($type);
117
        }
118
119 17
        return $this->__withDefinition($type);
120
    }
121
}
122