Test Failed
Push — master ( da2c9c...d0cc4d )
by Kirill
02:03
created

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