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

PHPCSStandards / PHP_CodeSniffer / 8532137691

03 Apr 2024 02:02AM UTC coverage: 72.352% (+0.02%) from 72.328%
8532137691

push

github

jrfnl
File::getMethodProperties(): skip over closure use statements

This PR improves performance of the `File::getMethodProperties()` method and prevents incorrect return type information for closures `use` clauses containing invalid variable imports in the `use` clause (defensive coding).

Closure `use` statements can only import plain variables, not properties or other more complex variables.

As things were, when such "illegal" variables were imported in a closure `use`, the information for the return type could get mangled.
While this would be a parse error, for the purposes of static analysis, the `File::getMethodProperties()` method should still handle this correctly.

This commit updates the `File::getMethodProperties()` method to always skip over the complete `use` clause, which prevents the issue and improves performance as the same time (less token walking).

Includes unit tests.

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

4 existing lines in 3 files now uncovered.

17358 of 23991 relevant lines covered (72.35%)

55.3 hits per line

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

95.45
/src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php
1
<?php
2
/**
3
 * Bans the use of the PHP long array syntax.
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\Generic\Sniffs\Arrays;
11

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

15
class DisallowLongArraySyntaxSniff implements Sniff
16
{
17

18

19
    /**
20
     * Registers the tokens that this sniff wants to listen for.
21
     *
22
     * @return array<int|string>
23
     */
24
    public function register()
3✔
25
    {
26
        return [T_ARRAY];
3✔
27

28
    }//end register()
29

30

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

44
        $phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'no');
3✔
45

46
        $error = 'Short array syntax must be used to define arrays';
3✔
47

48
        if (isset($tokens[$stackPtr]['parenthesis_opener']) === false
3✔
49
            || isset($tokens[$stackPtr]['parenthesis_closer']) === false
3✔
50
        ) {
51
            // Live coding/parse error, just show the error, don't try and fix it.
52
            $phpcsFile->addError($error, $stackPtr, 'Found');
3✔
53
            return;
3✔
54
        }
55

56
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
3✔
57

58
        if ($fix === true) {
3✔
59
            $opener = $tokens[$stackPtr]['parenthesis_opener'];
3✔
60
            $closer = $tokens[$stackPtr]['parenthesis_closer'];
3✔
61

62
            $phpcsFile->fixer->beginChangeset();
3✔
63

64
            if ($opener === null) {
3✔
UNCOV
65
                $phpcsFile->fixer->replaceToken($stackPtr, '[]');
×
66
            } else {
67
                $phpcsFile->fixer->replaceToken($stackPtr, '');
3✔
68
                $phpcsFile->fixer->replaceToken($opener, '[');
3✔
69
                $phpcsFile->fixer->replaceToken($closer, ']');
3✔
70
            }
71

72
            $phpcsFile->fixer->endChangeset();
3✔
73
        }
74

75
    }//end process()
1✔
76

77

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