• 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

86.96
/src/Standards/PSR12/Sniffs/Namespaces/CompoundNamespaceDepthSniff.php
1
<?php
2
/**
3
 * Verifies that compound namespaces are not defined too deep.
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\PSR12\Sniffs\Namespaces;
11

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

15
class CompoundNamespaceDepthSniff implements Sniff
16
{
17

18
    /**
19
     * The max depth for compound namespaces.
20
     *
21
     * @var integer
22
     */
23
    public $maxDepth = 2;
24

25

26
    /**
27
     * Returns an array of tokens this test wants to listen for.
28
     *
29
     * @return array<int|string>
30
     */
31
    public function register()
3✔
32
    {
33
        return [T_OPEN_USE_GROUP];
3✔
34
    }
35

36

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

50
        $tokens = $phpcsFile->getTokens();
3✔
51

52
        $end = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($stackPtr + 1));
3✔
53
        if ($end === false) {
3✔
UNCOV
54
            return;
×
55
        }
56

57
        $depth = 1;
3✔
58
        for ($i = ($stackPtr + 1); $i <= $end; $i++) {
3✔
59
            if ($tokens[$i]['code'] === T_NS_SEPARATOR) {
3✔
UNCOV
60
                $depth++;
×
61
                continue;
×
62
            }
63

64
            if ($tokens[$i]['code'] === T_NAME_FULLY_QUALIFIED || $tokens[$i]['code'] === T_NAME_QUALIFIED) {
3✔
65
                $depth += substr_count($tokens[$i]['content'], '\\');
3✔
66
                continue;
3✔
67
            }
68

69
            if ($i === $end || $tokens[$i]['code'] === T_COMMA) {
3✔
70
                // End of a namespace.
71
                if ($depth > $this->maxDepth) {
3✔
72
                    $error = 'Compound namespaces cannot have a depth more than %s';
3✔
73
                    $data  = [$this->maxDepth];
3✔
74
                    $phpcsFile->addError($error, $i, 'TooDeep', $data);
3✔
75
                }
76

77
                $depth = 1;
3✔
78
            }
79
        }//end for
80
    }
1✔
81
}
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