Passed
Pull Request — master (#19)
by
unknown
02:19
created

DocHelper::getCommentEndPointer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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