Completed
Push — php-7.3/new-function-call-trai... ( 2d3c51 )
by Juliette
01:53
created

NewTrailingCommaSniff::process()   C

Complexity

Conditions 9
Paths 11

Size

Total Lines 64
Code Lines 40

Duplication

Lines 5
Ratio 7.81 %

Importance

Changes 0
Metric Value
dl 5
loc 64
rs 6.5449
c 0
b 0
f 0
cc 9
eloc 40
nc 11
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\TrailingCommaSniff.
4
 *
5
 * PHP version 7.3
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Juliette Reinders Folmer <[email protected]>
10
 */
11
12
namespace PHPCompatibility\Sniffs\PHP;
13
14
use PHPCompatibility\Sniff;
15
16
/**
17
 * \PHPCompatibility\Sniffs\PHP\NewTrailingCommaSniff.
18
 *
19
 * PHP version 7.3
20
 *
21
 * Note: trailing comma's in group use statements as introduced in PHP 7.2 is covered
22
 * by the `NewGroupUseDeclaration` sniff.
23
 *
24
 * @category PHP
25
 * @package  PHPCompatibility
26
 * @author   Juliette Reinders Folmer <[email protected]>
27
 */
28
class NewTrailingCommaSniff extends Sniff
29
{
30
    /**
31
     * Returns an array of tokens this test wants to listen for.
32
     *
33
     * @return array
34
     */
35
    public function register()
36
    {
37
        return array(
38
            T_STRING,
39
            T_VARIABLE,
40
            T_ISSET,
41
            T_UNSET,
42
        );
43
    }
44
45
46
    /**
47
     * Processes this test, when one of its tokens is encountered.
48
     *
49
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
50
     * @param int                   $stackPtr  The position of the current token in
51
     *                                         the stack passed in $tokens.
52
     *
53
     * @return void
54
     */
55
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
56
    {
57
        if ($this->supportsBelow('7.2') === false) {
58
            return;
59
        }
60
61
        $tokens = $phpcsFile->getTokens();
62
63
        $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
64 View Code Duplication
        if ($tokens[$nextNonEmpty]['code'] !== T_OPEN_PARENTHESIS
65
            || isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false
66
        ) {
67
            return;
68
        }
69
70
        if ($tokens[$stackPtr]['code'] === T_STRING) {
71
            $ignore = array(
72
                T_FUNCTION        => true,
73
                T_CONST           => true,
74
                T_USE             => true,
75
            );
76
77
            $prevNonEmpty = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
78
            if (isset($ignore[$tokens[$prevNonEmpty]['code']]) === true) {
79
                // Not a function call.
80
                return;
81
            }
82
        }
83
84
        $closer            = $tokens[$nextNonEmpty]['parenthesis_closer'];
85
        $lastInParenthesis = $phpcsFile->findPrevious(
86
            \PHP_CodeSniffer_Tokens::$emptyTokens,
87
            ($closer - 1),
88
            $nextNonEmpty,
89
            true
90
        );
91
92
        if ($tokens[$lastInParenthesis]['code'] !== T_COMMA) {
93
            return;
94
        }
95
96
        $data = array();
97
        switch ($tokens[$stackPtr]['code']) {
98
            case T_ISSET:
99
                $data[] = 'calls to isset()';
100
                break;
101
102
            case T_UNSET:
103
                $data[] = 'calls to unset()';
104
                break;
105
106
            default:
107
                $data[] = 'function calls';
108
                break;
109
        }
110
111
        $phpcsFile->addError(
112
            'Trailing comma\'s are not allowed in %s in PHP 7.2 or earlier',
113
            $lastInParenthesis,
114
            'Found',
115
            $data
116
        );
117
118
    }//end process()
119
120
}//end class
121