• 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

93.33
/src/Standards/PSR2/Sniffs/Namespaces/NamespaceDeclarationSniff.php
1
<?php
2
/**
3
 * Ensures namespaces are declared correctly.
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2015 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\PSR2\Sniffs\Namespaces;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Sniffs\Sniff;
14

15
class NamespaceDeclarationSniff implements Sniff
16
{
17

18

19
    /**
20
     * Returns an array of tokens this test wants to listen for.
21
     *
22
     * @return array<int|string>
23
     */
24
    public function register()
3✔
25
    {
26
        return [T_NAMESPACE];
3✔
27

28
    }//end register()
29

30

31
    /**
32
     * Processes this test, when one of its tokens is encountered.
33
     *
34
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
35
     * @param int                         $stackPtr  The position of the current token in
36
     *                                               the stack passed in $tokens.
37
     *
38
     * @return void
39
     */
40
    public function process(File $phpcsFile, $stackPtr)
3✔
41
    {
42
        $tokens = $phpcsFile->getTokens();
3✔
43

44
        $end = $phpcsFile->findEndOfStatement($stackPtr);
3✔
45
        for ($i = ($end + 1); $i < ($phpcsFile->numTokens - 1); $i++) {
3✔
46
            if ($tokens[$i]['line'] === $tokens[$end]['line']) {
3✔
47
                continue;
3✔
48
            }
49

50
            break;
3✔
51
        }
52

53
        // The $i var now points to the first token on the line after the
54
        // namespace declaration, which must be a blank line.
55
        $next = $phpcsFile->findNext(T_WHITESPACE, $i, $phpcsFile->numTokens, true);
3✔
56
        if ($next === false) {
3✔
57
            return;
×
58
        }
59

60
        $diff = ($tokens[$next]['line'] - $tokens[$i]['line']);
3✔
61
        if ($diff === 1) {
3✔
62
            return;
3✔
63
        }
64

65
        if ($diff < 0) {
3✔
66
            $diff = 0;
×
67
        }
68

69
        $error = 'There must be one blank line after the namespace declaration';
3✔
70
        $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'BlankLineAfter');
3✔
71

72
        if ($fix === true) {
3✔
73
            if ($diff === 0) {
3✔
74
                $phpcsFile->fixer->addNewlineBefore($i);
3✔
75
            } else {
76
                $phpcsFile->fixer->beginChangeset();
3✔
77
                for ($x = $i; $x < $next; $x++) {
3✔
78
                    if ($tokens[$x]['line'] === $tokens[$next]['line']) {
3✔
79
                        break;
3✔
80
                    }
81

82
                    $phpcsFile->fixer->replaceToken($x, '');
3✔
83
                }
84

85
                $phpcsFile->fixer->addNewline($i);
3✔
86
                $phpcsFile->fixer->endChangeset();
3✔
87
            }
88
        }
89

90
    }//end process()
1✔
91

92

93
}//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

© 2026 Coveralls, Inc