Completed
Push — master ( 96b2f8...da2c9c )
by Kirill
05:58
created

AbstractDefinition::fetch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\Exception\ExternalFileException;
13
use Railt\Io\Readable;
14
use Railt\Reflection\Common\Serializable;
15
use Railt\Reflection\Contracts\Definition;
16
use Railt\Reflection\Contracts\Definition\TypeDefinition;
17
use Railt\Reflection\Contracts\Document as DocumentInterface;
18
use Railt\Reflection\Contracts\Type as TypeInterface;
19
20
/**
21
 * Class AbstractDefinition
22
 */
23
abstract class AbstractDefinition implements Definition
24
{
25
    use Serializable;
26
27
    /**
28
     * @var Document
29
     */
30
    protected $document;
31
32
    /**
33
     * @var int
34
     */
35
    protected $offset;
36
37
    /**
38
     * AbstractDefinition constructor.
39
     * @param Document $document
40
     * @param int $offset
41
     */
42
    public function __construct(Document $document, int $offset)
43
    {
44
        $this->offset   = $offset;
45
        $this->document = $document;
46
    }
47
48
    /**
49
     * @return DocumentInterface
50
     */
51
    public function getDocument(): DocumentInterface
52
    {
53
        return $this->document;
54
    }
55
56
    /**
57
     * @return int
58
     */
59
    public function getLine(): int
60
    {
61
        return $this->getFile()->getPosition($this->offset)->getLine();
62
    }
63
64
    /**
65
     * @return Readable
66
     */
67
    public function getFile(): Readable
68
    {
69
        return $this->document->getFile();
70
    }
71
72
    /**
73
     * @return int
74
     */
75
    public function getColumn(): int
76
    {
77
        return $this->getFile()->getPosition($this->offset)->getColumn();
78
    }
79
80
    /**
81
     * @param TypeInterface $type
82
     * @return bool
83
     */
84
    public function typeOf(TypeInterface $type): bool
85
    {
86
        return static::getType()->instanceOf($type);
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function __toString(): string
93
    {
94
        if (\method_exists($this, 'getName')) {
95
            return \sprintf('%s<%s>', $this->getName(), static::getType());
96
        }
97
98
        return \sprintf('?<%s>', static::getType());
99
    }
100
101
    /**
102
     * @param string $type
103
     * @return TypeDefinition
104
     * @throws ExternalFileException
105
     */
106
    protected function fetch(string $type): TypeDefinition
107
    {
108
        return $this->document->getReflection()->fetch($type, $this);
109
    }
110
}
111