Completed
Pull Request — master (#110)
by Wim
03:06
created

process()   C

Complexity

Conditions 17
Paths 15

Size

Total Lines 66
Code Lines 35

Duplication

Lines 5
Ratio 7.58 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 17
eloc 35
c 2
b 1
f 0
nc 15
nop 2
dl 5
loc 66
rs 5.8371

How to fix   Long Method    Complexity   

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_ForbiddenParenthesisAroundFunctionParametersSniff.
4
 *
5
 * PHP version 7.0 
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 */
11
12
/**
13
 * PHPCompatibility_Sniffs_PHP_ForbiddenParenthesisAroundFunctionParametersSniff.
14
 *
15
 * Parentheses around function parameters throws warning in PHP 7.0
16
 *
17
 * PHP version 7.0
18
 *
19
 * @category  PHP
20
 * @package   PHPCompatibility
21
 * @author    Wim Godden <[email protected]>
22
 */
23
class PHPCompatibility_Sniffs_PHP_ForbiddenParenthesisAroundFunctionParametersSniff extends PHPCompatibility_Sniff
24
{
25
26
    /**
27
     * If true, an error will be thrown; otherwise a warning.
28
     *
29
     * @var bool
30
     */
31
    protected $error = true;
32
33
    /**
34
     * Returns an array of tokens this test wants to listen for.
35
     *
36
     * @return array
37
     */
38
    public function register()
39
    {
40
        return array(T_STRING);
41
42
    }//end register()
43
44
    /**
45
     * Processes this test, when one of its tokens is encountered.
46
     *
47
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
48
     * @param int                  $stackPtr  The position of the current token
49
     *                                        in the stack passed in $tokens.
50
     *
51
     * @return void
52
     */
53
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
54
    {
55
        if ($this->supportsAbove('7.0')) {
56
            $tokens = $phpcsFile->getTokens();
57
58
            // Skip tokens that are the names of functions or classes
59
            // within their definitions. For example: function myFunction...
60
            // "myFunction" is T_STRING but we should skip because it is not a
61
            // function or method *call*.
62
            $functionName = $stackPtr;
63
            $findTokens   = array_merge(
64
                PHP_CodeSniffer_Tokens::$emptyTokens,
65
                array(T_BITWISE_AND)
66
                );
67
            
68
            $functionKeyword = $phpcsFile->findPrevious(
69
                $findTokens,
70
                ($stackPtr - 1),
71
                null,
72
                true
73
                );
74
            
75 View Code Duplication
            if ($tokens[$functionKeyword]['code'] === T_FUNCTION
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
                || $tokens[$functionKeyword]['code'] === T_CLASS
77
                ) {
78
                    return;
79
            }
80
            
81
            // If the next non-whitespace token after the function or method call
82
            // is not an opening parenthesis then it cant really be a *call*.
83
            $openBracket = $phpcsFile->findNext(
84
                PHP_CodeSniffer_Tokens::$emptyTokens,
85
                ($functionName + 1),
86
                null,
87
                true
88
                );
89
90
            if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
91
                return;
92
            }
93
        
94
            $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
95
96
            $nextSeparator = $openBracket;
97
            while (($nextComma = $phpcsFile->findNext(T_COMMA, ($nextSeparator + 1), $closeBracket)) !== false) {
98
                // Find every argument along the way, check if there are parenthesis around them
99
100
                if ($tokens[$nextSeparator + 1]['type'] == 'T_OPEN_PARENTHESIS' && $tokens[$nextComma - 1]['type'] == 'T_CLOSE_PARENTHESIS' && $tokens[$nextSeparator + 3]['type'] != 'T_BITWISE_AND' && $tokens[$nextSeparator + 4]['type'] != 'T_BITWISE_AND' && $tokens[$nextSeparator + 5]['type'] != 'T_BITWISE_AND') {
101
                    $error = 'Parentheses around function parameters throws warning in PHP 7.0';
102
                    $phpcsFile->addWarning($error, $nextSeparator + 1);
103
                }
104
                
105
                $nextSeparator = $nextComma;
106
            }//end while
107
            
108
            if ($tokens[$nextSeparator + 1]['type'] == 'T_WHITESPACE') {
109
                $nextSeparator++;
110
            }
111
            
112
            $nextOpen = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $nextSeparator + 1, $nextSeparator + 3);
113
            if ($nextOpen !== false && $nextOpen <= $nextSeparator + 1 && $tokens[$nextSeparator + 3]['type'] != 'T_BITWISE_AND' && $tokens[$nextSeparator + 4]['type'] != 'T_BITWISE_AND' && $tokens[$nextSeparator + 5]['type'] != 'T_BITWISE_AND') {
114
                $error = 'Parentheses around function parameters throws warning in PHP 7.0';
115
                $phpcsFile->addWarning($error, $nextSeparator + 1);
116
            }
117
        }
118
    }//end process()
119
120
}//end class
121