Completed
Push — master ( 611e0d...3b7232 )
by Kirill
02:21
created

AbstractDefinition::withOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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;
11
12
use Railt\Io\Readable;
13
use Railt\Reflection\Common\Serializable;
14
use Railt\Reflection\Contracts\Definition;
15
use Railt\Reflection\Contracts\Definition\TypeDefinition;
16
use Railt\Reflection\Contracts\Document as DocumentInterface;
17
use Railt\Reflection\Contracts\Invocation\TypeInvocation;
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 = 0;
36
37
    /**
38
     * AbstractDefinition constructor.
39
     * @param Document $document
40
     */
41 8
    public function __construct(Document $document)
42
    {
43 8
        $this->document = $document;
44 8
    }
45
46
    /**
47
     * @param TypeInterface $type
48
     * @return bool
49
     */
50
    public static function typeOf(TypeInterface $type): bool
51
    {
52
        return static::getType()->instanceOf($type);
53
    }
54
55
    /**
56
     * @param int $offset
57
     * @return Definition|TypeDefinition|TypeInvocation|$this
58
     */
59 1
    public function withOffset(int $offset): Definition
60
    {
61 1
        $this->offset = $offset;
62
63 1
        return $this;
64
    }
65
66
    /**
67
     * @return DocumentInterface
68
     */
69 6
    public function getDocument(): DocumentInterface
70
    {
71 6
        return $this->document;
72
    }
73
74
    /**
75
     * @return int
76
     */
77 1
    public function getLine(): int
78
    {
79 1
        return $this->getFile()->getPosition($this->offset)->getLine();
80
    }
81
82
    /**
83
     * @return Readable
84
     */
85 1
    public function getFile(): Readable
86
    {
87 1
        return $this->document->getFile();
88
    }
89
90
    /**
91
     * @return int
92
     */
93 2
    public function getColumn(): int
94
    {
95 2
        return $this->getFile()->getPosition($this->offset)->getColumn();
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function __toString(): string
102
    {
103
        return \sprintf('?<%s>', static::getType());
104
    }
105
106
    /**
107
     * @param string $type
108
     * @return TypeDefinition
109
     */
110 1
    protected function fetch(string $type): TypeDefinition
111
    {
112 1
        return $this->document->getDictionary()->get($type, $this);
113
    }
114
}
115