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

PHPCSStandards / PHP_CodeSniffer / 15253296250

26 May 2025 11:55AM UTC coverage: 78.632% (+0.3%) from 78.375%
15253296250

Pull #1105

github

web-flow
Merge d9441d98f into caf806050
Pull Request #1105: Skip tests when 'git' command is not available

19665 of 25009 relevant lines covered (78.63%)

88.67 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

15
class NullableTypeDeclarationSniff implements Sniff
16
{
17

18
    /**
19
     * An array of valid tokens after `T_NULLABLE` occurrences.
20
     *
21
     * @var array
22
     */
23
    private $validTokens = [
24
        T_STRING               => true,
25
        T_NAME_QUALIFIED       => true,
26
        T_NAME_FULLY_QUALIFIED => true,
27
        T_NAME_RELATIVE        => true,
28
        T_CALLABLE             => true,
29
        T_SELF                 => true,
30
        T_PARENT               => true,
31
        T_STATIC               => true,
32
        T_NULL                 => true,
33
        T_FALSE                => true,
34
        T_TRUE                 => true,
35
    ];
36

37

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

47
    }//end register()
48

49

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

67
        $tokens           = $phpcsFile->getTokens();
3✔
68
        $nextNonEmptyCode = $tokens[$nextNonEmptyPtr]['code'];
3✔
69
        $validTokenFound  = isset($this->validTokens[$nextNonEmptyCode]);
3✔
70

71
        if ($validTokenFound === true && $nextNonEmptyPtr === ($stackPtr + 1)) {
3✔
72
            // Valid structure.
73
            return;
3✔
74
        }
75

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

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

87
            return;
3✔
88
        }
89

90
        // Non-whitespace tokens found; trigger error but don't fix.
91
        $phpcsFile->addError($error, $stackPtr, 'UnexpectedCharactersFound');
3✔
92

93
    }//end process()
1✔
94

95

96
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc