Completed
Pull Request — master (#19)
by
unknown
07:50
created

DocHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\CodeSniffer\Helper;
6
7
use BestIt\CodeSniffer\File;
8
use BestIt\Sniffs\Commenting\AbstractDocSniff;
9
use PHP_CodeSniffer_Tokens;
10
11
/**
12
 * Class DocHelper
13
 *
14
 * @package BestIt\Helper
15
 * @author Nick Lubisch <[email protected]>
16
 */
17
class DocHelper
18
{
19
    /**
20
     * The php cs file.
21
     *
22
     * @var File
23
     */
24
    private $file;
25
26
    /**
27
     * Pointer to the token which is to be listened.
28
     *
29
     * @var int
30
     */
31
    private $stackPtr;
32
33
    /**
34
     * Token stack of the current file.
35
     *
36
     * @var array
37
     */
38
    private $tokens;
39
40
    /**
41
     * DocHelper constructor.
42
     *
43
     * @param File $file File object of file which is processed.
44
     * @param int $stackPtr Pointer to the token which is processed.
45
     */
46 121
    public function __construct(File $file, $stackPtr)
47
    {
48 121
        $this->file = $file;
49 121
        $this->tokens = $file->getTokens();
50 121
        $this->stackPtr = $stackPtr;
51 121
    }
52
53
    /**
54
     * Checks if a comment for the class exists.
55
     *
56
     * @param int $listenerPtr Pointer of the listener token
57
     *
58
     * @return bool Indicator if the comment exists or not
59
     */
60 121
    public function checkCommentExists(int $listenerPtr): bool
61
    {
62 121
        $listenerToken = $this->tokens[$listenerPtr];
63 121
        $commentEndToken = $this->getCommentEndToken();
64
65 121
        if ($commentEndToken['type'] !== 'T_DOC_COMMENT_CLOSE_TAG'
66 121
            || ($listenerToken['line'] - 1) !== $commentEndToken['line']
67
        ) {
68 4
            $this->file->addError(
69 4
                AbstractDocSniff::MESSAGE_NO_IMMEDIATE_DOC_FOUND,
70 4
                $listenerPtr,
71 4
                AbstractDocSniff::CODE_NO_IMMEDIATE_DOC_FOUND
72
            );
73
74 4
            return false;
75
        }
76
77 117
        return true;
78
    }
79
80
    /**
81
     * Checks if the comment is multi line.
82
     *
83
     * @return bool Indicator if the comment is multiline
84
     */
85 117
    public function checkCommentMultiLine(): bool
86
    {
87 117
        $commentStart = $this->getCommentStartToken();
88 117
        $commentEnd = $this->getCommentEndToken();
89
90 117
        if ($commentStart['line'] === $commentEnd['line']) {
91 4
            $this->file->addErrorOnLine(
92 4
                AbstractDocSniff::MESSAGE_COMMENT_NOT_MULTI_LINE,
93 4
                $commentStart['line'],
94 4
                AbstractDocSniff::CODE_COMMENT_NOT_MULTI_LINE
95
            );
96
97 4
            return false;
98
        }
99
100 113
        return true;
101
    }
102
103
    /**
104
     * Returns pointer to the class comment start.
105
     *
106
     * @return int Pointer to the class comment start.
107
     */
108 117
    public function getCommentStartPointer(): int
109
    {
110 117
        $commentEndToken = $this->getCommentEndToken();
111
112 117
        return $commentEndToken['comment_opener'];
113
    }
114
115
    /**
116
     * Returns token data of the evaluated class comment start.
117
     *
118
     * @return array Token data of the comment start.
119
     */
120 117
    public function getCommentStartToken(): array
121
    {
122 117
        $commentStartPtr = $this->getCommentStartPointer();
123
124 117
        return $this->tokens[$commentStartPtr];
125
    }
126
127
    /**
128
     * Returns pointer to the class comment end.
129
     *
130
     * @return int Pointer to the class comment end.
131
     */
132 121
    public function getCommentEndPointer(): int
133
    {
134 121
        $whitelistedTokens = array_merge(
135 121
            [T_WHITESPACE],
136 121
            PHP_CodeSniffer_Tokens::$methodPrefixes
137
        );
138
139 121
        return $this->file->findPrevious(
140 121
            $whitelistedTokens,
141 121
            $this->stackPtr - 1,
142 121
            null,
143 121
            true
144
        );
145
    }
146
147
    /**
148
     * Returns token data of the evaluated class comment end.
149
     *
150
     * @return array Token data of the comment end.
151
     */
152 121
    public function getCommentEndToken(): array
153
    {
154 121
        return $this->tokens[$this->getCommentEndPointer()];
155
    }
156
}
157