Completed
Pull Request — master (#44)
by Björn
02:58
created

DocPosProviderTrait::loadDocCommentPos()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\Sniffs;
6
7
use BestIt\CodeSniffer\File;
8
use BestIt\CodeSniffer\Helper\DocHelper;
9
10
/**
11
 * Trait DocPosProviderTrait.
12
 *
13
 * @author blange <[email protected]>
14
 * @package  BestIt\Sniffs
15
 */
16
trait DocPosProviderTrait
17
{
18
    /**
19
     * The position of the doc comment or null.
20
     *
21
     * @var int|null
22
     */
23
    private $docCommentPos = -1;
24
25
    /**
26
     * The used doc Helper.
27
     *
28
     * @var DocHelper
29
     */
30
    private $docHelper = null;
31
32
    /**
33
     * The used file.
34
     *
35
     * @var File|void
36
     */
37
    protected $file;
38
39
    /**
40
     * Position of the listened token.
41
     *
42
     * @var int|void
43
     */
44
    protected $stackPos;
45
46
    /**
47
     * Returns the position of the doc block if there is one.
48
     *
49
     * @return int|null
50
     */
51
    protected function getDocCommentPos(): ?int
52
    {
53
        if ($this->docCommentPos === -1) {
54
            $this->docCommentPos = $this->loadDocCommentPos();
55
        }
56
57
        return $this->docCommentPos;
58
    }
59
60
    /**
61
     * Returns the helper for the doc block.
62
     *
63
     * @return DocHelper
64
     */
65
    protected function getDocHelper(): DocHelper
66
    {
67
        if ($this->docHelper === null) {
68
            $this->docHelper = new DocHelper($this->getFile()->getBaseFile(), $this->getStackPos());
69
        }
70
71
        return $this->docHelper;
72
    }
73
74
    /**
75
     * Type safe getter for the file.
76
     *
77
     * @return File
78
     */
79
    private function getFile(): File
80
    {
81
        return $this->file;
82
    }
83
84
    /**
85
     * Type safe getter for the stack position for this sniff.
86
     *
87
     * @return int
88
     */
89
    private function getStackPos(): int
90
    {
91
        return $this->stackPos;
92
    }
93
94
    /**
95
     * Loads the position of the doc comment.
96
     *
97
     * @return int|null
98
     */
99
    protected function loadDocCommentPos(): ?int
100
    {
101
        $docHelper = $this->getDocHelper();
102
103
        return $docHelper->hasDocBlock() ? $docHelper->getBlockStartPosition() : null;
104
    }
105
106
    /**
107
     * Removes the cached data for the doc comment position.
108
     *
109
     * @return void
110
     */
111
    protected function resetDocCommentPos(): void
112
    {
113
        $this->docCommentPos = -1;
114
        $this->docHelper = null;
115
    }
116
}
117