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

PHPCSStandards / PHP_CodeSniffer / 11374498954

16 Oct 2024 09:58PM UTC coverage: 75.662% (-1.0%) from 76.652%
11374498954

Pull #631

github

web-flow
Merge 567e361af into 16c087f0e
Pull Request #631: GH Actions/remove labels: update lists of labels to auto-remove

23372 of 30890 relevant lines covered (75.66%)

61.78 hits per line

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

96.3
/src/Standards/PSR1/Sniffs/Classes/ClassDeclarationSniff.php
1
<?php
2
/**
3
 * Checks the declaration of the class is correct.
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\PSR1\Sniffs\Classes;
11

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

15
class ClassDeclarationSniff 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 [
1✔
27
            T_CLASS,
3✔
28
            T_INTERFACE,
3✔
29
            T_TRAIT,
3✔
30
            T_ENUM,
3✔
31
        ];
2✔
32

33
    }//end register()
34

35

36
    /**
37
     * Processes this test, when one of its tokens is encountered.
38
     *
39
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
40
     * @param integer                     $stackPtr  The position of the current token in
41
     *                                               the token stack.
42
     *
43
     * @return void
44
     */
45
    public function process(File $phpcsFile, $stackPtr)
3✔
46
    {
47
        $tokens = $phpcsFile->getTokens();
3✔
48
        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
3✔
49
            return;
×
50
        }
51

52
        $errorData = [strtolower($tokens[$stackPtr]['content'])];
3✔
53

54
        $nextClass = $phpcsFile->findNext([T_CLASS, T_INTERFACE, T_TRAIT, T_ENUM], ($tokens[$stackPtr]['scope_closer'] + 1));
3✔
55
        if ($nextClass !== false) {
3✔
56
            $error = 'Each %s must be in a file by itself';
3✔
57
            $phpcsFile->addError($error, $nextClass, 'MultipleClasses', $errorData);
3✔
58
            $phpcsFile->recordMetric($stackPtr, 'One class per file', 'no');
3✔
59
        } else {
1✔
60
            $phpcsFile->recordMetric($stackPtr, 'One class per file', 'yes');
3✔
61
        }
62

63
        $namespace = $phpcsFile->findNext([T_NAMESPACE, T_CLASS, T_INTERFACE, T_TRAIT, T_ENUM], 0);
3✔
64
        if ($tokens[$namespace]['code'] !== T_NAMESPACE) {
3✔
65
            $error = 'Each %s must be in a namespace of at least one level (a top-level vendor name)';
3✔
66
            $phpcsFile->addError($error, $stackPtr, 'MissingNamespace', $errorData);
3✔
67
            $phpcsFile->recordMetric($stackPtr, 'Class defined in namespace', 'no');
3✔
68
        } else {
1✔
69
            $phpcsFile->recordMetric($stackPtr, 'Class defined in namespace', 'yes');
3✔
70
        }
71

72
    }//end process()
2✔
73

74

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