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

PHPCSStandards / PHP_CodeSniffer / 13349185939

15 Feb 2025 10:26PM UTC coverage: 78.563% (+0.1%) from 78.468%
13349185939

Pull #816

github

web-flow
Merge f016606cd into 6b11ef60b
Pull Request #816: File::getDeclarationName(): prevent incorrect result during live coding

7 of 7 new or added lines in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

24727 of 31474 relevant lines covered (78.56%)

66.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.24
/src/Standards/Squiz/Sniffs/Classes/ValidClassNameSniff.php
1
<?php
2
/**
3
 * Ensures classes are in camel caps, and the first letter is capitalised.
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\Squiz\Sniffs\Classes;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Sniffs\Sniff;
14
use PHP_CodeSniffer\Util\Common;
15
use PHP_CodeSniffer\Util\Tokens;
16

17
class ValidClassNameSniff implements Sniff
18
{
19

20

21
    /**
22
     * Returns an array of tokens this test wants to listen for.
23
     *
24
     * @return array<int|string>
25
     */
26
    public function register()
3✔
27
    {
28
        return [
1✔
29
            T_CLASS,
3✔
30
            T_INTERFACE,
3✔
31
            T_TRAIT,
3✔
32
            T_ENUM,
3✔
33
        ];
2✔
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 current file being processed.
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
        $tokens = $phpcsFile->getTokens();
3✔
50

51
        if (isset($tokens[$stackPtr]['scope_opener']) === false) {
3✔
52
            $error = 'Possible parse error: %s missing opening or closing brace';
×
53
            $data  = [$tokens[$stackPtr]['content']];
×
54
            $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data);
×
UNCOV
55
            return;
×
56
        }
57

58
        // Determine the name of the class or interface. Note that we cannot
59
        // simply look for the first T_STRING because a class name
60
        // starting with the number will be multiple tokens.
61
        $opener    = $tokens[$stackPtr]['scope_opener'];
3✔
62
        $nameStart = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), $opener, true);
3✔
63
        $nameEnd   = $phpcsFile->findNext((Tokens::$emptyTokens + [T_COLON => T_COLON]), $nameStart, $opener);
3✔
64
        if ($nameEnd === false) {
3✔
65
            $name = $tokens[$nameStart]['content'];
3✔
66
        } else {
1✔
67
            $name = trim($phpcsFile->getTokensAsString($nameStart, ($nameEnd - $nameStart)));
3✔
68
        }
69

70
        // Check for PascalCase format.
71
        $valid = Common::isCamelCaps($name, true, true, false);
3✔
72
        if ($valid === false) {
3✔
73
            $type  = ucfirst($tokens[$stackPtr]['content']);
3✔
74
            $error = '%s name "%s" is not in PascalCase format';
3✔
75
            $data  = [
1✔
76
                $type,
3✔
77
                $name,
3✔
78
            ];
2✔
79
            $phpcsFile->addError($error, $nameStart, 'NotCamelCaps', $data);
3✔
80
            $phpcsFile->recordMetric($stackPtr, 'PascalCase class name', 'no');
3✔
81
        } else {
1✔
82
            $phpcsFile->recordMetric($stackPtr, 'PascalCase class name', 'yes');
3✔
83
        }
84

85
    }//end process()
2✔
86

87

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