• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 17663621563

12 Sep 2025 03:25AM UTC coverage: 78.786%. Remained the same
17663621563

push

github

web-flow
Merge pull request #1243 from PHPCSStandards/phpcs-4.x/feature/155-normalize-some-code-style-rules-5

CS: normalize code style rules [5]

294 of 308 new or added lines in 191 files covered. (95.45%)

2354 existing lines in 130 files now uncovered.

19732 of 25045 relevant lines covered (78.79%)

96.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.0
/src/Standards/PSR12/Sniffs/Functions/NullableTypeDeclarationSniff.php
1
<?php
2
/**
3
 * Verifies that nullable typehints are lacking superfluous whitespace, e.g. ?int
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2018 Squiz Pty Ltd (ABN 77 084 670 600)
7
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8
 */
9

10
namespace PHP_CodeSniffer\Standards\PSR12\Sniffs\Functions;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Sniffs\Sniff;
14
use PHP_CodeSniffer\Util\Tokens;
15

16
class NullableTypeDeclarationSniff implements Sniff
17
{
18

19
    /**
20
     * An array of valid tokens after `T_NULLABLE` occurrences.
21
     *
22
     * @var array<int|string, int|string>
23
     */
24
    private const VALID_TOKENS = (Tokens::NAME_TOKENS + [
25
        T_CALLABLE => T_CALLABLE,
26
        T_SELF     => T_SELF,
27
        T_PARENT   => T_PARENT,
28
        T_STATIC   => T_STATIC,
29
        T_NULL     => T_NULL,
30
        T_FALSE    => T_FALSE,
31
        T_TRUE     => T_TRUE,
32
    ]);
33

34

35
    /**
36
     * Returns an array of tokens this test wants to listen for.
37
     *
38
     * @return array<int|string>
39
     */
40
    public function register()
3✔
41
    {
42
        return [T_NULLABLE];
3✔
43
    }
44

45

46
    /**
47
     * Processes this test, when one of its tokens is encountered.
48
     *
49
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
50
     * @param int                         $stackPtr  The position of the current token in the
51
     *                                               stack passed in $tokens.
52
     *
53
     * @return void
54
     */
55
    public function process(File $phpcsFile, int $stackPtr)
3✔
56
    {
57
        $nextNonEmptyPtr = $phpcsFile->findNext([T_WHITESPACE], ($stackPtr + 1), null, true);
3✔
58
        if ($nextNonEmptyPtr === false) {
3✔
59
            // Parse error or live coding.
UNCOV
60
            return;
×
61
        }
62

63
        $tokens           = $phpcsFile->getTokens();
3✔
64
        $nextNonEmptyCode = $tokens[$nextNonEmptyPtr]['code'];
3✔
65
        $validTokenFound  = isset(self::VALID_TOKENS[$nextNonEmptyCode]);
3✔
66

67
        if ($validTokenFound === true && $nextNonEmptyPtr === ($stackPtr + 1)) {
3✔
68
            // Valid structure.
69
            return;
3✔
70
        }
71

72
        $error = 'There must not be a space between the question mark and the type in nullable type declarations';
3✔
73

74
        if ($validTokenFound === true) {
3✔
75
            // No other tokens then whitespace tokens found; fixable.
76
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'WhitespaceFound');
3✔
77
            if ($fix === true) {
3✔
78
                for ($i = ($stackPtr + 1); $i < $nextNonEmptyPtr; $i++) {
3✔
79
                    $phpcsFile->fixer->replaceToken($i, '');
3✔
80
                }
81
            }
82

83
            return;
3✔
84
        }
85

86
        // Non-whitespace tokens found; trigger error but don't fix.
87
        $phpcsFile->addError($error, $stackPtr, 'UnexpectedCharactersFound');
3✔
88
    }
1✔
89
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc