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

PHPCSStandards / PHP_CodeSniffer / 14523709455

17 Apr 2025 07:53PM UTC coverage: 77.921% (-0.1%) from 78.02%
14523709455

push

github

web-flow
Merge pull request #1020 from PHPCSStandards/phpcs-4.0/feature/3041-tokenizer-php-namespaced-name-tokenization

Tokenizer/PHP: namespaced names as single token, mirroring PHP 8.0+

203 of 211 new or added lines in 25 files covered. (96.21%)

6 existing lines in 3 files now uncovered.

19389 of 24883 relevant lines covered (77.92%)

85.62 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
    }//end register()
36

37

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

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

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

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

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

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

78
                $depth = 1;
3✔
79
            }
80
        }//end for
81

82
    }//end process()
1✔
83

84

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