Completed
Push — php70 ( 83abde...b6aa8a )
by Wim
05:28 queued 02:49
created

ForbiddenParenthesisAroundFunctionParametersSniff   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 15.63 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 96
rs 10
wmc 12
lcom 0
cbo 3

2 Methods

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

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_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 View Code Duplication
                if ($tokens[$nextSeparator + 1]['code'] == T_OPEN_PARENTHESIS && $tokens[$nextComma - 1]['code'] == T_CLOSE_PARENTHESIS) {
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...
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
            $nextOpen = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $nextSeparator + 1, $closeBracket);
109 View Code Duplication
            if ($nextOpen !== false) {
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...
110
                if ($tokens[$nextOpen]['code'] == T_OPEN_PARENTHESIS && $tokens[$closeBracket - 1]['code'] == T_CLOSE_PARENTHESIS) {
111
                    $error = 'Parentheses around function parameters throws warning in PHP 7.0';
112
                    $phpcsFile->addWarning($error, $nextSeparator + 1);
113
                }
114
            }
115
        }
116
    }//end process()
117
118
}//end class
119