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

SpaceAfterDeclareSniff::process()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
ccs 16
cts 16
cp 1
rs 8.5806
cc 4
eloc 15
nc 4
nop 2
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\Sniffs\Formatting;
6
7
use PHP_CodeSniffer_File;
8
use PHP_CodeSniffer_Sniff;
9
10
/**
11
 * Class SpaceAfterDeclareSniff
12
 *
13
 * @package BestIt\Sniffs\Formatting
14
 *
15
 * @author Nick Lubisch <[email protected]>
16
 */
17
class SpaceAfterDeclareSniff implements PHP_CodeSniffer_Sniff
18
{
19
    /**
20
     * Error message when no whitespace is found.
21
     *
22
     * @var string
23
     */
24
    const MESSAGE_NO_WHITESPACE_FOUND = 'There is no whitespace after declare-statement.';
25
26
    /**
27
     * Code when no whitespace is found.
28
     *
29
     * @var string
30
     */
31
    const CODE_NO_WHITESPACE_FOUND = 'NoWhitespaceFound';
32
33
    /**
34
     * Error message when more than one whitespaces are found.
35
     *
36
     * @var string
37
     */
38
    const MESSAGE_MUCH_WHITESPACE_FOUND = 'There are more than one whitespaces after declare-statement.';
39
40
    /**
41
     * Code when more than one whitespaces are found.
42
     *
43
     * @var string
44
     */
45
    const CODE_MUCH_WHITESPACE_FOUND = 'MuchWhitespaceFound';
46
47
    /**
48
     * Registers the tokens that this sniff wants to listen for.
49
     *
50
     * @return int[]
51
     */
52 5
    public function register()
53
    {
54
        return [
55 5
            T_DECLARE
56
        ];
57
    }
58
59
    /**
60
     * Called when one of the token types that this sniff is listening for
61
     * is found.
62
     *
63
     * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the token was found.
64
     * @param int $stackPtr The position in the PHP_CodeSniffer file's token stack where the token was found.
65
     *
66
     * @return void|int Optionally returns a stack pointer.
67
     */
68 5
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
69
    {
70 5
        $tokens = $phpcsFile->getTokens();
71
72 5
        $semicolonPtr = $phpcsFile->findEndOfStatement($stackPtr);
73
74 5
        $secondSpacePtr = $semicolonPtr + 2;
75 5
        $secondSpaceToken = $tokens[$secondSpacePtr];
76
77 5
        if ($secondSpaceToken['code'] !== T_WHITESPACE) {
78 3
            $this->handleNoWhitespaceFound($phpcsFile, $semicolonPtr);
79
80 3
            return;
81
        }
82
83 5
        $nextNonSpacePtr = $phpcsFile->findNext(T_WHITESPACE, $secondSpacePtr, null, true);
84
85 5
        if ($nextNonSpacePtr === false) {
86 1
            return;
87
        }
88
89 4
        $nextNonSpaceToken = $tokens[$nextNonSpacePtr];
90
91 4
        if (($nextNonSpaceToken['line'] - $secondSpaceToken['line']) > 1) {
92 1
            $this->handleMuchWhitespacesFound($phpcsFile, $semicolonPtr, $secondSpacePtr, $nextNonSpacePtr);
93
94 1
            return;
95
        }
96 4
    }
97
98
    /**
99
     * Handles when no whitespace is found.
100
     *
101
     * @param PHP_CodeSniffer_File $phpcsFile
102
     * @param int $semicolonPtr
103
     *
104
     * @return void
105
     */
106 3
    private function handleNoWhitespaceFound(PHP_CodeSniffer_File $phpcsFile, $semicolonPtr)
107
    {
108 3
        $fixNoWhitespace = $phpcsFile->addFixableError(
109 3
            self::MESSAGE_NO_WHITESPACE_FOUND,
110
            $semicolonPtr,
111 3
            self::CODE_NO_WHITESPACE_FOUND
112
        );
113
114 3
        if ($fixNoWhitespace) {
115 3
            $phpcsFile->fixer->beginChangeset();
116 3
            $phpcsFile->fixer->addNewline($semicolonPtr);
117 3
            $phpcsFile->fixer->endChangeset();
118
        }
119 3
    }
120
121
    /**
122
     * Handles when more than one whitespaces are found.
123
     *
124
     * @param PHP_CodeSniffer_File $phpcsFile
125
     * @param int $semicolonPtr
126
     * @param int $secondSpacePtr
127
     * @param int $nextNonSpacePtr
128
     *
129
     * @return void
130
     */
131 1
    private function handleMuchWhitespacesFound(
132
        PHP_CodeSniffer_File $phpcsFile,
133
        $semicolonPtr,
134
        $secondSpacePtr,
135
        $nextNonSpacePtr
136
    ) {
137 1
        $fixMuchWhitespaces = $phpcsFile->addFixableError(
138 1
            self::MESSAGE_MUCH_WHITESPACE_FOUND,
139
            $semicolonPtr,
140 1
            self::CODE_MUCH_WHITESPACE_FOUND
141
        );
142
143 1
        if ($fixMuchWhitespaces) {
144 1
            $phpcsFile->fixer->beginChangeset();
145 1
            for ($i = $secondSpacePtr; $i < $nextNonSpacePtr; $i++) {
146 1
                $phpcsFile->fixer->replaceToken($i, '');
147
            }
148 1
            $phpcsFile->fixer->endChangeset();
149
        }
150 1
    }
151
}
152