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

handleMuchWhitespacesFound()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 14
nc 3
nop 4
crap 3
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 3
    public function register()
53
    {
54
        return [
55 3
            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 3
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
69
    {
70 3
        $tokens = $phpcsFile->getTokens();
71
72 3
        $semicolonPtr = $phpcsFile->findEndOfStatement($stackPtr);
73
74 3
        $secondSpacePtr = $semicolonPtr + 2;
75 3
        $secondSpaceToken = $tokens[$secondSpacePtr];
76
77 3
        if ($secondSpaceToken['code'] !== T_WHITESPACE) {
78 2
            $this->handleNoWhitespaceFound($phpcsFile, $semicolonPtr);
79
80 2
            return;
81
        }
82
83 3
        $nextNonSpacePtr = $phpcsFile->findNext(T_WHITESPACE, $secondSpacePtr, null, true);
84 3
        $nextNonSpaceToken = $tokens[$nextNonSpacePtr];
85
86 3
        if (($nextNonSpaceToken['line'] - $secondSpaceToken['line']) > 1) {
87 1
            $this->handleMuchWhitespacesFound($phpcsFile, $semicolonPtr, $secondSpacePtr, $nextNonSpacePtr);
0 ignored issues
show
Security Bug introduced by
It seems like $nextNonSpacePtr defined by $phpcsFile->findNext(T_W...ndSpacePtr, null, true) on line 83 can also be of type false; however, BestIt\Sniffs\Formatting...eMuchWhitespacesFound() does only seem to accept integer, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
88
89 1
            return;
90
        }
91 3
    }
92
93
    /**
94
     * Handles when no whitespace is found.
95
     *
96
     * @param PHP_CodeSniffer_File $phpcsFile
97
     * @param int $semicolonPtr
98
     *
99
     * @return void
100
     */
101 2
    private function handleNoWhitespaceFound(PHP_CodeSniffer_File $phpcsFile, $semicolonPtr)
102
    {
103 2
        $fixNoWhitespace = $phpcsFile->addFixableError(
104 2
            self::MESSAGE_NO_WHITESPACE_FOUND,
105
            $semicolonPtr,
106 2
            self::CODE_NO_WHITESPACE_FOUND
107
        );
108
109 2
        if ($fixNoWhitespace) {
110 2
            $phpcsFile->fixer->beginChangeset();
111 2
            $phpcsFile->fixer->addNewline($semicolonPtr);
112 2
            $phpcsFile->fixer->endChangeset();
113
        }
114 2
    }
115
116
    /**
117
     * Handles when more than one whitespaces are found.
118
     *
119
     * @param PHP_CodeSniffer_File $phpcsFile
120
     * @param int $semicolonPtr
121
     * @param int $secondSpacePtr
122
     * @param int $nextNonSpacePtr
123
     *
124
     * @return void
125
     */
126 1
    private function handleMuchWhitespacesFound(
127
        PHP_CodeSniffer_File $phpcsFile,
128
        $semicolonPtr,
129
        $secondSpacePtr,
130
        $nextNonSpacePtr
131
    ) {
132 1
        $fixMuchWhitespaces = $phpcsFile->addFixableError(
133 1
            self::MESSAGE_MUCH_WHITESPACE_FOUND,
134
            $semicolonPtr,
135 1
            self::CODE_MUCH_WHITESPACE_FOUND
136
        );
137
138 1
        if ($fixMuchWhitespaces) {
139 1
            $phpcsFile->fixer->beginChangeset();
140 1
            for ($i = $secondSpacePtr; $i < $nextNonSpacePtr; $i++) {
141 1
                $phpcsFile->fixer->replaceToken($i, '');
142
            }
143 1
            $phpcsFile->fixer->endChangeset();
144
        }
145 1
    }
146
}
147