NewTrailingCommaSniff::process()   B
last analyzed

Complexity

Conditions 9
Paths 11

Size

Total Lines 67

Duplication

Lines 5
Ratio 7.46 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 5
loc 67
rs 7.1644
cc 9
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
                $errorCode = 'FoundInIsset';
101
                break;
102
103
            case T_UNSET:
104
                $data[]    = 'calls to unset()';
105
                $errorCode = 'FoundInUnset';
106
                break;
107
108
            default:
109
                $data[]    = 'function calls';
110
                $errorCode = 'FoundInFunctionCall';
111
                break;
112
        }
113
114
        $phpcsFile->addError(
115
            'Trailing comma\'s are not allowed in %s in PHP 7.2 or earlier',
116
            $lastInParenthesis,
117
            $errorCode,
118
            $data
119
        );
120
121
    }//end process()
122
123
}//end class
124