• 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

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

29

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

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

49
            break;
3✔
50
        }
51

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

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

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

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

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

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

84
                $phpcsFile->fixer->addNewline($i);
3✔
85
                $phpcsFile->fixer->endChangeset();
3✔
86
            }
87
        }
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