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

NewTrailingCommaSniff   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 5.38 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 5
loc 93
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 9 1
C process() 5 64 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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