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

PHPCSStandards / PHP_CodeSniffer / 15253296250

26 May 2025 11:55AM UTC coverage: 78.632% (+0.3%) from 78.375%
15253296250

Pull #1105

github

web-flow
Merge d9441d98f into caf806050
Pull Request #1105: Skip tests when 'git' command is not available

19665 of 25009 relevant lines covered (78.63%)

88.67 hits per line

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

95.24
/src/Standards/PSR12/Sniffs/Classes/ClassInstantiationSniff.php
1
<?php
2
/**
3
 * Verifies that classes are instantiated with parentheses.
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\Classes;
11

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

16
class ClassInstantiationSniff implements Sniff
17
{
18

19

20
    /**
21
     * Returns an array of tokens this test wants to listen for.
22
     *
23
     * @return array<int|string>
24
     */
25
    public function register()
3✔
26
    {
27
        return [T_NEW];
3✔
28

29
    }//end register()
30

31

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

45
        // Find the class name.
46
        $allowed  = Tokens::NAME_TOKENS;
3✔
47
        $allowed += [
2✔
48
            T_SELF                     => T_SELF,
3✔
49
            T_STATIC                   => T_STATIC,
3✔
50
            T_PARENT                   => T_PARENT,
3✔
51
            T_VARIABLE                 => T_VARIABLE,
3✔
52
            T_DOLLAR                   => T_DOLLAR,
3✔
53
            T_OBJECT_OPERATOR          => T_OBJECT_OPERATOR,
3✔
54
            T_NULLSAFE_OBJECT_OPERATOR => T_NULLSAFE_OBJECT_OPERATOR,
3✔
55
            T_DOUBLE_COLON             => T_DOUBLE_COLON,
3✔
56
        ];
2✔
57

58
        $allowed += Tokens::EMPTY_TOKENS;
3✔
59

60
        $classNameEnd = null;
3✔
61
        for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
3✔
62
            if (isset($allowed[$tokens[$i]['code']]) === true) {
3✔
63
                continue;
3✔
64
            }
65

66
            // Bow out when this is an anonymous class.
67
            // Anonymous classes are the only situation which would allow for an attribute
68
            // or for the readonly keyword between "new" and the class "name".
69
            if ($tokens[$i]['code'] === T_ATTRIBUTE
3✔
70
                || $tokens[$i]['code'] === T_READONLY
3✔
71
                || $tokens[$i]['code'] === T_ANON_CLASS
3✔
72
            ) {
73
                return;
3✔
74
            }
75

76
            if ($tokens[$i]['code'] === T_OPEN_SQUARE_BRACKET
3✔
77
                || $tokens[$i]['code'] === T_OPEN_CURLY_BRACKET
3✔
78
            ) {
79
                $i = $tokens[$i]['bracket_closer'];
3✔
80
                continue;
3✔
81
            }
82

83
            $classNameEnd = $i;
3✔
84
            break;
3✔
85
        }//end for
86

87
        if ($classNameEnd === null) {
3✔
88
            return;
×
89
        }
90

91
        if ($tokens[$classNameEnd]['code'] === T_OPEN_PARENTHESIS) {
3✔
92
            // Using parenthesis.
93
            return;
3✔
94
        }
95

96
        if ($classNameEnd === $stackPtr) {
3✔
97
            // Failed to find the class name.
98
            return;
×
99
        }
100

101
        $error = 'Parentheses must be used when instantiating a new class';
3✔
102
        $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'MissingParentheses');
3✔
103
        if ($fix === true) {
3✔
104
            $prev = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($classNameEnd - 1), null, true);
3✔
105
            $phpcsFile->fixer->addContent($prev, '()');
3✔
106
        }
107

108
    }//end process()
1✔
109

110

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