@@ -29,76 +29,76 @@ |
||
29 | 29 | */ |
30 | 30 | class ForbiddenNegativeBitshiftSniff extends Sniff |
31 | 31 | { |
32 | - /** |
|
33 | - * Potential end tokens for which the end pointer has to be set back by one. |
|
34 | - * |
|
35 | - * {@internal The PHPCS `findEndOfStatement()` method is not completely consistent |
|
36 | - * in how it returns the statement end. This is just a simple way to bypass |
|
37 | - * the inconsistency for our purposes.}} |
|
38 | - * |
|
39 | - * @var array |
|
40 | - */ |
|
41 | - private $inclusiveStopPoints = array( |
|
42 | - \T_COLON => true, |
|
43 | - \T_COMMA => true, |
|
44 | - \T_DOUBLE_ARROW => true, |
|
45 | - \T_SEMICOLON => true, |
|
46 | - ); |
|
32 | + /** |
|
33 | + * Potential end tokens for which the end pointer has to be set back by one. |
|
34 | + * |
|
35 | + * {@internal The PHPCS `findEndOfStatement()` method is not completely consistent |
|
36 | + * in how it returns the statement end. This is just a simple way to bypass |
|
37 | + * the inconsistency for our purposes.}} |
|
38 | + * |
|
39 | + * @var array |
|
40 | + */ |
|
41 | + private $inclusiveStopPoints = array( |
|
42 | + \T_COLON => true, |
|
43 | + \T_COMMA => true, |
|
44 | + \T_DOUBLE_ARROW => true, |
|
45 | + \T_SEMICOLON => true, |
|
46 | + ); |
|
47 | 47 | |
48 | - /** |
|
49 | - * Returns an array of tokens this test wants to listen for. |
|
50 | - * |
|
51 | - * @return array |
|
52 | - */ |
|
53 | - public function register() |
|
54 | - { |
|
55 | - return array( |
|
56 | - \T_SL, |
|
57 | - \T_SL_EQUAL, |
|
58 | - \T_SR, |
|
59 | - \T_SR_EQUAL, |
|
60 | - ); |
|
61 | - } |
|
48 | + /** |
|
49 | + * Returns an array of tokens this test wants to listen for. |
|
50 | + * |
|
51 | + * @return array |
|
52 | + */ |
|
53 | + public function register() |
|
54 | + { |
|
55 | + return array( |
|
56 | + \T_SL, |
|
57 | + \T_SL_EQUAL, |
|
58 | + \T_SR, |
|
59 | + \T_SR_EQUAL, |
|
60 | + ); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Processes this test, when one of its tokens is encountered. |
|
65 | - * |
|
66 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
67 | - * @param int $stackPtr The position of the current token |
|
68 | - * in the stack passed in $tokens. |
|
69 | - * |
|
70 | - * @return void |
|
71 | - */ |
|
72 | - public function process(File $phpcsFile, $stackPtr) |
|
73 | - { |
|
74 | - if ($this->supportsAbove('7.0') === false) { |
|
75 | - return; |
|
76 | - } |
|
63 | + /** |
|
64 | + * Processes this test, when one of its tokens is encountered. |
|
65 | + * |
|
66 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
67 | + * @param int $stackPtr The position of the current token |
|
68 | + * in the stack passed in $tokens. |
|
69 | + * |
|
70 | + * @return void |
|
71 | + */ |
|
72 | + public function process(File $phpcsFile, $stackPtr) |
|
73 | + { |
|
74 | + if ($this->supportsAbove('7.0') === false) { |
|
75 | + return; |
|
76 | + } |
|
77 | 77 | |
78 | - $tokens = $phpcsFile->getTokens(); |
|
78 | + $tokens = $phpcsFile->getTokens(); |
|
79 | 79 | |
80 | - // Determine the start and end of the part of the statement we need to examine. |
|
81 | - $start = ($stackPtr + 1); |
|
82 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, $start, null, true); |
|
83 | - if ($next !== false && $tokens[$next]['code'] === \T_OPEN_PARENTHESIS) { |
|
84 | - $start = ($next + 1); |
|
85 | - } |
|
80 | + // Determine the start and end of the part of the statement we need to examine. |
|
81 | + $start = ($stackPtr + 1); |
|
82 | + $next = $phpcsFile->findNext(Tokens::$emptyTokens, $start, null, true); |
|
83 | + if ($next !== false && $tokens[$next]['code'] === \T_OPEN_PARENTHESIS) { |
|
84 | + $start = ($next + 1); |
|
85 | + } |
|
86 | 86 | |
87 | - $end = PHPCSHelper::findEndOfStatement($phpcsFile, $start); |
|
88 | - if (isset($this->inclusiveStopPoints[$tokens[$end]['code']]) === true) { |
|
89 | - --$end; |
|
90 | - } |
|
87 | + $end = PHPCSHelper::findEndOfStatement($phpcsFile, $start); |
|
88 | + if (isset($this->inclusiveStopPoints[$tokens[$end]['code']]) === true) { |
|
89 | + --$end; |
|
90 | + } |
|
91 | 91 | |
92 | - if ($this->isNegativeNumber($phpcsFile, $start, $end, true) !== true) { |
|
93 | - // Not a negative number or undetermined. |
|
94 | - return; |
|
95 | - } |
|
92 | + if ($this->isNegativeNumber($phpcsFile, $start, $end, true) !== true) { |
|
93 | + // Not a negative number or undetermined. |
|
94 | + return; |
|
95 | + } |
|
96 | 96 | |
97 | - $phpcsFile->addError( |
|
98 | - 'Bitwise shifts by negative number will throw an ArithmeticError in PHP 7.0. Found: %s', |
|
99 | - $stackPtr, |
|
100 | - 'Found', |
|
101 | - array($phpcsFile->getTokensAsString($start, ($end - $start + 1))) |
|
102 | - ); |
|
103 | - } |
|
97 | + $phpcsFile->addError( |
|
98 | + 'Bitwise shifts by negative number will throw an ArithmeticError in PHP 7.0. Found: %s', |
|
99 | + $stackPtr, |
|
100 | + 'Found', |
|
101 | + array($phpcsFile->getTokensAsString($start, ($end - $start + 1))) |
|
102 | + ); |
|
103 | + } |
|
104 | 104 | } |
@@ -69,27 +69,27 @@ discard block |
||
69 | 69 | * |
70 | 70 | * @return void |
71 | 71 | */ |
72 | - public function process(File $phpcsFile, $stackPtr) |
|
72 | + public function process( File $phpcsFile, $stackPtr ) |
|
73 | 73 | { |
74 | - if ($this->supportsAbove('7.0') === false) { |
|
74 | + if ( $this->supportsAbove( '7.0' ) === false ) { |
|
75 | 75 | return; |
76 | 76 | } |
77 | 77 | |
78 | 78 | $tokens = $phpcsFile->getTokens(); |
79 | 79 | |
80 | 80 | // Determine the start and end of the part of the statement we need to examine. |
81 | - $start = ($stackPtr + 1); |
|
82 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, $start, null, true); |
|
83 | - if ($next !== false && $tokens[$next]['code'] === \T_OPEN_PARENTHESIS) { |
|
84 | - $start = ($next + 1); |
|
81 | + $start = ( $stackPtr + 1 ); |
|
82 | + $next = $phpcsFile->findNext( Tokens::$emptyTokens, $start, null, true ); |
|
83 | + if ( $next !== false && $tokens[ $next ][ 'code' ] === \T_OPEN_PARENTHESIS ) { |
|
84 | + $start = ( $next + 1 ); |
|
85 | 85 | } |
86 | 86 | |
87 | - $end = PHPCSHelper::findEndOfStatement($phpcsFile, $start); |
|
88 | - if (isset($this->inclusiveStopPoints[$tokens[$end]['code']]) === true) { |
|
87 | + $end = PHPCSHelper::findEndOfStatement( $phpcsFile, $start ); |
|
88 | + if ( isset( $this->inclusiveStopPoints[ $tokens[ $end ][ 'code' ] ] ) === true ) { |
|
89 | 89 | --$end; |
90 | 90 | } |
91 | 91 | |
92 | - if ($this->isNegativeNumber($phpcsFile, $start, $end, true) !== true) { |
|
92 | + if ( $this->isNegativeNumber( $phpcsFile, $start, $end, true ) !== true ) { |
|
93 | 93 | // Not a negative number or undetermined. |
94 | 94 | return; |
95 | 95 | } |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | 'Bitwise shifts by negative number will throw an ArithmeticError in PHP 7.0. Found: %s', |
99 | 99 | $stackPtr, |
100 | 100 | 'Found', |
101 | - array($phpcsFile->getTokensAsString($start, ($end - $start + 1))) |
|
101 | + array( $phpcsFile->getTokensAsString( $start, ( $end - $start + 1 ) ) ) |
|
102 | 102 | ); |
103 | 103 | } |
104 | 104 | } |
@@ -27,8 +27,7 @@ discard block |
||
27 | 27 | * @package PHPCompatibility |
28 | 28 | * @author Wim Godden <[email protected]> |
29 | 29 | */ |
30 | -class ForbiddenNegativeBitshiftSniff extends Sniff |
|
31 | -{ |
|
30 | +class ForbiddenNegativeBitshiftSniff extends Sniff { |
|
32 | 31 | /** |
33 | 32 | * Potential end tokens for which the end pointer has to be set back by one. |
34 | 33 | * |
@@ -50,8 +49,7 @@ discard block |
||
50 | 49 | * |
51 | 50 | * @return array |
52 | 51 | */ |
53 | - public function register() |
|
54 | - { |
|
52 | + public function register() { |
|
55 | 53 | return array( |
56 | 54 | \T_SL, |
57 | 55 | \T_SL_EQUAL, |
@@ -69,8 +67,7 @@ discard block |
||
69 | 67 | * |
70 | 68 | * @return void |
71 | 69 | */ |
72 | - public function process(File $phpcsFile, $stackPtr) |
|
73 | - { |
|
70 | + public function process(File $phpcsFile, $stackPtr) { |
|
74 | 71 | if ($this->supportsAbove('7.0') === false) { |
75 | 72 | return; |
76 | 73 | } |
@@ -65,7 +65,7 @@ |
||
65 | 65 | /** |
66 | 66 | * Returns an array of tokens this test wants to listen for. |
67 | 67 | * |
68 | - * @return array |
|
68 | + * @return integer[] |
|
69 | 69 | */ |
70 | 70 | public function register() |
71 | 71 | { |
@@ -30,127 +30,127 @@ |
||
30 | 30 | class RemovedTernaryAssociativitySniff extends Sniff |
31 | 31 | { |
32 | 32 | |
33 | - /** |
|
34 | - * List of tokens with a lower operator precedence than ternary. |
|
35 | - * |
|
36 | - * @since 9.2.0 |
|
37 | - * |
|
38 | - * @var array |
|
39 | - */ |
|
40 | - private $tokensWithLowerPrecedence = array( |
|
41 | - 'T_YIELD_FROM' => true, |
|
42 | - 'T_YIELD' => true, |
|
43 | - 'T_LOGICAL_AND' => true, |
|
44 | - 'T_LOGICAL_OR' => true, |
|
45 | - 'T_LOGICAL_XOR' => true, |
|
46 | - ); |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * Returns an array of tokens this test wants to listen for. |
|
51 | - * |
|
52 | - * @since 9.2.0 |
|
53 | - * |
|
54 | - * @return array |
|
55 | - */ |
|
56 | - public function register() |
|
57 | - { |
|
58 | - return array(\T_INLINE_THEN); |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Processes this test, when one of its tokens is encountered. |
|
63 | - * |
|
64 | - * @since 9.2.0 |
|
65 | - * |
|
66 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
67 | - * @param int $stackPtr The position of the current token |
|
68 | - * in the stack passed in $tokens. |
|
69 | - * |
|
70 | - * @return void |
|
71 | - */ |
|
72 | - public function process(File $phpcsFile, $stackPtr) |
|
73 | - { |
|
74 | - if ($this->supportsAbove('7.4') === false) { |
|
75 | - return; |
|
76 | - } |
|
77 | - |
|
78 | - $tokens = $phpcsFile->getTokens(); |
|
79 | - $endOfStatement = PHPCSHelper::findEndOfStatement($phpcsFile, $stackPtr); |
|
80 | - if ($tokens[$endOfStatement]['code'] !== \T_SEMICOLON |
|
81 | - && $tokens[$endOfStatement]['code'] !== \T_COLON |
|
82 | - && $tokens[$endOfStatement]['code'] !== \T_COMMA |
|
83 | - && $tokens[$endOfStatement]['code'] !== \T_DOUBLE_ARROW |
|
84 | - && $tokens[$endOfStatement]['code'] !== \T_OPEN_TAG |
|
85 | - && $tokens[$endOfStatement]['code'] !== \T_CLOSE_TAG |
|
86 | - ) { |
|
87 | - // End of statement is last non-empty before close brace, so make sure we examine that token too. |
|
88 | - ++$endOfStatement; |
|
89 | - } |
|
90 | - |
|
91 | - $ternaryCount = 0; |
|
92 | - $elseCount = 0; |
|
93 | - $shortTernaryCount = 0; |
|
94 | - |
|
95 | - // Start at $stackPtr so we don't need duplicate code for short ternary determination. |
|
96 | - for ($i = $stackPtr; $i < $endOfStatement; $i++) { |
|
97 | - if (($tokens[$i]['code'] === \T_OPEN_SHORT_ARRAY |
|
98 | - || $tokens[$i]['code'] === \T_OPEN_SQUARE_BRACKET |
|
99 | - || $tokens[$i]['code'] === \T_OPEN_CURLY_BRACKET) |
|
100 | - && isset($tokens[$i]['bracket_closer']) |
|
101 | - ) { |
|
102 | - // Skip over short arrays, array access keys and curlies. |
|
103 | - $i = $tokens[$i]['bracket_closer']; |
|
104 | - continue; |
|
105 | - } |
|
106 | - |
|
107 | - if ($tokens[$i]['code'] === \T_OPEN_PARENTHESIS |
|
108 | - && isset($tokens[$i]['parenthesis_closer']) |
|
109 | - ) { |
|
110 | - // Skip over anything between parentheses. |
|
111 | - $i = $tokens[$i]['parenthesis_closer']; |
|
112 | - continue; |
|
113 | - } |
|
114 | - |
|
115 | - // Check for operators with lower operator precedence. |
|
116 | - if (isset(Tokens::$assignmentTokens[$tokens[$i]['code']]) |
|
117 | - || isset($this->tokensWithLowerPrecedence[$tokens[$i]['code']]) |
|
118 | - ) { |
|
119 | - break; |
|
120 | - } |
|
121 | - |
|
122 | - if ($tokens[$i]['code'] === \T_INLINE_THEN) { |
|
123 | - ++$ternaryCount; |
|
124 | - |
|
125 | - if ($this->isShortTernary($phpcsFile, $i) === true) { |
|
126 | - ++$shortTernaryCount; |
|
127 | - } |
|
128 | - |
|
129 | - continue; |
|
130 | - } |
|
131 | - |
|
132 | - if ($tokens[$i]['code'] === \T_INLINE_ELSE) { |
|
133 | - if (($ternaryCount - $elseCount) >= 2) { |
|
134 | - // This is the `else` for a ternary in the middle part of a previous ternary. |
|
135 | - --$ternaryCount; |
|
136 | - } else { |
|
137 | - ++$elseCount; |
|
138 | - } |
|
139 | - continue; |
|
140 | - } |
|
141 | - } |
|
142 | - |
|
143 | - if ($ternaryCount > 1 && $ternaryCount === $elseCount && $ternaryCount > $shortTernaryCount) { |
|
144 | - $message = 'The left-associativity of the ternary operator has been deprecated in PHP 7.4'; |
|
145 | - $isError = false; |
|
146 | - if ($this->supportsAbove('8.0') === true) { |
|
147 | - $message .= ' and removed in PHP 8.0'; |
|
148 | - $isError = true; |
|
149 | - } |
|
150 | - |
|
151 | - $message .= '. Multiple consecutive ternaries detected. Use parenthesis to clarify the order in which the operations should be executed'; |
|
152 | - |
|
153 | - $this->addMessage($phpcsFile, $message, $stackPtr, $isError); |
|
154 | - } |
|
155 | - } |
|
33 | + /** |
|
34 | + * List of tokens with a lower operator precedence than ternary. |
|
35 | + * |
|
36 | + * @since 9.2.0 |
|
37 | + * |
|
38 | + * @var array |
|
39 | + */ |
|
40 | + private $tokensWithLowerPrecedence = array( |
|
41 | + 'T_YIELD_FROM' => true, |
|
42 | + 'T_YIELD' => true, |
|
43 | + 'T_LOGICAL_AND' => true, |
|
44 | + 'T_LOGICAL_OR' => true, |
|
45 | + 'T_LOGICAL_XOR' => true, |
|
46 | + ); |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * Returns an array of tokens this test wants to listen for. |
|
51 | + * |
|
52 | + * @since 9.2.0 |
|
53 | + * |
|
54 | + * @return array |
|
55 | + */ |
|
56 | + public function register() |
|
57 | + { |
|
58 | + return array(\T_INLINE_THEN); |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Processes this test, when one of its tokens is encountered. |
|
63 | + * |
|
64 | + * @since 9.2.0 |
|
65 | + * |
|
66 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
67 | + * @param int $stackPtr The position of the current token |
|
68 | + * in the stack passed in $tokens. |
|
69 | + * |
|
70 | + * @return void |
|
71 | + */ |
|
72 | + public function process(File $phpcsFile, $stackPtr) |
|
73 | + { |
|
74 | + if ($this->supportsAbove('7.4') === false) { |
|
75 | + return; |
|
76 | + } |
|
77 | + |
|
78 | + $tokens = $phpcsFile->getTokens(); |
|
79 | + $endOfStatement = PHPCSHelper::findEndOfStatement($phpcsFile, $stackPtr); |
|
80 | + if ($tokens[$endOfStatement]['code'] !== \T_SEMICOLON |
|
81 | + && $tokens[$endOfStatement]['code'] !== \T_COLON |
|
82 | + && $tokens[$endOfStatement]['code'] !== \T_COMMA |
|
83 | + && $tokens[$endOfStatement]['code'] !== \T_DOUBLE_ARROW |
|
84 | + && $tokens[$endOfStatement]['code'] !== \T_OPEN_TAG |
|
85 | + && $tokens[$endOfStatement]['code'] !== \T_CLOSE_TAG |
|
86 | + ) { |
|
87 | + // End of statement is last non-empty before close brace, so make sure we examine that token too. |
|
88 | + ++$endOfStatement; |
|
89 | + } |
|
90 | + |
|
91 | + $ternaryCount = 0; |
|
92 | + $elseCount = 0; |
|
93 | + $shortTernaryCount = 0; |
|
94 | + |
|
95 | + // Start at $stackPtr so we don't need duplicate code for short ternary determination. |
|
96 | + for ($i = $stackPtr; $i < $endOfStatement; $i++) { |
|
97 | + if (($tokens[$i]['code'] === \T_OPEN_SHORT_ARRAY |
|
98 | + || $tokens[$i]['code'] === \T_OPEN_SQUARE_BRACKET |
|
99 | + || $tokens[$i]['code'] === \T_OPEN_CURLY_BRACKET) |
|
100 | + && isset($tokens[$i]['bracket_closer']) |
|
101 | + ) { |
|
102 | + // Skip over short arrays, array access keys and curlies. |
|
103 | + $i = $tokens[$i]['bracket_closer']; |
|
104 | + continue; |
|
105 | + } |
|
106 | + |
|
107 | + if ($tokens[$i]['code'] === \T_OPEN_PARENTHESIS |
|
108 | + && isset($tokens[$i]['parenthesis_closer']) |
|
109 | + ) { |
|
110 | + // Skip over anything between parentheses. |
|
111 | + $i = $tokens[$i]['parenthesis_closer']; |
|
112 | + continue; |
|
113 | + } |
|
114 | + |
|
115 | + // Check for operators with lower operator precedence. |
|
116 | + if (isset(Tokens::$assignmentTokens[$tokens[$i]['code']]) |
|
117 | + || isset($this->tokensWithLowerPrecedence[$tokens[$i]['code']]) |
|
118 | + ) { |
|
119 | + break; |
|
120 | + } |
|
121 | + |
|
122 | + if ($tokens[$i]['code'] === \T_INLINE_THEN) { |
|
123 | + ++$ternaryCount; |
|
124 | + |
|
125 | + if ($this->isShortTernary($phpcsFile, $i) === true) { |
|
126 | + ++$shortTernaryCount; |
|
127 | + } |
|
128 | + |
|
129 | + continue; |
|
130 | + } |
|
131 | + |
|
132 | + if ($tokens[$i]['code'] === \T_INLINE_ELSE) { |
|
133 | + if (($ternaryCount - $elseCount) >= 2) { |
|
134 | + // This is the `else` for a ternary in the middle part of a previous ternary. |
|
135 | + --$ternaryCount; |
|
136 | + } else { |
|
137 | + ++$elseCount; |
|
138 | + } |
|
139 | + continue; |
|
140 | + } |
|
141 | + } |
|
142 | + |
|
143 | + if ($ternaryCount > 1 && $ternaryCount === $elseCount && $ternaryCount > $shortTernaryCount) { |
|
144 | + $message = 'The left-associativity of the ternary operator has been deprecated in PHP 7.4'; |
|
145 | + $isError = false; |
|
146 | + if ($this->supportsAbove('8.0') === true) { |
|
147 | + $message .= ' and removed in PHP 8.0'; |
|
148 | + $isError = true; |
|
149 | + } |
|
150 | + |
|
151 | + $message .= '. Multiple consecutive ternaries detected. Use parenthesis to clarify the order in which the operations should be executed'; |
|
152 | + |
|
153 | + $this->addMessage($phpcsFile, $message, $stackPtr, $isError); |
|
154 | + } |
|
155 | + } |
|
156 | 156 | } |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | */ |
56 | 56 | public function register() |
57 | 57 | { |
58 | - return array(\T_INLINE_THEN); |
|
58 | + return array( \T_INLINE_THEN ); |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | /** |
@@ -69,20 +69,20 @@ discard block |
||
69 | 69 | * |
70 | 70 | * @return void |
71 | 71 | */ |
72 | - public function process(File $phpcsFile, $stackPtr) |
|
72 | + public function process( File $phpcsFile, $stackPtr ) |
|
73 | 73 | { |
74 | - if ($this->supportsAbove('7.4') === false) { |
|
74 | + if ( $this->supportsAbove( '7.4' ) === false ) { |
|
75 | 75 | return; |
76 | 76 | } |
77 | 77 | |
78 | 78 | $tokens = $phpcsFile->getTokens(); |
79 | - $endOfStatement = PHPCSHelper::findEndOfStatement($phpcsFile, $stackPtr); |
|
80 | - if ($tokens[$endOfStatement]['code'] !== \T_SEMICOLON |
|
81 | - && $tokens[$endOfStatement]['code'] !== \T_COLON |
|
82 | - && $tokens[$endOfStatement]['code'] !== \T_COMMA |
|
83 | - && $tokens[$endOfStatement]['code'] !== \T_DOUBLE_ARROW |
|
84 | - && $tokens[$endOfStatement]['code'] !== \T_OPEN_TAG |
|
85 | - && $tokens[$endOfStatement]['code'] !== \T_CLOSE_TAG |
|
79 | + $endOfStatement = PHPCSHelper::findEndOfStatement( $phpcsFile, $stackPtr ); |
|
80 | + if ( $tokens[ $endOfStatement ][ 'code' ] !== \T_SEMICOLON |
|
81 | + && $tokens[ $endOfStatement ][ 'code' ] !== \T_COLON |
|
82 | + && $tokens[ $endOfStatement ][ 'code' ] !== \T_COMMA |
|
83 | + && $tokens[ $endOfStatement ][ 'code' ] !== \T_DOUBLE_ARROW |
|
84 | + && $tokens[ $endOfStatement ][ 'code' ] !== \T_OPEN_TAG |
|
85 | + && $tokens[ $endOfStatement ][ 'code' ] !== \T_CLOSE_TAG |
|
86 | 86 | ) { |
87 | 87 | // End of statement is last non-empty before close brace, so make sure we examine that token too. |
88 | 88 | ++$endOfStatement; |
@@ -93,44 +93,44 @@ discard block |
||
93 | 93 | $shortTernaryCount = 0; |
94 | 94 | |
95 | 95 | // Start at $stackPtr so we don't need duplicate code for short ternary determination. |
96 | - for ($i = $stackPtr; $i < $endOfStatement; $i++) { |
|
97 | - if (($tokens[$i]['code'] === \T_OPEN_SHORT_ARRAY |
|
98 | - || $tokens[$i]['code'] === \T_OPEN_SQUARE_BRACKET |
|
99 | - || $tokens[$i]['code'] === \T_OPEN_CURLY_BRACKET) |
|
100 | - && isset($tokens[$i]['bracket_closer']) |
|
96 | + for ( $i = $stackPtr; $i < $endOfStatement; $i++ ) { |
|
97 | + if ( ( $tokens[ $i ][ 'code' ] === \T_OPEN_SHORT_ARRAY |
|
98 | + || $tokens[ $i ][ 'code' ] === \T_OPEN_SQUARE_BRACKET |
|
99 | + || $tokens[ $i ][ 'code' ] === \T_OPEN_CURLY_BRACKET ) |
|
100 | + && isset( $tokens[ $i ][ 'bracket_closer' ] ) |
|
101 | 101 | ) { |
102 | 102 | // Skip over short arrays, array access keys and curlies. |
103 | - $i = $tokens[$i]['bracket_closer']; |
|
103 | + $i = $tokens[ $i ][ 'bracket_closer' ]; |
|
104 | 104 | continue; |
105 | 105 | } |
106 | 106 | |
107 | - if ($tokens[$i]['code'] === \T_OPEN_PARENTHESIS |
|
108 | - && isset($tokens[$i]['parenthesis_closer']) |
|
107 | + if ( $tokens[ $i ][ 'code' ] === \T_OPEN_PARENTHESIS |
|
108 | + && isset( $tokens[ $i ][ 'parenthesis_closer' ] ) |
|
109 | 109 | ) { |
110 | 110 | // Skip over anything between parentheses. |
111 | - $i = $tokens[$i]['parenthesis_closer']; |
|
111 | + $i = $tokens[ $i ][ 'parenthesis_closer' ]; |
|
112 | 112 | continue; |
113 | 113 | } |
114 | 114 | |
115 | 115 | // Check for operators with lower operator precedence. |
116 | - if (isset(Tokens::$assignmentTokens[$tokens[$i]['code']]) |
|
117 | - || isset($this->tokensWithLowerPrecedence[$tokens[$i]['code']]) |
|
116 | + if ( isset( Tokens::$assignmentTokens[ $tokens[ $i ][ 'code' ] ] ) |
|
117 | + || isset( $this->tokensWithLowerPrecedence[ $tokens[ $i ][ 'code' ] ] ) |
|
118 | 118 | ) { |
119 | 119 | break; |
120 | 120 | } |
121 | 121 | |
122 | - if ($tokens[$i]['code'] === \T_INLINE_THEN) { |
|
122 | + if ( $tokens[ $i ][ 'code' ] === \T_INLINE_THEN ) { |
|
123 | 123 | ++$ternaryCount; |
124 | 124 | |
125 | - if ($this->isShortTernary($phpcsFile, $i) === true) { |
|
125 | + if ( $this->isShortTernary( $phpcsFile, $i ) === true ) { |
|
126 | 126 | ++$shortTernaryCount; |
127 | 127 | } |
128 | 128 | |
129 | 129 | continue; |
130 | 130 | } |
131 | 131 | |
132 | - if ($tokens[$i]['code'] === \T_INLINE_ELSE) { |
|
133 | - if (($ternaryCount - $elseCount) >= 2) { |
|
132 | + if ( $tokens[ $i ][ 'code' ] === \T_INLINE_ELSE ) { |
|
133 | + if ( ( $ternaryCount - $elseCount ) >= 2 ) { |
|
134 | 134 | // This is the `else` for a ternary in the middle part of a previous ternary. |
135 | 135 | --$ternaryCount; |
136 | 136 | } else { |
@@ -140,17 +140,17 @@ discard block |
||
140 | 140 | } |
141 | 141 | } |
142 | 142 | |
143 | - if ($ternaryCount > 1 && $ternaryCount === $elseCount && $ternaryCount > $shortTernaryCount) { |
|
143 | + if ( $ternaryCount > 1 && $ternaryCount === $elseCount && $ternaryCount > $shortTernaryCount ) { |
|
144 | 144 | $message = 'The left-associativity of the ternary operator has been deprecated in PHP 7.4'; |
145 | 145 | $isError = false; |
146 | - if ($this->supportsAbove('8.0') === true) { |
|
146 | + if ( $this->supportsAbove( '8.0' ) === true ) { |
|
147 | 147 | $message .= ' and removed in PHP 8.0'; |
148 | 148 | $isError = true; |
149 | 149 | } |
150 | 150 | |
151 | 151 | $message .= '. Multiple consecutive ternaries detected. Use parenthesis to clarify the order in which the operations should be executed'; |
152 | 152 | |
153 | - $this->addMessage($phpcsFile, $message, $stackPtr, $isError); |
|
153 | + $this->addMessage( $phpcsFile, $message, $stackPtr, $isError ); |
|
154 | 154 | } |
155 | 155 | } |
156 | 156 | } |
@@ -27,8 +27,7 @@ discard block |
||
27 | 27 | * |
28 | 28 | * @since 9.2.0 |
29 | 29 | */ |
30 | -class RemovedTernaryAssociativitySniff extends Sniff |
|
31 | -{ |
|
30 | +class RemovedTernaryAssociativitySniff extends Sniff { |
|
32 | 31 | |
33 | 32 | /** |
34 | 33 | * List of tokens with a lower operator precedence than ternary. |
@@ -53,8 +52,7 @@ discard block |
||
53 | 52 | * |
54 | 53 | * @return array |
55 | 54 | */ |
56 | - public function register() |
|
57 | - { |
|
55 | + public function register() { |
|
58 | 56 | return array(\T_INLINE_THEN); |
59 | 57 | } |
60 | 58 | |
@@ -69,8 +67,7 @@ discard block |
||
69 | 67 | * |
70 | 68 | * @return void |
71 | 69 | */ |
72 | - public function process(File $phpcsFile, $stackPtr) |
|
73 | - { |
|
70 | + public function process(File $phpcsFile, $stackPtr) { |
|
74 | 71 | if ($this->supportsAbove('7.4') === false) { |
75 | 72 | return; |
76 | 73 | } |
@@ -45,7 +45,7 @@ |
||
45 | 45 | * |
46 | 46 | * @since 9.2.0 |
47 | 47 | * |
48 | - * @return array |
|
48 | + * @return string[] |
|
49 | 49 | */ |
50 | 50 | public function register() |
51 | 51 | { |
@@ -24,271 +24,271 @@ |
||
24 | 24 | class NewOperatorsSniff extends AbstractNewFeatureSniff |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * A list of new operators, not present in older versions. |
|
29 | - * |
|
30 | - * The array lists : version number with false (not present) or true (present). |
|
31 | - * If's sufficient to list the first version where the keyword appears. |
|
32 | - * |
|
33 | - * @var array(string => array(string => int|string|null)) |
|
34 | - */ |
|
35 | - protected $newOperators = array( |
|
36 | - 'T_POW' => array( |
|
37 | - '5.5' => false, |
|
38 | - '5.6' => true, |
|
39 | - 'description' => 'power operator (**)', |
|
40 | - ), // Identified in PHP < 5.6 icw PHPCS < 2.4.0 as T_MULTIPLY + T_MULTIPLY. |
|
41 | - 'T_POW_EQUAL' => array( |
|
42 | - '5.5' => false, |
|
43 | - '5.6' => true, |
|
44 | - 'description' => 'power assignment operator (**=)', |
|
45 | - ), // Identified in PHP < 5.6 icw PHPCS < 2.6.0 as T_MULTIPLY + T_MUL_EQUAL. |
|
46 | - 'T_SPACESHIP' => array( |
|
47 | - '5.6' => false, |
|
48 | - '7.0' => true, |
|
49 | - 'description' => 'spaceship operator (<=>)', |
|
50 | - ), // Identified in PHP < 7.0 icw PHPCS < 2.5.1 as T_IS_SMALLER_OR_EQUAL + T_GREATER_THAN. |
|
51 | - 'T_COALESCE' => array( |
|
52 | - '5.6' => false, |
|
53 | - '7.0' => true, |
|
54 | - 'description' => 'null coalescing operator (??)', |
|
55 | - ), // Identified in PHP < 7.0 icw PHPCS < 2.6.2 as T_INLINE_THEN + T_INLINE_THEN. |
|
56 | - /* |
|
27 | + /** |
|
28 | + * A list of new operators, not present in older versions. |
|
29 | + * |
|
30 | + * The array lists : version number with false (not present) or true (present). |
|
31 | + * If's sufficient to list the first version where the keyword appears. |
|
32 | + * |
|
33 | + * @var array(string => array(string => int|string|null)) |
|
34 | + */ |
|
35 | + protected $newOperators = array( |
|
36 | + 'T_POW' => array( |
|
37 | + '5.5' => false, |
|
38 | + '5.6' => true, |
|
39 | + 'description' => 'power operator (**)', |
|
40 | + ), // Identified in PHP < 5.6 icw PHPCS < 2.4.0 as T_MULTIPLY + T_MULTIPLY. |
|
41 | + 'T_POW_EQUAL' => array( |
|
42 | + '5.5' => false, |
|
43 | + '5.6' => true, |
|
44 | + 'description' => 'power assignment operator (**=)', |
|
45 | + ), // Identified in PHP < 5.6 icw PHPCS < 2.6.0 as T_MULTIPLY + T_MUL_EQUAL. |
|
46 | + 'T_SPACESHIP' => array( |
|
47 | + '5.6' => false, |
|
48 | + '7.0' => true, |
|
49 | + 'description' => 'spaceship operator (<=>)', |
|
50 | + ), // Identified in PHP < 7.0 icw PHPCS < 2.5.1 as T_IS_SMALLER_OR_EQUAL + T_GREATER_THAN. |
|
51 | + 'T_COALESCE' => array( |
|
52 | + '5.6' => false, |
|
53 | + '7.0' => true, |
|
54 | + 'description' => 'null coalescing operator (??)', |
|
55 | + ), // Identified in PHP < 7.0 icw PHPCS < 2.6.2 as T_INLINE_THEN + T_INLINE_THEN. |
|
56 | + /* |
|
57 | 57 | * Was slated for 7.2, but still not implemented. PHPCS however does already tokenize it. |
58 | 58 | * @link https://wiki.php.net/rfc/null_coalesce_equal_operator |
59 | 59 | */ |
60 | - 'T_COALESCE_EQUAL' => array( |
|
61 | - '7.3' => false, |
|
62 | - '7.4' => true, |
|
63 | - 'description' => 'null coalesce equal operator (??=)', |
|
64 | - ), // Identified in PHP < 7.0 icw PHPCS < 2.6.2 as T_INLINE_THEN + T_INLINE_THEN + T_EQUAL and between PHPCS 2.6.2 and PHPCS 2.8.1 as T_COALESCE + T_EQUAL. |
|
65 | - ); |
|
60 | + 'T_COALESCE_EQUAL' => array( |
|
61 | + '7.3' => false, |
|
62 | + '7.4' => true, |
|
63 | + 'description' => 'null coalesce equal operator (??=)', |
|
64 | + ), // Identified in PHP < 7.0 icw PHPCS < 2.6.2 as T_INLINE_THEN + T_INLINE_THEN + T_EQUAL and between PHPCS 2.6.2 and PHPCS 2.8.1 as T_COALESCE + T_EQUAL. |
|
65 | + ); |
|
66 | 66 | |
67 | 67 | |
68 | - /** |
|
69 | - * A list of new operators which are not recognized in older PHPCS versions. |
|
70 | - * |
|
71 | - * The array lists an alternative token to listen for. |
|
72 | - * |
|
73 | - * @var array(string => int) |
|
74 | - */ |
|
75 | - protected $newOperatorsPHPCSCompat = array( |
|
76 | - 'T_POW' => \T_MULTIPLY, |
|
77 | - 'T_POW_EQUAL' => \T_MUL_EQUAL, |
|
78 | - 'T_SPACESHIP' => \T_GREATER_THAN, |
|
79 | - 'T_COALESCE' => \T_INLINE_THEN, |
|
80 | - 'T_COALESCE_EQUAL' => \T_EQUAL, |
|
81 | - ); |
|
68 | + /** |
|
69 | + * A list of new operators which are not recognized in older PHPCS versions. |
|
70 | + * |
|
71 | + * The array lists an alternative token to listen for. |
|
72 | + * |
|
73 | + * @var array(string => int) |
|
74 | + */ |
|
75 | + protected $newOperatorsPHPCSCompat = array( |
|
76 | + 'T_POW' => \T_MULTIPLY, |
|
77 | + 'T_POW_EQUAL' => \T_MUL_EQUAL, |
|
78 | + 'T_SPACESHIP' => \T_GREATER_THAN, |
|
79 | + 'T_COALESCE' => \T_INLINE_THEN, |
|
80 | + 'T_COALESCE_EQUAL' => \T_EQUAL, |
|
81 | + ); |
|
82 | 82 | |
83 | - /** |
|
84 | - * Token translation table for older PHPCS versions. |
|
85 | - * |
|
86 | - * The 'before' index lists the token which would have to be directly before the |
|
87 | - * token found for it to be one of the new operators. |
|
88 | - * The 'real_token' index indicates which operator was found in that case. |
|
89 | - * |
|
90 | - * If the token combination has multi-layer complexity, such as is the case |
|
91 | - * with T_COALESCE(_EQUAL), a 'callback' index is added instead pointing to a |
|
92 | - * separate function which can determine whether this is the targetted token across |
|
93 | - * PHP and PHPCS versions. |
|
94 | - * |
|
95 | - * {@internal 'before' was chosen rather than 'after' as that allowed for a 1-on-1 |
|
96 | - * translation list with the current tokens.}} |
|
97 | - * |
|
98 | - * @var array(string => array(string => string)) |
|
99 | - */ |
|
100 | - protected $PHPCSCompatTranslate = array( |
|
101 | - 'T_MULTIPLY' => array( |
|
102 | - 'before' => 'T_MULTIPLY', |
|
103 | - 'real_token' => 'T_POW', |
|
104 | - ), |
|
105 | - 'T_MUL_EQUAL' => array( |
|
106 | - 'before' => 'T_MULTIPLY', |
|
107 | - 'real_token' => 'T_POW_EQUAL', |
|
108 | - ), |
|
109 | - 'T_GREATER_THAN' => array( |
|
110 | - 'before' => 'T_IS_SMALLER_OR_EQUAL', |
|
111 | - 'real_token' => 'T_SPACESHIP', |
|
112 | - ), |
|
113 | - 'T_INLINE_THEN' => array( |
|
114 | - 'callback' => 'isTCoalesce', |
|
115 | - 'real_token' => 'T_COALESCE', |
|
116 | - ), |
|
117 | - 'T_EQUAL' => array( |
|
118 | - 'callback' => 'isTCoalesceEqual', |
|
119 | - 'real_token' => 'T_COALESCE_EQUAL', |
|
120 | - ), |
|
121 | - ); |
|
83 | + /** |
|
84 | + * Token translation table for older PHPCS versions. |
|
85 | + * |
|
86 | + * The 'before' index lists the token which would have to be directly before the |
|
87 | + * token found for it to be one of the new operators. |
|
88 | + * The 'real_token' index indicates which operator was found in that case. |
|
89 | + * |
|
90 | + * If the token combination has multi-layer complexity, such as is the case |
|
91 | + * with T_COALESCE(_EQUAL), a 'callback' index is added instead pointing to a |
|
92 | + * separate function which can determine whether this is the targetted token across |
|
93 | + * PHP and PHPCS versions. |
|
94 | + * |
|
95 | + * {@internal 'before' was chosen rather than 'after' as that allowed for a 1-on-1 |
|
96 | + * translation list with the current tokens.}} |
|
97 | + * |
|
98 | + * @var array(string => array(string => string)) |
|
99 | + */ |
|
100 | + protected $PHPCSCompatTranslate = array( |
|
101 | + 'T_MULTIPLY' => array( |
|
102 | + 'before' => 'T_MULTIPLY', |
|
103 | + 'real_token' => 'T_POW', |
|
104 | + ), |
|
105 | + 'T_MUL_EQUAL' => array( |
|
106 | + 'before' => 'T_MULTIPLY', |
|
107 | + 'real_token' => 'T_POW_EQUAL', |
|
108 | + ), |
|
109 | + 'T_GREATER_THAN' => array( |
|
110 | + 'before' => 'T_IS_SMALLER_OR_EQUAL', |
|
111 | + 'real_token' => 'T_SPACESHIP', |
|
112 | + ), |
|
113 | + 'T_INLINE_THEN' => array( |
|
114 | + 'callback' => 'isTCoalesce', |
|
115 | + 'real_token' => 'T_COALESCE', |
|
116 | + ), |
|
117 | + 'T_EQUAL' => array( |
|
118 | + 'callback' => 'isTCoalesceEqual', |
|
119 | + 'real_token' => 'T_COALESCE_EQUAL', |
|
120 | + ), |
|
121 | + ); |
|
122 | 122 | |
123 | - /** |
|
124 | - * Returns an array of tokens this test wants to listen for. |
|
125 | - * |
|
126 | - * @return array |
|
127 | - */ |
|
128 | - public function register() |
|
129 | - { |
|
130 | - $tokens = array(); |
|
131 | - foreach ($this->newOperators as $token => $versions) { |
|
132 | - if (\defined($token)) { |
|
133 | - $tokens[] = constant($token); |
|
134 | - } elseif (isset($this->newOperatorsPHPCSCompat[$token])) { |
|
135 | - $tokens[] = $this->newOperatorsPHPCSCompat[$token]; |
|
136 | - } |
|
137 | - } |
|
138 | - return $tokens; |
|
139 | - } |
|
123 | + /** |
|
124 | + * Returns an array of tokens this test wants to listen for. |
|
125 | + * |
|
126 | + * @return array |
|
127 | + */ |
|
128 | + public function register() |
|
129 | + { |
|
130 | + $tokens = array(); |
|
131 | + foreach ($this->newOperators as $token => $versions) { |
|
132 | + if (\defined($token)) { |
|
133 | + $tokens[] = constant($token); |
|
134 | + } elseif (isset($this->newOperatorsPHPCSCompat[$token])) { |
|
135 | + $tokens[] = $this->newOperatorsPHPCSCompat[$token]; |
|
136 | + } |
|
137 | + } |
|
138 | + return $tokens; |
|
139 | + } |
|
140 | 140 | |
141 | 141 | |
142 | - /** |
|
143 | - * Processes this test, when one of its tokens is encountered. |
|
144 | - * |
|
145 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
146 | - * @param int $stackPtr The position of the current token in |
|
147 | - * the stack passed in $tokens. |
|
148 | - * |
|
149 | - * @return void |
|
150 | - */ |
|
151 | - public function process(File $phpcsFile, $stackPtr) |
|
152 | - { |
|
153 | - $tokens = $phpcsFile->getTokens(); |
|
154 | - $tokenType = $tokens[$stackPtr]['type']; |
|
142 | + /** |
|
143 | + * Processes this test, when one of its tokens is encountered. |
|
144 | + * |
|
145 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
146 | + * @param int $stackPtr The position of the current token in |
|
147 | + * the stack passed in $tokens. |
|
148 | + * |
|
149 | + * @return void |
|
150 | + */ |
|
151 | + public function process(File $phpcsFile, $stackPtr) |
|
152 | + { |
|
153 | + $tokens = $phpcsFile->getTokens(); |
|
154 | + $tokenType = $tokens[$stackPtr]['type']; |
|
155 | 155 | |
156 | - // Translate older PHPCS token combis for new operators to the actual operator. |
|
157 | - if (isset($this->newOperators[$tokenType]) === false) { |
|
158 | - if (isset($this->PHPCSCompatTranslate[$tokenType]) |
|
159 | - && ((isset($this->PHPCSCompatTranslate[$tokenType]['before'], $tokens[$stackPtr - 1]) === true |
|
160 | - && $tokens[$stackPtr - 1]['type'] === $this->PHPCSCompatTranslate[$tokenType]['before']) |
|
161 | - || (isset($this->PHPCSCompatTranslate[$tokenType]['callback']) === true |
|
162 | - && \call_user_func(array($this, $this->PHPCSCompatTranslate[$tokenType]['callback']), $tokens, $stackPtr) === true)) |
|
163 | - ) { |
|
164 | - $tokenType = $this->PHPCSCompatTranslate[$tokenType]['real_token']; |
|
165 | - } |
|
166 | - } elseif ($tokenType === 'T_COALESCE') { |
|
167 | - // Make sure that T_COALESCE is not confused with T_COALESCE_EQUAL. |
|
168 | - if (isset($tokens[($stackPtr + 1)]) !== false && $tokens[($stackPtr + 1)]['code'] === \T_EQUAL) { |
|
169 | - // Ignore as will be dealt with via the T_EQUAL token. |
|
170 | - return; |
|
171 | - } |
|
172 | - } |
|
156 | + // Translate older PHPCS token combis for new operators to the actual operator. |
|
157 | + if (isset($this->newOperators[$tokenType]) === false) { |
|
158 | + if (isset($this->PHPCSCompatTranslate[$tokenType]) |
|
159 | + && ((isset($this->PHPCSCompatTranslate[$tokenType]['before'], $tokens[$stackPtr - 1]) === true |
|
160 | + && $tokens[$stackPtr - 1]['type'] === $this->PHPCSCompatTranslate[$tokenType]['before']) |
|
161 | + || (isset($this->PHPCSCompatTranslate[$tokenType]['callback']) === true |
|
162 | + && \call_user_func(array($this, $this->PHPCSCompatTranslate[$tokenType]['callback']), $tokens, $stackPtr) === true)) |
|
163 | + ) { |
|
164 | + $tokenType = $this->PHPCSCompatTranslate[$tokenType]['real_token']; |
|
165 | + } |
|
166 | + } elseif ($tokenType === 'T_COALESCE') { |
|
167 | + // Make sure that T_COALESCE is not confused with T_COALESCE_EQUAL. |
|
168 | + if (isset($tokens[($stackPtr + 1)]) !== false && $tokens[($stackPtr + 1)]['code'] === \T_EQUAL) { |
|
169 | + // Ignore as will be dealt with via the T_EQUAL token. |
|
170 | + return; |
|
171 | + } |
|
172 | + } |
|
173 | 173 | |
174 | - // If the translation did not yield one of the tokens we are looking for, bow out. |
|
175 | - if (isset($this->newOperators[$tokenType]) === false) { |
|
176 | - return; |
|
177 | - } |
|
174 | + // If the translation did not yield one of the tokens we are looking for, bow out. |
|
175 | + if (isset($this->newOperators[$tokenType]) === false) { |
|
176 | + return; |
|
177 | + } |
|
178 | 178 | |
179 | - $itemInfo = array( |
|
180 | - 'name' => $tokenType, |
|
181 | - ); |
|
182 | - $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
183 | - } |
|
179 | + $itemInfo = array( |
|
180 | + 'name' => $tokenType, |
|
181 | + ); |
|
182 | + $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
183 | + } |
|
184 | 184 | |
185 | 185 | |
186 | - /** |
|
187 | - * Get the relevant sub-array for a specific item from a multi-dimensional array. |
|
188 | - * |
|
189 | - * @param array $itemInfo Base information about the item. |
|
190 | - * |
|
191 | - * @return array Version and other information about the item. |
|
192 | - */ |
|
193 | - public function getItemArray(array $itemInfo) |
|
194 | - { |
|
195 | - return $this->newOperators[$itemInfo['name']]; |
|
196 | - } |
|
186 | + /** |
|
187 | + * Get the relevant sub-array for a specific item from a multi-dimensional array. |
|
188 | + * |
|
189 | + * @param array $itemInfo Base information about the item. |
|
190 | + * |
|
191 | + * @return array Version and other information about the item. |
|
192 | + */ |
|
193 | + public function getItemArray(array $itemInfo) |
|
194 | + { |
|
195 | + return $this->newOperators[$itemInfo['name']]; |
|
196 | + } |
|
197 | 197 | |
198 | 198 | |
199 | - /** |
|
200 | - * Get an array of the non-PHP-version array keys used in a sub-array. |
|
201 | - * |
|
202 | - * @return array |
|
203 | - */ |
|
204 | - protected function getNonVersionArrayKeys() |
|
205 | - { |
|
206 | - return array('description'); |
|
207 | - } |
|
199 | + /** |
|
200 | + * Get an array of the non-PHP-version array keys used in a sub-array. |
|
201 | + * |
|
202 | + * @return array |
|
203 | + */ |
|
204 | + protected function getNonVersionArrayKeys() |
|
205 | + { |
|
206 | + return array('description'); |
|
207 | + } |
|
208 | 208 | |
209 | 209 | |
210 | - /** |
|
211 | - * Retrieve the relevant detail (version) information for use in an error message. |
|
212 | - * |
|
213 | - * @param array $itemArray Version and other information about the item. |
|
214 | - * @param array $itemInfo Base information about the item. |
|
215 | - * |
|
216 | - * @return array |
|
217 | - */ |
|
218 | - public function getErrorInfo(array $itemArray, array $itemInfo) |
|
219 | - { |
|
220 | - $errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
|
221 | - $errorInfo['description'] = $itemArray['description']; |
|
210 | + /** |
|
211 | + * Retrieve the relevant detail (version) information for use in an error message. |
|
212 | + * |
|
213 | + * @param array $itemArray Version and other information about the item. |
|
214 | + * @param array $itemInfo Base information about the item. |
|
215 | + * |
|
216 | + * @return array |
|
217 | + */ |
|
218 | + public function getErrorInfo(array $itemArray, array $itemInfo) |
|
219 | + { |
|
220 | + $errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
|
221 | + $errorInfo['description'] = $itemArray['description']; |
|
222 | 222 | |
223 | - return $errorInfo; |
|
224 | - } |
|
223 | + return $errorInfo; |
|
224 | + } |
|
225 | 225 | |
226 | 226 | |
227 | - /** |
|
228 | - * Allow for concrete child classes to filter the error data before it's passed to PHPCS. |
|
229 | - * |
|
230 | - * @param array $data The error data array which was created. |
|
231 | - * @param array $itemInfo Base information about the item this error message applies to. |
|
232 | - * @param array $errorInfo Detail information about an item this error message applies to. |
|
233 | - * |
|
234 | - * @return array |
|
235 | - */ |
|
236 | - protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
|
237 | - { |
|
238 | - $data[0] = $errorInfo['description']; |
|
239 | - return $data; |
|
240 | - } |
|
227 | + /** |
|
228 | + * Allow for concrete child classes to filter the error data before it's passed to PHPCS. |
|
229 | + * |
|
230 | + * @param array $data The error data array which was created. |
|
231 | + * @param array $itemInfo Base information about the item this error message applies to. |
|
232 | + * @param array $errorInfo Detail information about an item this error message applies to. |
|
233 | + * |
|
234 | + * @return array |
|
235 | + */ |
|
236 | + protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
|
237 | + { |
|
238 | + $data[0] = $errorInfo['description']; |
|
239 | + return $data; |
|
240 | + } |
|
241 | 241 | |
242 | 242 | |
243 | - /** |
|
244 | - * Callback function to determine whether a T_EQUAL token is really a T_COALESCE_EQUAL token. |
|
245 | - * |
|
246 | - * @param array $tokens The token stack. |
|
247 | - * @param int $stackPtr The current position in the token stack. |
|
248 | - * |
|
249 | - * @return bool |
|
250 | - */ |
|
251 | - private function isTCoalesceEqual($tokens, $stackPtr) |
|
252 | - { |
|
253 | - if ($tokens[$stackPtr]['code'] !== \T_EQUAL || isset($tokens[($stackPtr - 1)]) === false) { |
|
254 | - // Function called for wrong token or token has no predecesor. |
|
255 | - return false; |
|
256 | - } |
|
243 | + /** |
|
244 | + * Callback function to determine whether a T_EQUAL token is really a T_COALESCE_EQUAL token. |
|
245 | + * |
|
246 | + * @param array $tokens The token stack. |
|
247 | + * @param int $stackPtr The current position in the token stack. |
|
248 | + * |
|
249 | + * @return bool |
|
250 | + */ |
|
251 | + private function isTCoalesceEqual($tokens, $stackPtr) |
|
252 | + { |
|
253 | + if ($tokens[$stackPtr]['code'] !== \T_EQUAL || isset($tokens[($stackPtr - 1)]) === false) { |
|
254 | + // Function called for wrong token or token has no predecesor. |
|
255 | + return false; |
|
256 | + } |
|
257 | 257 | |
258 | - if ($tokens[($stackPtr - 1)]['type'] === 'T_COALESCE') { |
|
259 | - return true; |
|
260 | - } |
|
261 | - if ($tokens[($stackPtr - 1)]['type'] === 'T_INLINE_THEN' |
|
262 | - && (isset($tokens[($stackPtr - 2)]) && $tokens[($stackPtr - 2)]['type'] === 'T_INLINE_THEN') |
|
263 | - ) { |
|
264 | - return true; |
|
265 | - } |
|
258 | + if ($tokens[($stackPtr - 1)]['type'] === 'T_COALESCE') { |
|
259 | + return true; |
|
260 | + } |
|
261 | + if ($tokens[($stackPtr - 1)]['type'] === 'T_INLINE_THEN' |
|
262 | + && (isset($tokens[($stackPtr - 2)]) && $tokens[($stackPtr - 2)]['type'] === 'T_INLINE_THEN') |
|
263 | + ) { |
|
264 | + return true; |
|
265 | + } |
|
266 | 266 | |
267 | - return false; |
|
268 | - } |
|
267 | + return false; |
|
268 | + } |
|
269 | 269 | |
270 | - /** |
|
271 | - * Callback function to determine whether a T_INLINE_THEN token is really a T_COALESCE token. |
|
272 | - * |
|
273 | - * @param array $tokens The token stack. |
|
274 | - * @param int $stackPtr The current position in the token stack. |
|
275 | - * |
|
276 | - * @return bool |
|
277 | - */ |
|
278 | - private function isTCoalesce($tokens, $stackPtr) |
|
279 | - { |
|
280 | - if ($tokens[$stackPtr]['code'] !== \T_INLINE_THEN || isset($tokens[($stackPtr - 1)]) === false) { |
|
281 | - // Function called for wrong token or token has no predecesor. |
|
282 | - return false; |
|
283 | - } |
|
270 | + /** |
|
271 | + * Callback function to determine whether a T_INLINE_THEN token is really a T_COALESCE token. |
|
272 | + * |
|
273 | + * @param array $tokens The token stack. |
|
274 | + * @param int $stackPtr The current position in the token stack. |
|
275 | + * |
|
276 | + * @return bool |
|
277 | + */ |
|
278 | + private function isTCoalesce($tokens, $stackPtr) |
|
279 | + { |
|
280 | + if ($tokens[$stackPtr]['code'] !== \T_INLINE_THEN || isset($tokens[($stackPtr - 1)]) === false) { |
|
281 | + // Function called for wrong token or token has no predecesor. |
|
282 | + return false; |
|
283 | + } |
|
284 | 284 | |
285 | - if ($tokens[($stackPtr - 1)]['code'] === \T_INLINE_THEN) { |
|
286 | - // Make sure not to confuse it with the T_COALESCE_EQUAL token. |
|
287 | - if (isset($tokens[($stackPtr + 1)]) === false || $tokens[($stackPtr + 1)]['code'] !== \T_EQUAL) { |
|
288 | - return true; |
|
289 | - } |
|
290 | - } |
|
285 | + if ($tokens[($stackPtr - 1)]['code'] === \T_INLINE_THEN) { |
|
286 | + // Make sure not to confuse it with the T_COALESCE_EQUAL token. |
|
287 | + if (isset($tokens[($stackPtr + 1)]) === false || $tokens[($stackPtr + 1)]['code'] !== \T_EQUAL) { |
|
288 | + return true; |
|
289 | + } |
|
290 | + } |
|
291 | 291 | |
292 | - return false; |
|
293 | - } |
|
292 | + return false; |
|
293 | + } |
|
294 | 294 | } |
@@ -128,11 +128,11 @@ discard block |
||
128 | 128 | public function register() |
129 | 129 | { |
130 | 130 | $tokens = array(); |
131 | - foreach ($this->newOperators as $token => $versions) { |
|
132 | - if (\defined($token)) { |
|
133 | - $tokens[] = constant($token); |
|
134 | - } elseif (isset($this->newOperatorsPHPCSCompat[$token])) { |
|
135 | - $tokens[] = $this->newOperatorsPHPCSCompat[$token]; |
|
131 | + foreach ( $this->newOperators as $token => $versions ) { |
|
132 | + if ( \defined( $token ) ) { |
|
133 | + $tokens[ ] = constant( $token ); |
|
134 | + } elseif ( isset( $this->newOperatorsPHPCSCompat[ $token ] ) ) { |
|
135 | + $tokens[ ] = $this->newOperatorsPHPCSCompat[ $token ]; |
|
136 | 136 | } |
137 | 137 | } |
138 | 138 | return $tokens; |
@@ -148,38 +148,38 @@ discard block |
||
148 | 148 | * |
149 | 149 | * @return void |
150 | 150 | */ |
151 | - public function process(File $phpcsFile, $stackPtr) |
|
151 | + public function process( File $phpcsFile, $stackPtr ) |
|
152 | 152 | { |
153 | 153 | $tokens = $phpcsFile->getTokens(); |
154 | - $tokenType = $tokens[$stackPtr]['type']; |
|
154 | + $tokenType = $tokens[ $stackPtr ][ 'type' ]; |
|
155 | 155 | |
156 | 156 | // Translate older PHPCS token combis for new operators to the actual operator. |
157 | - if (isset($this->newOperators[$tokenType]) === false) { |
|
158 | - if (isset($this->PHPCSCompatTranslate[$tokenType]) |
|
159 | - && ((isset($this->PHPCSCompatTranslate[$tokenType]['before'], $tokens[$stackPtr - 1]) === true |
|
160 | - && $tokens[$stackPtr - 1]['type'] === $this->PHPCSCompatTranslate[$tokenType]['before']) |
|
161 | - || (isset($this->PHPCSCompatTranslate[$tokenType]['callback']) === true |
|
162 | - && \call_user_func(array($this, $this->PHPCSCompatTranslate[$tokenType]['callback']), $tokens, $stackPtr) === true)) |
|
157 | + if ( isset( $this->newOperators[ $tokenType ] ) === false ) { |
|
158 | + if ( isset( $this->PHPCSCompatTranslate[ $tokenType ] ) |
|
159 | + && ( ( isset( $this->PHPCSCompatTranslate[ $tokenType ][ 'before' ], $tokens[ $stackPtr - 1 ] ) === true |
|
160 | + && $tokens[ $stackPtr - 1 ][ 'type' ] === $this->PHPCSCompatTranslate[ $tokenType ][ 'before' ] ) |
|
161 | + || ( isset( $this->PHPCSCompatTranslate[ $tokenType ][ 'callback' ] ) === true |
|
162 | + && \call_user_func( array( $this, $this->PHPCSCompatTranslate[ $tokenType ][ 'callback' ] ), $tokens, $stackPtr ) === true ) ) |
|
163 | 163 | ) { |
164 | - $tokenType = $this->PHPCSCompatTranslate[$tokenType]['real_token']; |
|
164 | + $tokenType = $this->PHPCSCompatTranslate[ $tokenType ][ 'real_token' ]; |
|
165 | 165 | } |
166 | - } elseif ($tokenType === 'T_COALESCE') { |
|
166 | + } elseif ( $tokenType === 'T_COALESCE' ) { |
|
167 | 167 | // Make sure that T_COALESCE is not confused with T_COALESCE_EQUAL. |
168 | - if (isset($tokens[($stackPtr + 1)]) !== false && $tokens[($stackPtr + 1)]['code'] === \T_EQUAL) { |
|
168 | + if ( isset( $tokens[ ( $stackPtr + 1 ) ] ) !== false && $tokens[ ( $stackPtr + 1 ) ][ 'code' ] === \T_EQUAL ) { |
|
169 | 169 | // Ignore as will be dealt with via the T_EQUAL token. |
170 | 170 | return; |
171 | 171 | } |
172 | 172 | } |
173 | 173 | |
174 | 174 | // If the translation did not yield one of the tokens we are looking for, bow out. |
175 | - if (isset($this->newOperators[$tokenType]) === false) { |
|
175 | + if ( isset( $this->newOperators[ $tokenType ] ) === false ) { |
|
176 | 176 | return; |
177 | 177 | } |
178 | 178 | |
179 | 179 | $itemInfo = array( |
180 | 180 | 'name' => $tokenType, |
181 | 181 | ); |
182 | - $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
182 | + $this->handleFeature( $phpcsFile, $stackPtr, $itemInfo ); |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | |
@@ -190,9 +190,9 @@ discard block |
||
190 | 190 | * |
191 | 191 | * @return array Version and other information about the item. |
192 | 192 | */ |
193 | - public function getItemArray(array $itemInfo) |
|
193 | + public function getItemArray( array $itemInfo ) |
|
194 | 194 | { |
195 | - return $this->newOperators[$itemInfo['name']]; |
|
195 | + return $this->newOperators[ $itemInfo[ 'name' ] ]; |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | */ |
204 | 204 | protected function getNonVersionArrayKeys() |
205 | 205 | { |
206 | - return array('description'); |
|
206 | + return array( 'description' ); |
|
207 | 207 | } |
208 | 208 | |
209 | 209 | |
@@ -215,10 +215,10 @@ discard block |
||
215 | 215 | * |
216 | 216 | * @return array |
217 | 217 | */ |
218 | - public function getErrorInfo(array $itemArray, array $itemInfo) |
|
218 | + public function getErrorInfo( array $itemArray, array $itemInfo ) |
|
219 | 219 | { |
220 | - $errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
|
221 | - $errorInfo['description'] = $itemArray['description']; |
|
220 | + $errorInfo = parent::getErrorInfo( $itemArray, $itemInfo ); |
|
221 | + $errorInfo[ 'description' ] = $itemArray[ 'description' ]; |
|
222 | 222 | |
223 | 223 | return $errorInfo; |
224 | 224 | } |
@@ -233,9 +233,9 @@ discard block |
||
233 | 233 | * |
234 | 234 | * @return array |
235 | 235 | */ |
236 | - protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
|
236 | + protected function filterErrorData( array $data, array $itemInfo, array $errorInfo ) |
|
237 | 237 | { |
238 | - $data[0] = $errorInfo['description']; |
|
238 | + $data[ 0 ] = $errorInfo[ 'description' ]; |
|
239 | 239 | return $data; |
240 | 240 | } |
241 | 241 | |
@@ -248,18 +248,18 @@ discard block |
||
248 | 248 | * |
249 | 249 | * @return bool |
250 | 250 | */ |
251 | - private function isTCoalesceEqual($tokens, $stackPtr) |
|
251 | + private function isTCoalesceEqual( $tokens, $stackPtr ) |
|
252 | 252 | { |
253 | - if ($tokens[$stackPtr]['code'] !== \T_EQUAL || isset($tokens[($stackPtr - 1)]) === false) { |
|
253 | + if ( $tokens[ $stackPtr ][ 'code' ] !== \T_EQUAL || isset( $tokens[ ( $stackPtr - 1 ) ] ) === false ) { |
|
254 | 254 | // Function called for wrong token or token has no predecesor. |
255 | 255 | return false; |
256 | 256 | } |
257 | 257 | |
258 | - if ($tokens[($stackPtr - 1)]['type'] === 'T_COALESCE') { |
|
258 | + if ( $tokens[ ( $stackPtr - 1 ) ][ 'type' ] === 'T_COALESCE' ) { |
|
259 | 259 | return true; |
260 | 260 | } |
261 | - if ($tokens[($stackPtr - 1)]['type'] === 'T_INLINE_THEN' |
|
262 | - && (isset($tokens[($stackPtr - 2)]) && $tokens[($stackPtr - 2)]['type'] === 'T_INLINE_THEN') |
|
261 | + if ( $tokens[ ( $stackPtr - 1 ) ][ 'type' ] === 'T_INLINE_THEN' |
|
262 | + && ( isset( $tokens[ ( $stackPtr - 2 ) ] ) && $tokens[ ( $stackPtr - 2 ) ][ 'type' ] === 'T_INLINE_THEN' ) |
|
263 | 263 | ) { |
264 | 264 | return true; |
265 | 265 | } |
@@ -275,16 +275,16 @@ discard block |
||
275 | 275 | * |
276 | 276 | * @return bool |
277 | 277 | */ |
278 | - private function isTCoalesce($tokens, $stackPtr) |
|
278 | + private function isTCoalesce( $tokens, $stackPtr ) |
|
279 | 279 | { |
280 | - if ($tokens[$stackPtr]['code'] !== \T_INLINE_THEN || isset($tokens[($stackPtr - 1)]) === false) { |
|
280 | + if ( $tokens[ $stackPtr ][ 'code' ] !== \T_INLINE_THEN || isset( $tokens[ ( $stackPtr - 1 ) ] ) === false ) { |
|
281 | 281 | // Function called for wrong token or token has no predecesor. |
282 | 282 | return false; |
283 | 283 | } |
284 | 284 | |
285 | - if ($tokens[($stackPtr - 1)]['code'] === \T_INLINE_THEN) { |
|
285 | + if ( $tokens[ ( $stackPtr - 1 ) ][ 'code' ] === \T_INLINE_THEN ) { |
|
286 | 286 | // Make sure not to confuse it with the T_COALESCE_EQUAL token. |
287 | - if (isset($tokens[($stackPtr + 1)]) === false || $tokens[($stackPtr + 1)]['code'] !== \T_EQUAL) { |
|
287 | + if ( isset( $tokens[ ( $stackPtr + 1 ) ] ) === false || $tokens[ ( $stackPtr + 1 ) ][ 'code' ] !== \T_EQUAL ) { |
|
288 | 288 | return true; |
289 | 289 | } |
290 | 290 | } |
@@ -21,8 +21,7 @@ discard block |
||
21 | 21 | * @author Wim Godden <[email protected]> |
22 | 22 | * @copyright 2013 Cu.be Solutions bvba |
23 | 23 | */ |
24 | -class NewOperatorsSniff extends AbstractNewFeatureSniff |
|
25 | -{ |
|
24 | +class NewOperatorsSniff extends AbstractNewFeatureSniff { |
|
26 | 25 | |
27 | 26 | /** |
28 | 27 | * A list of new operators, not present in older versions. |
@@ -125,8 +124,7 @@ discard block |
||
125 | 124 | * |
126 | 125 | * @return array |
127 | 126 | */ |
128 | - public function register() |
|
129 | - { |
|
127 | + public function register() { |
|
130 | 128 | $tokens = array(); |
131 | 129 | foreach ($this->newOperators as $token => $versions) { |
132 | 130 | if (\defined($token)) { |
@@ -148,8 +146,7 @@ discard block |
||
148 | 146 | * |
149 | 147 | * @return void |
150 | 148 | */ |
151 | - public function process(File $phpcsFile, $stackPtr) |
|
152 | - { |
|
149 | + public function process(File $phpcsFile, $stackPtr) { |
|
153 | 150 | $tokens = $phpcsFile->getTokens(); |
154 | 151 | $tokenType = $tokens[$stackPtr]['type']; |
155 | 152 | |
@@ -190,8 +187,7 @@ discard block |
||
190 | 187 | * |
191 | 188 | * @return array Version and other information about the item. |
192 | 189 | */ |
193 | - public function getItemArray(array $itemInfo) |
|
194 | - { |
|
190 | + public function getItemArray(array $itemInfo) { |
|
195 | 191 | return $this->newOperators[$itemInfo['name']]; |
196 | 192 | } |
197 | 193 | |
@@ -201,8 +197,7 @@ discard block |
||
201 | 197 | * |
202 | 198 | * @return array |
203 | 199 | */ |
204 | - protected function getNonVersionArrayKeys() |
|
205 | - { |
|
200 | + protected function getNonVersionArrayKeys() { |
|
206 | 201 | return array('description'); |
207 | 202 | } |
208 | 203 | |
@@ -215,8 +210,7 @@ discard block |
||
215 | 210 | * |
216 | 211 | * @return array |
217 | 212 | */ |
218 | - public function getErrorInfo(array $itemArray, array $itemInfo) |
|
219 | - { |
|
213 | + public function getErrorInfo(array $itemArray, array $itemInfo) { |
|
220 | 214 | $errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
221 | 215 | $errorInfo['description'] = $itemArray['description']; |
222 | 216 | |
@@ -233,8 +227,7 @@ discard block |
||
233 | 227 | * |
234 | 228 | * @return array |
235 | 229 | */ |
236 | - protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
|
237 | - { |
|
230 | + protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) { |
|
238 | 231 | $data[0] = $errorInfo['description']; |
239 | 232 | return $data; |
240 | 233 | } |
@@ -248,8 +241,7 @@ discard block |
||
248 | 241 | * |
249 | 242 | * @return bool |
250 | 243 | */ |
251 | - private function isTCoalesceEqual($tokens, $stackPtr) |
|
252 | - { |
|
244 | + private function isTCoalesceEqual($tokens, $stackPtr) { |
|
253 | 245 | if ($tokens[$stackPtr]['code'] !== \T_EQUAL || isset($tokens[($stackPtr - 1)]) === false) { |
254 | 246 | // Function called for wrong token or token has no predecesor. |
255 | 247 | return false; |
@@ -275,8 +267,7 @@ discard block |
||
275 | 267 | * |
276 | 268 | * @return bool |
277 | 269 | */ |
278 | - private function isTCoalesce($tokens, $stackPtr) |
|
279 | - { |
|
270 | + private function isTCoalesce($tokens, $stackPtr) { |
|
280 | 271 | if ($tokens[$stackPtr]['code'] !== \T_INLINE_THEN || isset($tokens[($stackPtr - 1)]) === false) { |
281 | 272 | // Function called for wrong token or token has no predecesor. |
282 | 273 | return false; |
@@ -301,7 +301,7 @@ |
||
301 | 301 | /** |
302 | 302 | * Get an array of the non-PHP-version array keys used in a sub-array. |
303 | 303 | * |
304 | - * @return array |
|
304 | + * @return string[] |
|
305 | 305 | */ |
306 | 306 | protected function getNonVersionArrayKeys() |
307 | 307 | { |
@@ -34,96 +34,96 @@ discard block |
||
34 | 34 | */ |
35 | 35 | class LowPHPCSSniff extends Sniff |
36 | 36 | { |
37 | - /** |
|
38 | - * The minimum supported PHPCS version. |
|
39 | - * |
|
40 | - * Users on PHPCS versions below this will see an ERROR message. |
|
41 | - * |
|
42 | - * @var string |
|
43 | - */ |
|
44 | - protected $minSupportedVersion = '2.3.0'; |
|
45 | - |
|
46 | - /** |
|
47 | - * The minimum recommended PHPCS version. |
|
48 | - * |
|
49 | - * Users on PHPCS versions below this will see a WARNING. |
|
50 | - * |
|
51 | - * @var string |
|
52 | - */ |
|
53 | - protected $minRecommendedVersion = '2.6.0'; |
|
54 | - |
|
55 | - /** |
|
56 | - * Keep track of whether this sniff needs to actually run. |
|
57 | - * |
|
58 | - * This will be set to `false` when either a high enough PHPCS |
|
59 | - * version is detected or once the error/warning has been thrown, |
|
60 | - * to make sure that the notice will only be thrown once per run. |
|
61 | - * |
|
62 | - * @var bool |
|
63 | - */ |
|
64 | - private $examine = true; |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * Returns an array of tokens this test wants to listen for. |
|
69 | - * |
|
70 | - * @return array |
|
71 | - */ |
|
72 | - public function register() |
|
73 | - { |
|
74 | - return array( |
|
75 | - \T_OPEN_TAG, |
|
76 | - ); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Processes this test, when one of its tokens is encountered. |
|
81 | - * |
|
82 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
83 | - * @param int $stackPtr The position of the current token in the |
|
84 | - * stack passed in $tokens. |
|
85 | - * |
|
86 | - * @return int|void Integer stack pointer to skip forward or void to continue |
|
87 | - * normal file processing. |
|
88 | - */ |
|
89 | - public function process(File $phpcsFile, $stackPtr) |
|
90 | - { |
|
91 | - // Don't do anything if the warning has already been thrown or is not necessary. |
|
92 | - if ($this->examine === false) { |
|
93 | - return ($phpcsFile->numTokens + 1); |
|
94 | - } |
|
95 | - |
|
96 | - $phpcsVersion = PHPCSHelper::getVersion(); |
|
97 | - |
|
98 | - // Don't do anything if the PHPCS version used is above the minimum recommended version. |
|
99 | - if (version_compare($phpcsVersion, $this->minRecommendedVersion, '>=')) { |
|
100 | - $this->examine = false; |
|
101 | - return ($phpcsFile->numTokens + 1); |
|
102 | - } |
|
103 | - |
|
104 | - if (version_compare($phpcsVersion, $this->minSupportedVersion, '<')) { |
|
105 | - $isError = true; |
|
106 | - $message = "IMPORTANT: Please be advised that the minimum PHP_CodeSniffer version the PHPCompatibility standard supports is %s. You are currently using PHP_CodeSniffer %s. Please upgrade your PHP_CodeSniffer installation. The recommended version of PHP_CodeSniffer for PHPCompatibility is %s or higher."; |
|
107 | - $errorCode = 'Unsupported_' . $this->stringToErrorCode($this->minSupportedVersion); |
|
108 | - $replacements = array( |
|
109 | - $this->minSupportedVersion, |
|
110 | - $phpcsVersion, |
|
111 | - $this->minRecommendedVersion, |
|
112 | - $errorCode, |
|
113 | - ); |
|
114 | - } else { |
|
115 | - $isError = false; |
|
116 | - $message = "IMPORTANT: Please be advised that for the most reliable PHPCompatibility results, PHP_CodeSniffer %s or higher should be used. Support for lower versions will be dropped in the foreseeable future. You are currently using PHP_CodeSniffer %s. Please upgrade your PHP_CodeSniffer installation to version %s or higher."; |
|
117 | - $errorCode = 'BelowRecommended_' . $this->stringToErrorCode($this->minRecommendedVersion); |
|
118 | - $replacements = array( |
|
119 | - $this->minRecommendedVersion, |
|
120 | - $phpcsVersion, |
|
121 | - $this->minRecommendedVersion, |
|
122 | - $errorCode, |
|
123 | - ); |
|
124 | - } |
|
125 | - |
|
126 | - /* |
|
37 | + /** |
|
38 | + * The minimum supported PHPCS version. |
|
39 | + * |
|
40 | + * Users on PHPCS versions below this will see an ERROR message. |
|
41 | + * |
|
42 | + * @var string |
|
43 | + */ |
|
44 | + protected $minSupportedVersion = '2.3.0'; |
|
45 | + |
|
46 | + /** |
|
47 | + * The minimum recommended PHPCS version. |
|
48 | + * |
|
49 | + * Users on PHPCS versions below this will see a WARNING. |
|
50 | + * |
|
51 | + * @var string |
|
52 | + */ |
|
53 | + protected $minRecommendedVersion = '2.6.0'; |
|
54 | + |
|
55 | + /** |
|
56 | + * Keep track of whether this sniff needs to actually run. |
|
57 | + * |
|
58 | + * This will be set to `false` when either a high enough PHPCS |
|
59 | + * version is detected or once the error/warning has been thrown, |
|
60 | + * to make sure that the notice will only be thrown once per run. |
|
61 | + * |
|
62 | + * @var bool |
|
63 | + */ |
|
64 | + private $examine = true; |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * Returns an array of tokens this test wants to listen for. |
|
69 | + * |
|
70 | + * @return array |
|
71 | + */ |
|
72 | + public function register() |
|
73 | + { |
|
74 | + return array( |
|
75 | + \T_OPEN_TAG, |
|
76 | + ); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Processes this test, when one of its tokens is encountered. |
|
81 | + * |
|
82 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
83 | + * @param int $stackPtr The position of the current token in the |
|
84 | + * stack passed in $tokens. |
|
85 | + * |
|
86 | + * @return int|void Integer stack pointer to skip forward or void to continue |
|
87 | + * normal file processing. |
|
88 | + */ |
|
89 | + public function process(File $phpcsFile, $stackPtr) |
|
90 | + { |
|
91 | + // Don't do anything if the warning has already been thrown or is not necessary. |
|
92 | + if ($this->examine === false) { |
|
93 | + return ($phpcsFile->numTokens + 1); |
|
94 | + } |
|
95 | + |
|
96 | + $phpcsVersion = PHPCSHelper::getVersion(); |
|
97 | + |
|
98 | + // Don't do anything if the PHPCS version used is above the minimum recommended version. |
|
99 | + if (version_compare($phpcsVersion, $this->minRecommendedVersion, '>=')) { |
|
100 | + $this->examine = false; |
|
101 | + return ($phpcsFile->numTokens + 1); |
|
102 | + } |
|
103 | + |
|
104 | + if (version_compare($phpcsVersion, $this->minSupportedVersion, '<')) { |
|
105 | + $isError = true; |
|
106 | + $message = "IMPORTANT: Please be advised that the minimum PHP_CodeSniffer version the PHPCompatibility standard supports is %s. You are currently using PHP_CodeSniffer %s. Please upgrade your PHP_CodeSniffer installation. The recommended version of PHP_CodeSniffer for PHPCompatibility is %s or higher."; |
|
107 | + $errorCode = 'Unsupported_' . $this->stringToErrorCode($this->minSupportedVersion); |
|
108 | + $replacements = array( |
|
109 | + $this->minSupportedVersion, |
|
110 | + $phpcsVersion, |
|
111 | + $this->minRecommendedVersion, |
|
112 | + $errorCode, |
|
113 | + ); |
|
114 | + } else { |
|
115 | + $isError = false; |
|
116 | + $message = "IMPORTANT: Please be advised that for the most reliable PHPCompatibility results, PHP_CodeSniffer %s or higher should be used. Support for lower versions will be dropped in the foreseeable future. You are currently using PHP_CodeSniffer %s. Please upgrade your PHP_CodeSniffer installation to version %s or higher."; |
|
117 | + $errorCode = 'BelowRecommended_' . $this->stringToErrorCode($this->minRecommendedVersion); |
|
118 | + $replacements = array( |
|
119 | + $this->minRecommendedVersion, |
|
120 | + $phpcsVersion, |
|
121 | + $this->minRecommendedVersion, |
|
122 | + $errorCode, |
|
123 | + ); |
|
124 | + } |
|
125 | + |
|
126 | + /* |
|
127 | 127 | * Figure out the report width to determine how long the delimiter lines should be. |
128 | 128 | * |
129 | 129 | * This is not an exact calculation as there are a number of unknowns at the time the |
@@ -149,24 +149,24 @@ discard block |
||
149 | 149 | * If/when the upstream PR has been merged and the minimum supported/recommended version |
150 | 150 | * of PHPCompatibility would go beyond that, the below code should be adjusted.}} |
151 | 151 | */ |
152 | - $reportWidth = PHPCSHelper::getCommandLineData($phpcsFile, 'reportWidth'); |
|
153 | - $showSources = PHPCSHelper::getCommandLineData($phpcsFile, 'showSources'); |
|
154 | - if ($showSources === true && version_compare($phpcsVersion, '2.3.0', '>=')) { |
|
155 | - $reportWidth += 6; |
|
156 | - } |
|
157 | - |
|
158 | - $messageWidth = ($reportWidth - 15); // 15 is length of " # | WARNING | ". |
|
159 | - $delimiterLine = str_repeat('-', ($messageWidth)); |
|
160 | - $disableNotice = 'To disable this notice, add --exclude=PHPCompatibility.Upgrade.LowPHPCS to your command or add <exclude name="PHPCompatibility.Upgrade.LowPHPCS.%s"/> to your custom ruleset. '; |
|
161 | - $thankYou = 'Thank you for using PHPCompatibility!'; |
|
162 | - |
|
163 | - $message .= ' ' . $delimiterLine; |
|
164 | - $message .= ' ' . $disableNotice; |
|
165 | - $message .= ' ' . $delimiterLine; |
|
166 | - $message .= ' ' . $thankYou; |
|
167 | - |
|
168 | - $this->addMessage($phpcsFile, $message, 0, $isError, $errorCode, $replacements); |
|
169 | - |
|
170 | - $this->examine = false; |
|
171 | - } |
|
152 | + $reportWidth = PHPCSHelper::getCommandLineData($phpcsFile, 'reportWidth'); |
|
153 | + $showSources = PHPCSHelper::getCommandLineData($phpcsFile, 'showSources'); |
|
154 | + if ($showSources === true && version_compare($phpcsVersion, '2.3.0', '>=')) { |
|
155 | + $reportWidth += 6; |
|
156 | + } |
|
157 | + |
|
158 | + $messageWidth = ($reportWidth - 15); // 15 is length of " # | WARNING | ". |
|
159 | + $delimiterLine = str_repeat('-', ($messageWidth)); |
|
160 | + $disableNotice = 'To disable this notice, add --exclude=PHPCompatibility.Upgrade.LowPHPCS to your command or add <exclude name="PHPCompatibility.Upgrade.LowPHPCS.%s"/> to your custom ruleset. '; |
|
161 | + $thankYou = 'Thank you for using PHPCompatibility!'; |
|
162 | + |
|
163 | + $message .= ' ' . $delimiterLine; |
|
164 | + $message .= ' ' . $disableNotice; |
|
165 | + $message .= ' ' . $delimiterLine; |
|
166 | + $message .= ' ' . $thankYou; |
|
167 | + |
|
168 | + $this->addMessage($phpcsFile, $message, 0, $isError, $errorCode, $replacements); |
|
169 | + |
|
170 | + $this->examine = false; |
|
171 | + } |
|
172 | 172 | } |
@@ -86,25 +86,25 @@ discard block |
||
86 | 86 | * @return int|void Integer stack pointer to skip forward or void to continue |
87 | 87 | * normal file processing. |
88 | 88 | */ |
89 | - public function process(File $phpcsFile, $stackPtr) |
|
89 | + public function process( File $phpcsFile, $stackPtr ) |
|
90 | 90 | { |
91 | 91 | // Don't do anything if the warning has already been thrown or is not necessary. |
92 | - if ($this->examine === false) { |
|
93 | - return ($phpcsFile->numTokens + 1); |
|
92 | + if ( $this->examine === false ) { |
|
93 | + return ( $phpcsFile->numTokens + 1 ); |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | $phpcsVersion = PHPCSHelper::getVersion(); |
97 | 97 | |
98 | 98 | // Don't do anything if the PHPCS version used is above the minimum recommended version. |
99 | - if (version_compare($phpcsVersion, $this->minRecommendedVersion, '>=')) { |
|
99 | + if ( version_compare( $phpcsVersion, $this->minRecommendedVersion, '>=' ) ) { |
|
100 | 100 | $this->examine = false; |
101 | - return ($phpcsFile->numTokens + 1); |
|
101 | + return ( $phpcsFile->numTokens + 1 ); |
|
102 | 102 | } |
103 | 103 | |
104 | - if (version_compare($phpcsVersion, $this->minSupportedVersion, '<')) { |
|
104 | + if ( version_compare( $phpcsVersion, $this->minSupportedVersion, '<' ) ) { |
|
105 | 105 | $isError = true; |
106 | 106 | $message = "IMPORTANT: Please be advised that the minimum PHP_CodeSniffer version the PHPCompatibility standard supports is %s. You are currently using PHP_CodeSniffer %s. Please upgrade your PHP_CodeSniffer installation. The recommended version of PHP_CodeSniffer for PHPCompatibility is %s or higher."; |
107 | - $errorCode = 'Unsupported_' . $this->stringToErrorCode($this->minSupportedVersion); |
|
107 | + $errorCode = 'Unsupported_' . $this->stringToErrorCode( $this->minSupportedVersion ); |
|
108 | 108 | $replacements = array( |
109 | 109 | $this->minSupportedVersion, |
110 | 110 | $phpcsVersion, |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | } else { |
115 | 115 | $isError = false; |
116 | 116 | $message = "IMPORTANT: Please be advised that for the most reliable PHPCompatibility results, PHP_CodeSniffer %s or higher should be used. Support for lower versions will be dropped in the foreseeable future. You are currently using PHP_CodeSniffer %s. Please upgrade your PHP_CodeSniffer installation to version %s or higher."; |
117 | - $errorCode = 'BelowRecommended_' . $this->stringToErrorCode($this->minRecommendedVersion); |
|
117 | + $errorCode = 'BelowRecommended_' . $this->stringToErrorCode( $this->minRecommendedVersion ); |
|
118 | 118 | $replacements = array( |
119 | 119 | $this->minRecommendedVersion, |
120 | 120 | $phpcsVersion, |
@@ -149,14 +149,14 @@ discard block |
||
149 | 149 | * If/when the upstream PR has been merged and the minimum supported/recommended version |
150 | 150 | * of PHPCompatibility would go beyond that, the below code should be adjusted.}} |
151 | 151 | */ |
152 | - $reportWidth = PHPCSHelper::getCommandLineData($phpcsFile, 'reportWidth'); |
|
153 | - $showSources = PHPCSHelper::getCommandLineData($phpcsFile, 'showSources'); |
|
154 | - if ($showSources === true && version_compare($phpcsVersion, '2.3.0', '>=')) { |
|
152 | + $reportWidth = PHPCSHelper::getCommandLineData( $phpcsFile, 'reportWidth' ); |
|
153 | + $showSources = PHPCSHelper::getCommandLineData( $phpcsFile, 'showSources' ); |
|
154 | + if ( $showSources === true && version_compare( $phpcsVersion, '2.3.0', '>=' ) ) { |
|
155 | 155 | $reportWidth += 6; |
156 | 156 | } |
157 | 157 | |
158 | - $messageWidth = ($reportWidth - 15); // 15 is length of " # | WARNING | ". |
|
159 | - $delimiterLine = str_repeat('-', ($messageWidth)); |
|
158 | + $messageWidth = ( $reportWidth - 15 ); // 15 is length of " # | WARNING | ". |
|
159 | + $delimiterLine = str_repeat( '-', ( $messageWidth ) ); |
|
160 | 160 | $disableNotice = 'To disable this notice, add --exclude=PHPCompatibility.Upgrade.LowPHPCS to your command or add <exclude name="PHPCompatibility.Upgrade.LowPHPCS.%s"/> to your custom ruleset. '; |
161 | 161 | $thankYou = 'Thank you for using PHPCompatibility!'; |
162 | 162 | |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | $message .= ' ' . $delimiterLine; |
166 | 166 | $message .= ' ' . $thankYou; |
167 | 167 | |
168 | - $this->addMessage($phpcsFile, $message, 0, $isError, $errorCode, $replacements); |
|
168 | + $this->addMessage( $phpcsFile, $message, 0, $isError, $errorCode, $replacements ); |
|
169 | 169 | |
170 | 170 | $this->examine = false; |
171 | 171 | } |
@@ -32,8 +32,7 @@ discard block |
||
32 | 32 | * @package PHPCompatibility |
33 | 33 | * @author Juliette Reinders Folmer <[email protected]> |
34 | 34 | */ |
35 | -class LowPHPCSSniff extends Sniff |
|
36 | -{ |
|
35 | +class LowPHPCSSniff extends Sniff { |
|
37 | 36 | /** |
38 | 37 | * The minimum supported PHPCS version. |
39 | 38 | * |
@@ -69,8 +68,7 @@ discard block |
||
69 | 68 | * |
70 | 69 | * @return array |
71 | 70 | */ |
72 | - public function register() |
|
73 | - { |
|
71 | + public function register() { |
|
74 | 72 | return array( |
75 | 73 | \T_OPEN_TAG, |
76 | 74 | ); |
@@ -86,8 +84,7 @@ discard block |
||
86 | 84 | * @return int|void Integer stack pointer to skip forward or void to continue |
87 | 85 | * normal file processing. |
88 | 86 | */ |
89 | - public function process(File $phpcsFile, $stackPtr) |
|
90 | - { |
|
87 | + public function process(File $phpcsFile, $stackPtr) { |
|
91 | 88 | // Don't do anything if the warning has already been thrown or is not necessary. |
92 | 89 | if ($this->examine === false) { |
93 | 90 | return ($phpcsFile->numTokens + 1); |
@@ -65,7 +65,7 @@ |
||
65 | 65 | /** |
66 | 66 | * Returns an array of tokens this test wants to listen for. |
67 | 67 | * |
68 | - * @return array |
|
68 | + * @return integer[] |
|
69 | 69 | */ |
70 | 70 | public function register() |
71 | 71 | { |
@@ -50,375 +50,375 @@ |
||
50 | 50 | class ForbiddenThisUseContextsSniff extends Sniff |
51 | 51 | { |
52 | 52 | |
53 | - /** |
|
54 | - * OO scope tokens. |
|
55 | - * |
|
56 | - * Duplicate of Tokens::$ooScopeTokens array in PHPCS which was added in 3.1.0. |
|
57 | - * |
|
58 | - * @since 9.1.0 |
|
59 | - * |
|
60 | - * @var array |
|
61 | - */ |
|
62 | - private $ooScopeTokens = array( |
|
63 | - 'T_CLASS' => \T_CLASS, |
|
64 | - 'T_INTERFACE' => \T_INTERFACE, |
|
65 | - 'T_TRAIT' => \T_TRAIT, |
|
66 | - ); |
|
67 | - |
|
68 | - /** |
|
69 | - * Scopes to skip over when examining the contents of functions. |
|
70 | - * |
|
71 | - * @since 9.1.0 |
|
72 | - * |
|
73 | - * @var array |
|
74 | - */ |
|
75 | - private $skipOverScopes = array( |
|
76 | - 'T_FUNCTION' => true, |
|
77 | - 'T_CLOSURE' => true, |
|
78 | - ); |
|
79 | - |
|
80 | - /** |
|
81 | - * Valid uses of $this in plain functions or methods outside object context. |
|
82 | - * |
|
83 | - * @since 9.1.0 |
|
84 | - * |
|
85 | - * @var array |
|
86 | - */ |
|
87 | - private $validUseOutsideObject = array( |
|
88 | - \T_ISSET => true, |
|
89 | - \T_EMPTY => true, |
|
90 | - ); |
|
91 | - |
|
92 | - /** |
|
93 | - * Returns an array of tokens this test wants to listen for. |
|
94 | - * |
|
95 | - * @since 9.1.0 |
|
96 | - * |
|
97 | - * @return array |
|
98 | - */ |
|
99 | - public function register() |
|
100 | - { |
|
101 | - if (\defined('T_ANON_CLASS')) { |
|
102 | - $this->ooScopeTokens['T_ANON_CLASS'] = \T_ANON_CLASS; |
|
103 | - } |
|
104 | - |
|
105 | - $this->skipOverScopes += $this->ooScopeTokens; |
|
106 | - |
|
107 | - return array( |
|
108 | - \T_FUNCTION, |
|
109 | - \T_CLOSURE, |
|
110 | - \T_GLOBAL, |
|
111 | - \T_CATCH, |
|
112 | - \T_FOREACH, |
|
113 | - \T_UNSET, |
|
114 | - ); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Processes this test, when one of its tokens is encountered. |
|
119 | - * |
|
120 | - * @since 9.1.0 |
|
121 | - * |
|
122 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
123 | - * @param int $stackPtr The position of the current token in |
|
124 | - * the stack passed in $tokens. |
|
125 | - * |
|
126 | - * @return void |
|
127 | - */ |
|
128 | - public function process(File $phpcsFile, $stackPtr) |
|
129 | - { |
|
130 | - if ($this->supportsAbove('7.1') === false) { |
|
131 | - return; |
|
132 | - } |
|
133 | - |
|
134 | - $tokens = $phpcsFile->getTokens(); |
|
135 | - |
|
136 | - switch ($tokens[$stackPtr]['code']) { |
|
137 | - case \T_FUNCTION: |
|
138 | - $this->isThisUsedAsParameter($phpcsFile, $stackPtr); |
|
139 | - $this->isThisUsedOutsideObjectContext($phpcsFile, $stackPtr); |
|
140 | - break; |
|
141 | - |
|
142 | - case \T_CLOSURE: |
|
143 | - $this->isThisUsedAsParameter($phpcsFile, $stackPtr); |
|
144 | - break; |
|
145 | - |
|
146 | - case \T_GLOBAL: |
|
147 | - /* |
|
53 | + /** |
|
54 | + * OO scope tokens. |
|
55 | + * |
|
56 | + * Duplicate of Tokens::$ooScopeTokens array in PHPCS which was added in 3.1.0. |
|
57 | + * |
|
58 | + * @since 9.1.0 |
|
59 | + * |
|
60 | + * @var array |
|
61 | + */ |
|
62 | + private $ooScopeTokens = array( |
|
63 | + 'T_CLASS' => \T_CLASS, |
|
64 | + 'T_INTERFACE' => \T_INTERFACE, |
|
65 | + 'T_TRAIT' => \T_TRAIT, |
|
66 | + ); |
|
67 | + |
|
68 | + /** |
|
69 | + * Scopes to skip over when examining the contents of functions. |
|
70 | + * |
|
71 | + * @since 9.1.0 |
|
72 | + * |
|
73 | + * @var array |
|
74 | + */ |
|
75 | + private $skipOverScopes = array( |
|
76 | + 'T_FUNCTION' => true, |
|
77 | + 'T_CLOSURE' => true, |
|
78 | + ); |
|
79 | + |
|
80 | + /** |
|
81 | + * Valid uses of $this in plain functions or methods outside object context. |
|
82 | + * |
|
83 | + * @since 9.1.0 |
|
84 | + * |
|
85 | + * @var array |
|
86 | + */ |
|
87 | + private $validUseOutsideObject = array( |
|
88 | + \T_ISSET => true, |
|
89 | + \T_EMPTY => true, |
|
90 | + ); |
|
91 | + |
|
92 | + /** |
|
93 | + * Returns an array of tokens this test wants to listen for. |
|
94 | + * |
|
95 | + * @since 9.1.0 |
|
96 | + * |
|
97 | + * @return array |
|
98 | + */ |
|
99 | + public function register() |
|
100 | + { |
|
101 | + if (\defined('T_ANON_CLASS')) { |
|
102 | + $this->ooScopeTokens['T_ANON_CLASS'] = \T_ANON_CLASS; |
|
103 | + } |
|
104 | + |
|
105 | + $this->skipOverScopes += $this->ooScopeTokens; |
|
106 | + |
|
107 | + return array( |
|
108 | + \T_FUNCTION, |
|
109 | + \T_CLOSURE, |
|
110 | + \T_GLOBAL, |
|
111 | + \T_CATCH, |
|
112 | + \T_FOREACH, |
|
113 | + \T_UNSET, |
|
114 | + ); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Processes this test, when one of its tokens is encountered. |
|
119 | + * |
|
120 | + * @since 9.1.0 |
|
121 | + * |
|
122 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
123 | + * @param int $stackPtr The position of the current token in |
|
124 | + * the stack passed in $tokens. |
|
125 | + * |
|
126 | + * @return void |
|
127 | + */ |
|
128 | + public function process(File $phpcsFile, $stackPtr) |
|
129 | + { |
|
130 | + if ($this->supportsAbove('7.1') === false) { |
|
131 | + return; |
|
132 | + } |
|
133 | + |
|
134 | + $tokens = $phpcsFile->getTokens(); |
|
135 | + |
|
136 | + switch ($tokens[$stackPtr]['code']) { |
|
137 | + case \T_FUNCTION: |
|
138 | + $this->isThisUsedAsParameter($phpcsFile, $stackPtr); |
|
139 | + $this->isThisUsedOutsideObjectContext($phpcsFile, $stackPtr); |
|
140 | + break; |
|
141 | + |
|
142 | + case \T_CLOSURE: |
|
143 | + $this->isThisUsedAsParameter($phpcsFile, $stackPtr); |
|
144 | + break; |
|
145 | + |
|
146 | + case \T_GLOBAL: |
|
147 | + /* |
|
148 | 148 | * $this can no longer be imported using the `global` keyword. |
149 | 149 | * This worked in PHP 7.0, though in PHP 5.x, it would throw a |
150 | 150 | * fatal "Cannot re-assign $this" error. |
151 | 151 | */ |
152 | - $endOfStatement = $phpcsFile->findNext(array(\T_SEMICOLON, \T_CLOSE_TAG), ($stackPtr + 1)); |
|
153 | - if ($endOfStatement === false) { |
|
154 | - // No semi-colon - live coding. |
|
155 | - return; |
|
156 | - } |
|
157 | - |
|
158 | - for ($i = ($stackPtr + 1); $i < $endOfStatement; $i++) { |
|
159 | - if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$this') { |
|
160 | - continue; |
|
161 | - } |
|
162 | - |
|
163 | - $phpcsFile->addError( |
|
164 | - '"$this" can no longer be used with the "global" keyword since PHP 7.1.', |
|
165 | - $i, |
|
166 | - 'Global' |
|
167 | - ); |
|
168 | - } |
|
169 | - |
|
170 | - break; |
|
171 | - |
|
172 | - case \T_CATCH: |
|
173 | - /* |
|
152 | + $endOfStatement = $phpcsFile->findNext(array(\T_SEMICOLON, \T_CLOSE_TAG), ($stackPtr + 1)); |
|
153 | + if ($endOfStatement === false) { |
|
154 | + // No semi-colon - live coding. |
|
155 | + return; |
|
156 | + } |
|
157 | + |
|
158 | + for ($i = ($stackPtr + 1); $i < $endOfStatement; $i++) { |
|
159 | + if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$this') { |
|
160 | + continue; |
|
161 | + } |
|
162 | + |
|
163 | + $phpcsFile->addError( |
|
164 | + '"$this" can no longer be used with the "global" keyword since PHP 7.1.', |
|
165 | + $i, |
|
166 | + 'Global' |
|
167 | + ); |
|
168 | + } |
|
169 | + |
|
170 | + break; |
|
171 | + |
|
172 | + case \T_CATCH: |
|
173 | + /* |
|
174 | 174 | * $this can no longer be used as a catch variable. |
175 | 175 | */ |
176 | - if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) { |
|
177 | - return; |
|
178 | - } |
|
179 | - |
|
180 | - $varPtr = $phpcsFile->findNext( |
|
181 | - \T_VARIABLE, |
|
182 | - ($tokens[$stackPtr]['parenthesis_opener'] + 1), |
|
183 | - $tokens[$stackPtr]['parenthesis_closer'] |
|
184 | - ); |
|
185 | - |
|
186 | - if ($varPtr === false || $tokens[$varPtr]['content'] !== '$this') { |
|
187 | - return; |
|
188 | - } |
|
189 | - |
|
190 | - $phpcsFile->addError( |
|
191 | - '"$this" can no longer be used as a catch variable since PHP 7.1.', |
|
192 | - $varPtr, |
|
193 | - 'Catch' |
|
194 | - ); |
|
195 | - |
|
196 | - break; |
|
197 | - |
|
198 | - case \T_FOREACH: |
|
199 | - /* |
|
176 | + if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) { |
|
177 | + return; |
|
178 | + } |
|
179 | + |
|
180 | + $varPtr = $phpcsFile->findNext( |
|
181 | + \T_VARIABLE, |
|
182 | + ($tokens[$stackPtr]['parenthesis_opener'] + 1), |
|
183 | + $tokens[$stackPtr]['parenthesis_closer'] |
|
184 | + ); |
|
185 | + |
|
186 | + if ($varPtr === false || $tokens[$varPtr]['content'] !== '$this') { |
|
187 | + return; |
|
188 | + } |
|
189 | + |
|
190 | + $phpcsFile->addError( |
|
191 | + '"$this" can no longer be used as a catch variable since PHP 7.1.', |
|
192 | + $varPtr, |
|
193 | + 'Catch' |
|
194 | + ); |
|
195 | + |
|
196 | + break; |
|
197 | + |
|
198 | + case \T_FOREACH: |
|
199 | + /* |
|
200 | 200 | * $this can no longer be used as a foreach *value* variable. |
201 | 201 | * This worked in PHP 7.0, though in PHP 5.x, it would throw a |
202 | 202 | * fatal "Cannot re-assign $this" error. |
203 | 203 | */ |
204 | - if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) { |
|
205 | - return; |
|
206 | - } |
|
207 | - |
|
208 | - $stopPtr = $phpcsFile->findPrevious( |
|
209 | - array(\T_AS, \T_DOUBLE_ARROW), |
|
210 | - ($tokens[$stackPtr]['parenthesis_closer'] - 1), |
|
211 | - $tokens[$stackPtr]['parenthesis_opener'] |
|
212 | - ); |
|
213 | - if ($stopPtr === false) { |
|
214 | - return; |
|
215 | - } |
|
216 | - |
|
217 | - $valueVarPtr = $phpcsFile->findNext( |
|
218 | - \T_VARIABLE, |
|
219 | - ($stopPtr + 1), |
|
220 | - $tokens[$stackPtr]['parenthesis_closer'] |
|
221 | - ); |
|
222 | - if ($valueVarPtr === false || $tokens[$valueVarPtr]['content'] !== '$this') { |
|
223 | - return; |
|
224 | - } |
|
225 | - |
|
226 | - $afterThis = $phpcsFile->findNext( |
|
227 | - Tokens::$emptyTokens, |
|
228 | - ($valueVarPtr + 1), |
|
229 | - $tokens[$stackPtr]['parenthesis_closer'], |
|
230 | - true |
|
231 | - ); |
|
232 | - |
|
233 | - if ($afterThis !== false |
|
234 | - && ($tokens[$afterThis]['code'] === \T_OBJECT_OPERATOR |
|
235 | - || $tokens[$afterThis]['code'] === \T_DOUBLE_COLON) |
|
236 | - ) { |
|
237 | - return; |
|
238 | - } |
|
239 | - |
|
240 | - $phpcsFile->addError( |
|
241 | - '"$this" can no longer be used as value variable in a foreach control structure since PHP 7.1.', |
|
242 | - $valueVarPtr, |
|
243 | - 'ForeachValueVar' |
|
244 | - ); |
|
245 | - |
|
246 | - break; |
|
247 | - |
|
248 | - case \T_UNSET: |
|
249 | - /* |
|
204 | + if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) { |
|
205 | + return; |
|
206 | + } |
|
207 | + |
|
208 | + $stopPtr = $phpcsFile->findPrevious( |
|
209 | + array(\T_AS, \T_DOUBLE_ARROW), |
|
210 | + ($tokens[$stackPtr]['parenthesis_closer'] - 1), |
|
211 | + $tokens[$stackPtr]['parenthesis_opener'] |
|
212 | + ); |
|
213 | + if ($stopPtr === false) { |
|
214 | + return; |
|
215 | + } |
|
216 | + |
|
217 | + $valueVarPtr = $phpcsFile->findNext( |
|
218 | + \T_VARIABLE, |
|
219 | + ($stopPtr + 1), |
|
220 | + $tokens[$stackPtr]['parenthesis_closer'] |
|
221 | + ); |
|
222 | + if ($valueVarPtr === false || $tokens[$valueVarPtr]['content'] !== '$this') { |
|
223 | + return; |
|
224 | + } |
|
225 | + |
|
226 | + $afterThis = $phpcsFile->findNext( |
|
227 | + Tokens::$emptyTokens, |
|
228 | + ($valueVarPtr + 1), |
|
229 | + $tokens[$stackPtr]['parenthesis_closer'], |
|
230 | + true |
|
231 | + ); |
|
232 | + |
|
233 | + if ($afterThis !== false |
|
234 | + && ($tokens[$afterThis]['code'] === \T_OBJECT_OPERATOR |
|
235 | + || $tokens[$afterThis]['code'] === \T_DOUBLE_COLON) |
|
236 | + ) { |
|
237 | + return; |
|
238 | + } |
|
239 | + |
|
240 | + $phpcsFile->addError( |
|
241 | + '"$this" can no longer be used as value variable in a foreach control structure since PHP 7.1.', |
|
242 | + $valueVarPtr, |
|
243 | + 'ForeachValueVar' |
|
244 | + ); |
|
245 | + |
|
246 | + break; |
|
247 | + |
|
248 | + case \T_UNSET: |
|
249 | + /* |
|
250 | 250 | * $this can no longer be unset. |
251 | 251 | */ |
252 | - $openParenthesis = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
253 | - if ($openParenthesis === false |
|
254 | - || $tokens[$openParenthesis]['code'] !== \T_OPEN_PARENTHESIS |
|
255 | - || isset($tokens[$openParenthesis]['parenthesis_closer']) === false |
|
256 | - ) { |
|
257 | - return; |
|
258 | - } |
|
259 | - |
|
260 | - for ($i = ($openParenthesis + 1); $i < $tokens[$openParenthesis]['parenthesis_closer']; $i++) { |
|
261 | - if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$this') { |
|
262 | - continue; |
|
263 | - } |
|
264 | - |
|
265 | - $afterThis = $phpcsFile->findNext( |
|
266 | - Tokens::$emptyTokens, |
|
267 | - ($i + 1), |
|
268 | - $tokens[$openParenthesis]['parenthesis_closer'], |
|
269 | - true |
|
270 | - ); |
|
271 | - |
|
272 | - if ($afterThis !== false |
|
273 | - && ($tokens[$afterThis]['code'] === \T_OBJECT_OPERATOR |
|
274 | - || $tokens[$afterThis]['code'] === \T_DOUBLE_COLON |
|
275 | - || $tokens[$afterThis]['code'] === \T_OPEN_SQUARE_BRACKET) |
|
276 | - ) { |
|
277 | - $i = $afterThis; |
|
278 | - continue; |
|
279 | - } |
|
280 | - |
|
281 | - $phpcsFile->addError( |
|
282 | - '"$this" can no longer be unset since PHP 7.1.', |
|
283 | - $i, |
|
284 | - 'Unset' |
|
285 | - ); |
|
286 | - } |
|
287 | - |
|
288 | - break; |
|
289 | - } |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * Check if $this is used as a parameter in a function declaration. |
|
294 | - * |
|
295 | - * $this can no longer be used as a parameter in a *global* function. |
|
296 | - * Use as a parameter in a method was already an error prior to PHP 7.1. |
|
297 | - * |
|
298 | - * @since 9.1.0 |
|
299 | - * |
|
300 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
301 | - * @param int $stackPtr The position of the current token in |
|
302 | - * the stack passed in $tokens. |
|
303 | - * |
|
304 | - * @return void |
|
305 | - */ |
|
306 | - protected function isThisUsedAsParameter(File $phpcsFile, $stackPtr) |
|
307 | - { |
|
308 | - if ($this->validDirectScope($phpcsFile, $stackPtr, $this->ooScopeTokens) !== false) { |
|
309 | - return; |
|
310 | - } |
|
311 | - |
|
312 | - $params = PHPCSHelper::getMethodParameters($phpcsFile, $stackPtr); |
|
313 | - if (empty($params)) { |
|
314 | - return; |
|
315 | - } |
|
316 | - |
|
317 | - $tokens = $phpcsFile->getTokens(); |
|
318 | - |
|
319 | - foreach ($params as $param) { |
|
320 | - if ($param['name'] !== '$this') { |
|
321 | - continue; |
|
322 | - } |
|
323 | - |
|
324 | - if ($tokens[$stackPtr]['code'] === \T_FUNCTION) { |
|
325 | - $phpcsFile->addError( |
|
326 | - '"$this" can no longer be used as a parameter since PHP 7.1.', |
|
327 | - $param['token'], |
|
328 | - 'FunctionParam' |
|
329 | - ); |
|
330 | - } else { |
|
331 | - $phpcsFile->addError( |
|
332 | - '"$this" can no longer be used as a closure parameter since PHP 7.0.7.', |
|
333 | - $param['token'], |
|
334 | - 'ClosureParam' |
|
335 | - ); |
|
336 | - } |
|
337 | - } |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * Check if $this is used in a plain function or method. |
|
342 | - * |
|
343 | - * Prior to PHP 7.1, this would result in an "undefined variable" notice |
|
344 | - * and execution would continue with $this regarded as `null`. |
|
345 | - * As of PHP 7.1, this throws an exception. |
|
346 | - * |
|
347 | - * Note: use within isset() and empty() to check object context is still allowed. |
|
348 | - * Note: $this can still be used within a closure. |
|
349 | - * |
|
350 | - * @since 9.1.0 |
|
351 | - * |
|
352 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
353 | - * @param int $stackPtr The position of the current token in |
|
354 | - * the stack passed in $tokens. |
|
355 | - * |
|
356 | - * @return void |
|
357 | - */ |
|
358 | - protected function isThisUsedOutsideObjectContext(File $phpcsFile, $stackPtr) |
|
359 | - { |
|
360 | - $tokens = $phpcsFile->getTokens(); |
|
361 | - |
|
362 | - if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { |
|
363 | - return; |
|
364 | - } |
|
365 | - |
|
366 | - if ($this->validDirectScope($phpcsFile, $stackPtr, $this->ooScopeTokens) !== false) { |
|
367 | - $methodProps = $phpcsFile->getMethodProperties($stackPtr); |
|
368 | - if ($methodProps['is_static'] === false) { |
|
369 | - return; |
|
370 | - } else { |
|
371 | - $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
372 | - if ($methodName === '__call') { |
|
373 | - /* |
|
252 | + $openParenthesis = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
253 | + if ($openParenthesis === false |
|
254 | + || $tokens[$openParenthesis]['code'] !== \T_OPEN_PARENTHESIS |
|
255 | + || isset($tokens[$openParenthesis]['parenthesis_closer']) === false |
|
256 | + ) { |
|
257 | + return; |
|
258 | + } |
|
259 | + |
|
260 | + for ($i = ($openParenthesis + 1); $i < $tokens[$openParenthesis]['parenthesis_closer']; $i++) { |
|
261 | + if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$this') { |
|
262 | + continue; |
|
263 | + } |
|
264 | + |
|
265 | + $afterThis = $phpcsFile->findNext( |
|
266 | + Tokens::$emptyTokens, |
|
267 | + ($i + 1), |
|
268 | + $tokens[$openParenthesis]['parenthesis_closer'], |
|
269 | + true |
|
270 | + ); |
|
271 | + |
|
272 | + if ($afterThis !== false |
|
273 | + && ($tokens[$afterThis]['code'] === \T_OBJECT_OPERATOR |
|
274 | + || $tokens[$afterThis]['code'] === \T_DOUBLE_COLON |
|
275 | + || $tokens[$afterThis]['code'] === \T_OPEN_SQUARE_BRACKET) |
|
276 | + ) { |
|
277 | + $i = $afterThis; |
|
278 | + continue; |
|
279 | + } |
|
280 | + |
|
281 | + $phpcsFile->addError( |
|
282 | + '"$this" can no longer be unset since PHP 7.1.', |
|
283 | + $i, |
|
284 | + 'Unset' |
|
285 | + ); |
|
286 | + } |
|
287 | + |
|
288 | + break; |
|
289 | + } |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * Check if $this is used as a parameter in a function declaration. |
|
294 | + * |
|
295 | + * $this can no longer be used as a parameter in a *global* function. |
|
296 | + * Use as a parameter in a method was already an error prior to PHP 7.1. |
|
297 | + * |
|
298 | + * @since 9.1.0 |
|
299 | + * |
|
300 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
301 | + * @param int $stackPtr The position of the current token in |
|
302 | + * the stack passed in $tokens. |
|
303 | + * |
|
304 | + * @return void |
|
305 | + */ |
|
306 | + protected function isThisUsedAsParameter(File $phpcsFile, $stackPtr) |
|
307 | + { |
|
308 | + if ($this->validDirectScope($phpcsFile, $stackPtr, $this->ooScopeTokens) !== false) { |
|
309 | + return; |
|
310 | + } |
|
311 | + |
|
312 | + $params = PHPCSHelper::getMethodParameters($phpcsFile, $stackPtr); |
|
313 | + if (empty($params)) { |
|
314 | + return; |
|
315 | + } |
|
316 | + |
|
317 | + $tokens = $phpcsFile->getTokens(); |
|
318 | + |
|
319 | + foreach ($params as $param) { |
|
320 | + if ($param['name'] !== '$this') { |
|
321 | + continue; |
|
322 | + } |
|
323 | + |
|
324 | + if ($tokens[$stackPtr]['code'] === \T_FUNCTION) { |
|
325 | + $phpcsFile->addError( |
|
326 | + '"$this" can no longer be used as a parameter since PHP 7.1.', |
|
327 | + $param['token'], |
|
328 | + 'FunctionParam' |
|
329 | + ); |
|
330 | + } else { |
|
331 | + $phpcsFile->addError( |
|
332 | + '"$this" can no longer be used as a closure parameter since PHP 7.0.7.', |
|
333 | + $param['token'], |
|
334 | + 'ClosureParam' |
|
335 | + ); |
|
336 | + } |
|
337 | + } |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * Check if $this is used in a plain function or method. |
|
342 | + * |
|
343 | + * Prior to PHP 7.1, this would result in an "undefined variable" notice |
|
344 | + * and execution would continue with $this regarded as `null`. |
|
345 | + * As of PHP 7.1, this throws an exception. |
|
346 | + * |
|
347 | + * Note: use within isset() and empty() to check object context is still allowed. |
|
348 | + * Note: $this can still be used within a closure. |
|
349 | + * |
|
350 | + * @since 9.1.0 |
|
351 | + * |
|
352 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
353 | + * @param int $stackPtr The position of the current token in |
|
354 | + * the stack passed in $tokens. |
|
355 | + * |
|
356 | + * @return void |
|
357 | + */ |
|
358 | + protected function isThisUsedOutsideObjectContext(File $phpcsFile, $stackPtr) |
|
359 | + { |
|
360 | + $tokens = $phpcsFile->getTokens(); |
|
361 | + |
|
362 | + if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { |
|
363 | + return; |
|
364 | + } |
|
365 | + |
|
366 | + if ($this->validDirectScope($phpcsFile, $stackPtr, $this->ooScopeTokens) !== false) { |
|
367 | + $methodProps = $phpcsFile->getMethodProperties($stackPtr); |
|
368 | + if ($methodProps['is_static'] === false) { |
|
369 | + return; |
|
370 | + } else { |
|
371 | + $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
372 | + if ($methodName === '__call') { |
|
373 | + /* |
|
374 | 374 | * This is an exception. |
375 | 375 | * @link https://wiki.php.net/rfc/this_var#always_show_true_this_value_in_magic_method_call |
376 | 376 | */ |
377 | - return; |
|
378 | - } |
|
379 | - } |
|
380 | - } |
|
381 | - |
|
382 | - for ($i = ($tokens[$stackPtr]['scope_opener'] + 1); $i < $tokens[$stackPtr]['scope_closer']; $i++) { |
|
383 | - if (isset($this->skipOverScopes[$tokens[$i]['type']])) { |
|
384 | - if (isset($tokens[$i]['scope_closer']) === false) { |
|
385 | - // Live coding or parse error, will only lead to inaccurate results. |
|
386 | - return; |
|
387 | - } |
|
388 | - |
|
389 | - // Skip over nested structures. |
|
390 | - $i = $tokens[$i]['scope_closer']; |
|
391 | - continue; |
|
392 | - } |
|
393 | - |
|
394 | - if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$this') { |
|
395 | - continue; |
|
396 | - } |
|
397 | - |
|
398 | - if (isset($tokens[$i]['nested_parenthesis']) === true) { |
|
399 | - $nestedParenthesis = $tokens[$i]['nested_parenthesis']; |
|
400 | - $nestedOpenParenthesis = array_keys($nestedParenthesis); |
|
401 | - $lastOpenParenthesis = array_pop($nestedOpenParenthesis); |
|
402 | - |
|
403 | - $previousNonEmpty = $phpcsFile->findPrevious( |
|
404 | - Tokens::$emptyTokens, |
|
405 | - ($lastOpenParenthesis - 1), |
|
406 | - null, |
|
407 | - true, |
|
408 | - null, |
|
409 | - true |
|
410 | - ); |
|
411 | - |
|
412 | - if (isset($this->validUseOutsideObject[$tokens[$previousNonEmpty]['code']])) { |
|
413 | - continue; |
|
414 | - } |
|
415 | - } |
|
416 | - |
|
417 | - $phpcsFile->addError( |
|
418 | - '"$this" can no longer be used in a plain function or method since PHP 7.1.', |
|
419 | - $i, |
|
420 | - 'OutsideObjectContext' |
|
421 | - ); |
|
422 | - } |
|
423 | - } |
|
377 | + return; |
|
378 | + } |
|
379 | + } |
|
380 | + } |
|
381 | + |
|
382 | + for ($i = ($tokens[$stackPtr]['scope_opener'] + 1); $i < $tokens[$stackPtr]['scope_closer']; $i++) { |
|
383 | + if (isset($this->skipOverScopes[$tokens[$i]['type']])) { |
|
384 | + if (isset($tokens[$i]['scope_closer']) === false) { |
|
385 | + // Live coding or parse error, will only lead to inaccurate results. |
|
386 | + return; |
|
387 | + } |
|
388 | + |
|
389 | + // Skip over nested structures. |
|
390 | + $i = $tokens[$i]['scope_closer']; |
|
391 | + continue; |
|
392 | + } |
|
393 | + |
|
394 | + if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$this') { |
|
395 | + continue; |
|
396 | + } |
|
397 | + |
|
398 | + if (isset($tokens[$i]['nested_parenthesis']) === true) { |
|
399 | + $nestedParenthesis = $tokens[$i]['nested_parenthesis']; |
|
400 | + $nestedOpenParenthesis = array_keys($nestedParenthesis); |
|
401 | + $lastOpenParenthesis = array_pop($nestedOpenParenthesis); |
|
402 | + |
|
403 | + $previousNonEmpty = $phpcsFile->findPrevious( |
|
404 | + Tokens::$emptyTokens, |
|
405 | + ($lastOpenParenthesis - 1), |
|
406 | + null, |
|
407 | + true, |
|
408 | + null, |
|
409 | + true |
|
410 | + ); |
|
411 | + |
|
412 | + if (isset($this->validUseOutsideObject[$tokens[$previousNonEmpty]['code']])) { |
|
413 | + continue; |
|
414 | + } |
|
415 | + } |
|
416 | + |
|
417 | + $phpcsFile->addError( |
|
418 | + '"$this" can no longer be used in a plain function or method since PHP 7.1.', |
|
419 | + $i, |
|
420 | + 'OutsideObjectContext' |
|
421 | + ); |
|
422 | + } |
|
423 | + } |
|
424 | 424 | } |
@@ -98,8 +98,8 @@ discard block |
||
98 | 98 | */ |
99 | 99 | public function register() |
100 | 100 | { |
101 | - if (\defined('T_ANON_CLASS')) { |
|
102 | - $this->ooScopeTokens['T_ANON_CLASS'] = \T_ANON_CLASS; |
|
101 | + if ( \defined( 'T_ANON_CLASS' ) ) { |
|
102 | + $this->ooScopeTokens[ 'T_ANON_CLASS' ] = \T_ANON_CLASS; |
|
103 | 103 | } |
104 | 104 | |
105 | 105 | $this->skipOverScopes += $this->ooScopeTokens; |
@@ -125,22 +125,22 @@ discard block |
||
125 | 125 | * |
126 | 126 | * @return void |
127 | 127 | */ |
128 | - public function process(File $phpcsFile, $stackPtr) |
|
128 | + public function process( File $phpcsFile, $stackPtr ) |
|
129 | 129 | { |
130 | - if ($this->supportsAbove('7.1') === false) { |
|
130 | + if ( $this->supportsAbove( '7.1' ) === false ) { |
|
131 | 131 | return; |
132 | 132 | } |
133 | 133 | |
134 | 134 | $tokens = $phpcsFile->getTokens(); |
135 | 135 | |
136 | - switch ($tokens[$stackPtr]['code']) { |
|
136 | + switch ( $tokens[ $stackPtr ][ 'code' ] ) { |
|
137 | 137 | case \T_FUNCTION: |
138 | - $this->isThisUsedAsParameter($phpcsFile, $stackPtr); |
|
139 | - $this->isThisUsedOutsideObjectContext($phpcsFile, $stackPtr); |
|
138 | + $this->isThisUsedAsParameter( $phpcsFile, $stackPtr ); |
|
139 | + $this->isThisUsedOutsideObjectContext( $phpcsFile, $stackPtr ); |
|
140 | 140 | break; |
141 | 141 | |
142 | 142 | case \T_CLOSURE: |
143 | - $this->isThisUsedAsParameter($phpcsFile, $stackPtr); |
|
143 | + $this->isThisUsedAsParameter( $phpcsFile, $stackPtr ); |
|
144 | 144 | break; |
145 | 145 | |
146 | 146 | case \T_GLOBAL: |
@@ -149,14 +149,14 @@ discard block |
||
149 | 149 | * This worked in PHP 7.0, though in PHP 5.x, it would throw a |
150 | 150 | * fatal "Cannot re-assign $this" error. |
151 | 151 | */ |
152 | - $endOfStatement = $phpcsFile->findNext(array(\T_SEMICOLON, \T_CLOSE_TAG), ($stackPtr + 1)); |
|
153 | - if ($endOfStatement === false) { |
|
152 | + $endOfStatement = $phpcsFile->findNext( array( \T_SEMICOLON, \T_CLOSE_TAG ), ( $stackPtr + 1 ) ); |
|
153 | + if ( $endOfStatement === false ) { |
|
154 | 154 | // No semi-colon - live coding. |
155 | 155 | return; |
156 | 156 | } |
157 | 157 | |
158 | - for ($i = ($stackPtr + 1); $i < $endOfStatement; $i++) { |
|
159 | - if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$this') { |
|
158 | + for ( $i = ( $stackPtr + 1 ); $i < $endOfStatement; $i++ ) { |
|
159 | + if ( $tokens[ $i ][ 'code' ] !== \T_VARIABLE || $tokens[ $i ][ 'content' ] !== '$this' ) { |
|
160 | 160 | continue; |
161 | 161 | } |
162 | 162 | |
@@ -173,17 +173,17 @@ discard block |
||
173 | 173 | /* |
174 | 174 | * $this can no longer be used as a catch variable. |
175 | 175 | */ |
176 | - if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) { |
|
176 | + if ( isset( $tokens[ $stackPtr ][ 'parenthesis_opener' ], $tokens[ $stackPtr ][ 'parenthesis_closer' ] ) === false ) { |
|
177 | 177 | return; |
178 | 178 | } |
179 | 179 | |
180 | 180 | $varPtr = $phpcsFile->findNext( |
181 | 181 | \T_VARIABLE, |
182 | - ($tokens[$stackPtr]['parenthesis_opener'] + 1), |
|
183 | - $tokens[$stackPtr]['parenthesis_closer'] |
|
182 | + ( $tokens[ $stackPtr ][ 'parenthesis_opener' ] + 1 ), |
|
183 | + $tokens[ $stackPtr ][ 'parenthesis_closer' ] |
|
184 | 184 | ); |
185 | 185 | |
186 | - if ($varPtr === false || $tokens[$varPtr]['content'] !== '$this') { |
|
186 | + if ( $varPtr === false || $tokens[ $varPtr ][ 'content' ] !== '$this' ) { |
|
187 | 187 | return; |
188 | 188 | } |
189 | 189 | |
@@ -201,38 +201,38 @@ discard block |
||
201 | 201 | * This worked in PHP 7.0, though in PHP 5.x, it would throw a |
202 | 202 | * fatal "Cannot re-assign $this" error. |
203 | 203 | */ |
204 | - if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) { |
|
204 | + if ( isset( $tokens[ $stackPtr ][ 'parenthesis_opener' ], $tokens[ $stackPtr ][ 'parenthesis_closer' ] ) === false ) { |
|
205 | 205 | return; |
206 | 206 | } |
207 | 207 | |
208 | 208 | $stopPtr = $phpcsFile->findPrevious( |
209 | - array(\T_AS, \T_DOUBLE_ARROW), |
|
210 | - ($tokens[$stackPtr]['parenthesis_closer'] - 1), |
|
211 | - $tokens[$stackPtr]['parenthesis_opener'] |
|
209 | + array( \T_AS, \T_DOUBLE_ARROW ), |
|
210 | + ( $tokens[ $stackPtr ][ 'parenthesis_closer' ] - 1 ), |
|
211 | + $tokens[ $stackPtr ][ 'parenthesis_opener' ] |
|
212 | 212 | ); |
213 | - if ($stopPtr === false) { |
|
213 | + if ( $stopPtr === false ) { |
|
214 | 214 | return; |
215 | 215 | } |
216 | 216 | |
217 | 217 | $valueVarPtr = $phpcsFile->findNext( |
218 | 218 | \T_VARIABLE, |
219 | - ($stopPtr + 1), |
|
220 | - $tokens[$stackPtr]['parenthesis_closer'] |
|
219 | + ( $stopPtr + 1 ), |
|
220 | + $tokens[ $stackPtr ][ 'parenthesis_closer' ] |
|
221 | 221 | ); |
222 | - if ($valueVarPtr === false || $tokens[$valueVarPtr]['content'] !== '$this') { |
|
222 | + if ( $valueVarPtr === false || $tokens[ $valueVarPtr ][ 'content' ] !== '$this' ) { |
|
223 | 223 | return; |
224 | 224 | } |
225 | 225 | |
226 | 226 | $afterThis = $phpcsFile->findNext( |
227 | 227 | Tokens::$emptyTokens, |
228 | - ($valueVarPtr + 1), |
|
229 | - $tokens[$stackPtr]['parenthesis_closer'], |
|
228 | + ( $valueVarPtr + 1 ), |
|
229 | + $tokens[ $stackPtr ][ 'parenthesis_closer' ], |
|
230 | 230 | true |
231 | 231 | ); |
232 | 232 | |
233 | - if ($afterThis !== false |
|
234 | - && ($tokens[$afterThis]['code'] === \T_OBJECT_OPERATOR |
|
235 | - || $tokens[$afterThis]['code'] === \T_DOUBLE_COLON) |
|
233 | + if ( $afterThis !== false |
|
234 | + && ( $tokens[ $afterThis ][ 'code' ] === \T_OBJECT_OPERATOR |
|
235 | + || $tokens[ $afterThis ][ 'code' ] === \T_DOUBLE_COLON ) |
|
236 | 236 | ) { |
237 | 237 | return; |
238 | 238 | } |
@@ -249,30 +249,30 @@ discard block |
||
249 | 249 | /* |
250 | 250 | * $this can no longer be unset. |
251 | 251 | */ |
252 | - $openParenthesis = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
253 | - if ($openParenthesis === false |
|
254 | - || $tokens[$openParenthesis]['code'] !== \T_OPEN_PARENTHESIS |
|
255 | - || isset($tokens[$openParenthesis]['parenthesis_closer']) === false |
|
252 | + $openParenthesis = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); |
|
253 | + if ( $openParenthesis === false |
|
254 | + || $tokens[ $openParenthesis ][ 'code' ] !== \T_OPEN_PARENTHESIS |
|
255 | + || isset( $tokens[ $openParenthesis ][ 'parenthesis_closer' ] ) === false |
|
256 | 256 | ) { |
257 | 257 | return; |
258 | 258 | } |
259 | 259 | |
260 | - for ($i = ($openParenthesis + 1); $i < $tokens[$openParenthesis]['parenthesis_closer']; $i++) { |
|
261 | - if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$this') { |
|
260 | + for ( $i = ( $openParenthesis + 1 ); $i < $tokens[ $openParenthesis ][ 'parenthesis_closer' ]; $i++ ) { |
|
261 | + if ( $tokens[ $i ][ 'code' ] !== \T_VARIABLE || $tokens[ $i ][ 'content' ] !== '$this' ) { |
|
262 | 262 | continue; |
263 | 263 | } |
264 | 264 | |
265 | 265 | $afterThis = $phpcsFile->findNext( |
266 | 266 | Tokens::$emptyTokens, |
267 | - ($i + 1), |
|
268 | - $tokens[$openParenthesis]['parenthesis_closer'], |
|
267 | + ( $i + 1 ), |
|
268 | + $tokens[ $openParenthesis ][ 'parenthesis_closer' ], |
|
269 | 269 | true |
270 | 270 | ); |
271 | 271 | |
272 | - if ($afterThis !== false |
|
273 | - && ($tokens[$afterThis]['code'] === \T_OBJECT_OPERATOR |
|
274 | - || $tokens[$afterThis]['code'] === \T_DOUBLE_COLON |
|
275 | - || $tokens[$afterThis]['code'] === \T_OPEN_SQUARE_BRACKET) |
|
272 | + if ( $afterThis !== false |
|
273 | + && ( $tokens[ $afterThis ][ 'code' ] === \T_OBJECT_OPERATOR |
|
274 | + || $tokens[ $afterThis ][ 'code' ] === \T_DOUBLE_COLON |
|
275 | + || $tokens[ $afterThis ][ 'code' ] === \T_OPEN_SQUARE_BRACKET ) |
|
276 | 276 | ) { |
277 | 277 | $i = $afterThis; |
278 | 278 | continue; |
@@ -303,34 +303,34 @@ discard block |
||
303 | 303 | * |
304 | 304 | * @return void |
305 | 305 | */ |
306 | - protected function isThisUsedAsParameter(File $phpcsFile, $stackPtr) |
|
306 | + protected function isThisUsedAsParameter( File $phpcsFile, $stackPtr ) |
|
307 | 307 | { |
308 | - if ($this->validDirectScope($phpcsFile, $stackPtr, $this->ooScopeTokens) !== false) { |
|
308 | + if ( $this->validDirectScope( $phpcsFile, $stackPtr, $this->ooScopeTokens ) !== false ) { |
|
309 | 309 | return; |
310 | 310 | } |
311 | 311 | |
312 | - $params = PHPCSHelper::getMethodParameters($phpcsFile, $stackPtr); |
|
313 | - if (empty($params)) { |
|
312 | + $params = PHPCSHelper::getMethodParameters( $phpcsFile, $stackPtr ); |
|
313 | + if ( empty( $params ) ) { |
|
314 | 314 | return; |
315 | 315 | } |
316 | 316 | |
317 | 317 | $tokens = $phpcsFile->getTokens(); |
318 | 318 | |
319 | - foreach ($params as $param) { |
|
320 | - if ($param['name'] !== '$this') { |
|
319 | + foreach ( $params as $param ) { |
|
320 | + if ( $param[ 'name' ] !== '$this' ) { |
|
321 | 321 | continue; |
322 | 322 | } |
323 | 323 | |
324 | - if ($tokens[$stackPtr]['code'] === \T_FUNCTION) { |
|
324 | + if ( $tokens[ $stackPtr ][ 'code' ] === \T_FUNCTION ) { |
|
325 | 325 | $phpcsFile->addError( |
326 | 326 | '"$this" can no longer be used as a parameter since PHP 7.1.', |
327 | - $param['token'], |
|
327 | + $param[ 'token' ], |
|
328 | 328 | 'FunctionParam' |
329 | 329 | ); |
330 | 330 | } else { |
331 | 331 | $phpcsFile->addError( |
332 | 332 | '"$this" can no longer be used as a closure parameter since PHP 7.0.7.', |
333 | - $param['token'], |
|
333 | + $param[ 'token' ], |
|
334 | 334 | 'ClosureParam' |
335 | 335 | ); |
336 | 336 | } |
@@ -355,21 +355,21 @@ discard block |
||
355 | 355 | * |
356 | 356 | * @return void |
357 | 357 | */ |
358 | - protected function isThisUsedOutsideObjectContext(File $phpcsFile, $stackPtr) |
|
358 | + protected function isThisUsedOutsideObjectContext( File $phpcsFile, $stackPtr ) |
|
359 | 359 | { |
360 | 360 | $tokens = $phpcsFile->getTokens(); |
361 | 361 | |
362 | - if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { |
|
362 | + if ( isset( $tokens[ $stackPtr ][ 'scope_opener' ], $tokens[ $stackPtr ][ 'scope_closer' ] ) === false ) { |
|
363 | 363 | return; |
364 | 364 | } |
365 | 365 | |
366 | - if ($this->validDirectScope($phpcsFile, $stackPtr, $this->ooScopeTokens) !== false) { |
|
367 | - $methodProps = $phpcsFile->getMethodProperties($stackPtr); |
|
368 | - if ($methodProps['is_static'] === false) { |
|
366 | + if ( $this->validDirectScope( $phpcsFile, $stackPtr, $this->ooScopeTokens ) !== false ) { |
|
367 | + $methodProps = $phpcsFile->getMethodProperties( $stackPtr ); |
|
368 | + if ( $methodProps[ 'is_static' ] === false ) { |
|
369 | 369 | return; |
370 | 370 | } else { |
371 | - $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
372 | - if ($methodName === '__call') { |
|
371 | + $methodName = $phpcsFile->getDeclarationName( $stackPtr ); |
|
372 | + if ( $methodName === '__call' ) { |
|
373 | 373 | /* |
374 | 374 | * This is an exception. |
375 | 375 | * @link https://wiki.php.net/rfc/this_var#always_show_true_this_value_in_magic_method_call |
@@ -379,37 +379,37 @@ discard block |
||
379 | 379 | } |
380 | 380 | } |
381 | 381 | |
382 | - for ($i = ($tokens[$stackPtr]['scope_opener'] + 1); $i < $tokens[$stackPtr]['scope_closer']; $i++) { |
|
383 | - if (isset($this->skipOverScopes[$tokens[$i]['type']])) { |
|
384 | - if (isset($tokens[$i]['scope_closer']) === false) { |
|
382 | + for ( $i = ( $tokens[ $stackPtr ][ 'scope_opener' ] + 1 ); $i < $tokens[ $stackPtr ][ 'scope_closer' ]; $i++ ) { |
|
383 | + if ( isset( $this->skipOverScopes[ $tokens[ $i ][ 'type' ] ] ) ) { |
|
384 | + if ( isset( $tokens[ $i ][ 'scope_closer' ] ) === false ) { |
|
385 | 385 | // Live coding or parse error, will only lead to inaccurate results. |
386 | 386 | return; |
387 | 387 | } |
388 | 388 | |
389 | 389 | // Skip over nested structures. |
390 | - $i = $tokens[$i]['scope_closer']; |
|
390 | + $i = $tokens[ $i ][ 'scope_closer' ]; |
|
391 | 391 | continue; |
392 | 392 | } |
393 | 393 | |
394 | - if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$this') { |
|
394 | + if ( $tokens[ $i ][ 'code' ] !== \T_VARIABLE || $tokens[ $i ][ 'content' ] !== '$this' ) { |
|
395 | 395 | continue; |
396 | 396 | } |
397 | 397 | |
398 | - if (isset($tokens[$i]['nested_parenthesis']) === true) { |
|
399 | - $nestedParenthesis = $tokens[$i]['nested_parenthesis']; |
|
400 | - $nestedOpenParenthesis = array_keys($nestedParenthesis); |
|
401 | - $lastOpenParenthesis = array_pop($nestedOpenParenthesis); |
|
398 | + if ( isset( $tokens[ $i ][ 'nested_parenthesis' ] ) === true ) { |
|
399 | + $nestedParenthesis = $tokens[ $i ][ 'nested_parenthesis' ]; |
|
400 | + $nestedOpenParenthesis = array_keys( $nestedParenthesis ); |
|
401 | + $lastOpenParenthesis = array_pop( $nestedOpenParenthesis ); |
|
402 | 402 | |
403 | 403 | $previousNonEmpty = $phpcsFile->findPrevious( |
404 | 404 | Tokens::$emptyTokens, |
405 | - ($lastOpenParenthesis - 1), |
|
405 | + ( $lastOpenParenthesis - 1 ), |
|
406 | 406 | null, |
407 | 407 | true, |
408 | 408 | null, |
409 | 409 | true |
410 | 410 | ); |
411 | 411 | |
412 | - if (isset($this->validUseOutsideObject[$tokens[$previousNonEmpty]['code']])) { |
|
412 | + if ( isset( $this->validUseOutsideObject[ $tokens[ $previousNonEmpty ][ 'code' ] ] ) ) { |
|
413 | 413 | continue; |
414 | 414 | } |
415 | 415 | } |
@@ -47,8 +47,7 @@ discard block |
||
47 | 47 | * |
48 | 48 | * @since 9.1.0 |
49 | 49 | */ |
50 | -class ForbiddenThisUseContextsSniff extends Sniff |
|
51 | -{ |
|
50 | +class ForbiddenThisUseContextsSniff extends Sniff { |
|
52 | 51 | |
53 | 52 | /** |
54 | 53 | * OO scope tokens. |
@@ -96,8 +95,7 @@ discard block |
||
96 | 95 | * |
97 | 96 | * @return array |
98 | 97 | */ |
99 | - public function register() |
|
100 | - { |
|
98 | + public function register() { |
|
101 | 99 | if (\defined('T_ANON_CLASS')) { |
102 | 100 | $this->ooScopeTokens['T_ANON_CLASS'] = \T_ANON_CLASS; |
103 | 101 | } |
@@ -125,8 +123,7 @@ discard block |
||
125 | 123 | * |
126 | 124 | * @return void |
127 | 125 | */ |
128 | - public function process(File $phpcsFile, $stackPtr) |
|
129 | - { |
|
126 | + public function process(File $phpcsFile, $stackPtr) { |
|
130 | 127 | if ($this->supportsAbove('7.1') === false) { |
131 | 128 | return; |
132 | 129 | } |
@@ -303,8 +300,7 @@ discard block |
||
303 | 300 | * |
304 | 301 | * @return void |
305 | 302 | */ |
306 | - protected function isThisUsedAsParameter(File $phpcsFile, $stackPtr) |
|
307 | - { |
|
303 | + protected function isThisUsedAsParameter(File $phpcsFile, $stackPtr) { |
|
308 | 304 | if ($this->validDirectScope($phpcsFile, $stackPtr, $this->ooScopeTokens) !== false) { |
309 | 305 | return; |
310 | 306 | } |
@@ -355,8 +351,7 @@ discard block |
||
355 | 351 | * |
356 | 352 | * @return void |
357 | 353 | */ |
358 | - protected function isThisUsedOutsideObjectContext(File $phpcsFile, $stackPtr) |
|
359 | - { |
|
354 | + protected function isThisUsedOutsideObjectContext(File $phpcsFile, $stackPtr) { |
|
360 | 355 | $tokens = $phpcsFile->getTokens(); |
361 | 356 | |
362 | 357 | if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { |
@@ -28,84 +28,84 @@ |
||
28 | 28 | */ |
29 | 29 | class NewUniformVariableSyntaxSniff extends Sniff |
30 | 30 | { |
31 | - /** |
|
32 | - * Returns an array of tokens this test wants to listen for. |
|
33 | - * |
|
34 | - * @return array |
|
35 | - */ |
|
36 | - public function register() |
|
37 | - { |
|
38 | - return array(\T_VARIABLE); |
|
39 | - } |
|
31 | + /** |
|
32 | + * Returns an array of tokens this test wants to listen for. |
|
33 | + * |
|
34 | + * @return array |
|
35 | + */ |
|
36 | + public function register() |
|
37 | + { |
|
38 | + return array(\T_VARIABLE); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Processes this test, when one of its tokens is encountered. |
|
43 | - * |
|
44 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
45 | - * @param int $stackPtr The position of the current token |
|
46 | - * in the stack passed in $tokens. |
|
47 | - * |
|
48 | - * @return void |
|
49 | - */ |
|
50 | - public function process(File $phpcsFile, $stackPtr) |
|
51 | - { |
|
52 | - if ($this->supportsAbove('7.0') === false) { |
|
53 | - return; |
|
54 | - } |
|
41 | + /** |
|
42 | + * Processes this test, when one of its tokens is encountered. |
|
43 | + * |
|
44 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
45 | + * @param int $stackPtr The position of the current token |
|
46 | + * in the stack passed in $tokens. |
|
47 | + * |
|
48 | + * @return void |
|
49 | + */ |
|
50 | + public function process(File $phpcsFile, $stackPtr) |
|
51 | + { |
|
52 | + if ($this->supportsAbove('7.0') === false) { |
|
53 | + return; |
|
54 | + } |
|
55 | 55 | |
56 | - $tokens = $phpcsFile->getTokens(); |
|
56 | + $tokens = $phpcsFile->getTokens(); |
|
57 | 57 | |
58 | - // Verify that the next token is a square open bracket. If not, bow out. |
|
59 | - $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); |
|
58 | + // Verify that the next token is a square open bracket. If not, bow out. |
|
59 | + $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); |
|
60 | 60 | |
61 | - if ($nextToken === false || $tokens[$nextToken]['code'] !== \T_OPEN_SQUARE_BRACKET || isset($tokens[$nextToken]['bracket_closer']) === false) { |
|
62 | - return; |
|
63 | - } |
|
61 | + if ($nextToken === false || $tokens[$nextToken]['code'] !== \T_OPEN_SQUARE_BRACKET || isset($tokens[$nextToken]['bracket_closer']) === false) { |
|
62 | + return; |
|
63 | + } |
|
64 | 64 | |
65 | - // The previous non-empty token has to be a $, -> or ::. |
|
66 | - $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
|
67 | - if ($prevToken === false || \in_array($tokens[$prevToken]['code'], array(\T_DOLLAR, \T_OBJECT_OPERATOR, \T_DOUBLE_COLON), true) === false) { |
|
68 | - return; |
|
69 | - } |
|
65 | + // The previous non-empty token has to be a $, -> or ::. |
|
66 | + $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
|
67 | + if ($prevToken === false || \in_array($tokens[$prevToken]['code'], array(\T_DOLLAR, \T_OBJECT_OPERATOR, \T_DOUBLE_COLON), true) === false) { |
|
68 | + return; |
|
69 | + } |
|
70 | 70 | |
71 | - // For static object calls, it only applies when this is a function call. |
|
72 | - if ($tokens[$prevToken]['code'] === \T_DOUBLE_COLON) { |
|
73 | - $hasBrackets = $tokens[$nextToken]['bracket_closer']; |
|
74 | - while (($hasBrackets = $phpcsFile->findNext(Tokens::$emptyTokens, ($hasBrackets + 1), null, true, null, true)) !== false) { |
|
75 | - if ($tokens[$hasBrackets]['code'] === \T_OPEN_SQUARE_BRACKET) { |
|
76 | - if (isset($tokens[$hasBrackets]['bracket_closer'])) { |
|
77 | - $hasBrackets = $tokens[$hasBrackets]['bracket_closer']; |
|
78 | - continue; |
|
79 | - } else { |
|
80 | - // Live coding. |
|
81 | - return; |
|
82 | - } |
|
71 | + // For static object calls, it only applies when this is a function call. |
|
72 | + if ($tokens[$prevToken]['code'] === \T_DOUBLE_COLON) { |
|
73 | + $hasBrackets = $tokens[$nextToken]['bracket_closer']; |
|
74 | + while (($hasBrackets = $phpcsFile->findNext(Tokens::$emptyTokens, ($hasBrackets + 1), null, true, null, true)) !== false) { |
|
75 | + if ($tokens[$hasBrackets]['code'] === \T_OPEN_SQUARE_BRACKET) { |
|
76 | + if (isset($tokens[$hasBrackets]['bracket_closer'])) { |
|
77 | + $hasBrackets = $tokens[$hasBrackets]['bracket_closer']; |
|
78 | + continue; |
|
79 | + } else { |
|
80 | + // Live coding. |
|
81 | + return; |
|
82 | + } |
|
83 | 83 | |
84 | - } elseif ($tokens[$hasBrackets]['code'] === \T_OPEN_PARENTHESIS) { |
|
85 | - // Caught! |
|
86 | - break; |
|
84 | + } elseif ($tokens[$hasBrackets]['code'] === \T_OPEN_PARENTHESIS) { |
|
85 | + // Caught! |
|
86 | + break; |
|
87 | 87 | |
88 | - } else { |
|
89 | - // Not a function call, so bow out. |
|
90 | - return; |
|
91 | - } |
|
92 | - } |
|
88 | + } else { |
|
89 | + // Not a function call, so bow out. |
|
90 | + return; |
|
91 | + } |
|
92 | + } |
|
93 | 93 | |
94 | - // Now let's also prevent false positives when used with self and static which still work fine. |
|
95 | - $classToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevToken - 1), null, true, null, true); |
|
96 | - if ($classToken !== false) { |
|
97 | - if ($tokens[$classToken]['code'] === \T_STATIC || $tokens[$classToken]['code'] === \T_SELF) { |
|
98 | - return; |
|
99 | - } elseif ($tokens[$classToken]['code'] === \T_STRING && $tokens[$classToken]['content'] === 'self') { |
|
100 | - return; |
|
101 | - } |
|
102 | - } |
|
103 | - } |
|
94 | + // Now let's also prevent false positives when used with self and static which still work fine. |
|
95 | + $classToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevToken - 1), null, true, null, true); |
|
96 | + if ($classToken !== false) { |
|
97 | + if ($tokens[$classToken]['code'] === \T_STATIC || $tokens[$classToken]['code'] === \T_SELF) { |
|
98 | + return; |
|
99 | + } elseif ($tokens[$classToken]['code'] === \T_STRING && $tokens[$classToken]['content'] === 'self') { |
|
100 | + return; |
|
101 | + } |
|
102 | + } |
|
103 | + } |
|
104 | 104 | |
105 | - $phpcsFile->addError( |
|
106 | - 'Indirect access to variables, properties and methods will be evaluated strictly in left-to-right order since PHP 7.0. Use curly braces to remove ambiguity.', |
|
107 | - $stackPtr, |
|
108 | - 'Found' |
|
109 | - ); |
|
110 | - } |
|
105 | + $phpcsFile->addError( |
|
106 | + 'Indirect access to variables, properties and methods will be evaluated strictly in left-to-right order since PHP 7.0. Use curly braces to remove ambiguity.', |
|
107 | + $stackPtr, |
|
108 | + 'Found' |
|
109 | + ); |
|
110 | + } |
|
111 | 111 | } |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | */ |
36 | 36 | public function register() |
37 | 37 | { |
38 | - return array(\T_VARIABLE); |
|
38 | + return array( \T_VARIABLE ); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | /** |
@@ -47,41 +47,41 @@ discard block |
||
47 | 47 | * |
48 | 48 | * @return void |
49 | 49 | */ |
50 | - public function process(File $phpcsFile, $stackPtr) |
|
50 | + public function process( File $phpcsFile, $stackPtr ) |
|
51 | 51 | { |
52 | - if ($this->supportsAbove('7.0') === false) { |
|
52 | + if ( $this->supportsAbove( '7.0' ) === false ) { |
|
53 | 53 | return; |
54 | 54 | } |
55 | 55 | |
56 | 56 | $tokens = $phpcsFile->getTokens(); |
57 | 57 | |
58 | 58 | // Verify that the next token is a square open bracket. If not, bow out. |
59 | - $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); |
|
59 | + $nextToken = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); |
|
60 | 60 | |
61 | - if ($nextToken === false || $tokens[$nextToken]['code'] !== \T_OPEN_SQUARE_BRACKET || isset($tokens[$nextToken]['bracket_closer']) === false) { |
|
61 | + if ( $nextToken === false || $tokens[ $nextToken ][ 'code' ] !== \T_OPEN_SQUARE_BRACKET || isset( $tokens[ $nextToken ][ 'bracket_closer' ] ) === false ) { |
|
62 | 62 | return; |
63 | 63 | } |
64 | 64 | |
65 | 65 | // The previous non-empty token has to be a $, -> or ::. |
66 | - $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
|
67 | - if ($prevToken === false || \in_array($tokens[$prevToken]['code'], array(\T_DOLLAR, \T_OBJECT_OPERATOR, \T_DOUBLE_COLON), true) === false) { |
|
66 | + $prevToken = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true, null, true ); |
|
67 | + if ( $prevToken === false || \in_array( $tokens[ $prevToken ][ 'code' ], array( \T_DOLLAR, \T_OBJECT_OPERATOR, \T_DOUBLE_COLON ), true ) === false ) { |
|
68 | 68 | return; |
69 | 69 | } |
70 | 70 | |
71 | 71 | // For static object calls, it only applies when this is a function call. |
72 | - if ($tokens[$prevToken]['code'] === \T_DOUBLE_COLON) { |
|
73 | - $hasBrackets = $tokens[$nextToken]['bracket_closer']; |
|
74 | - while (($hasBrackets = $phpcsFile->findNext(Tokens::$emptyTokens, ($hasBrackets + 1), null, true, null, true)) !== false) { |
|
75 | - if ($tokens[$hasBrackets]['code'] === \T_OPEN_SQUARE_BRACKET) { |
|
76 | - if (isset($tokens[$hasBrackets]['bracket_closer'])) { |
|
77 | - $hasBrackets = $tokens[$hasBrackets]['bracket_closer']; |
|
72 | + if ( $tokens[ $prevToken ][ 'code' ] === \T_DOUBLE_COLON ) { |
|
73 | + $hasBrackets = $tokens[ $nextToken ][ 'bracket_closer' ]; |
|
74 | + while ( ( $hasBrackets = $phpcsFile->findNext( Tokens::$emptyTokens, ( $hasBrackets + 1 ), null, true, null, true ) ) !== false ) { |
|
75 | + if ( $tokens[ $hasBrackets ][ 'code' ] === \T_OPEN_SQUARE_BRACKET ) { |
|
76 | + if ( isset( $tokens[ $hasBrackets ][ 'bracket_closer' ] ) ) { |
|
77 | + $hasBrackets = $tokens[ $hasBrackets ][ 'bracket_closer' ]; |
|
78 | 78 | continue; |
79 | 79 | } else { |
80 | 80 | // Live coding. |
81 | 81 | return; |
82 | 82 | } |
83 | 83 | |
84 | - } elseif ($tokens[$hasBrackets]['code'] === \T_OPEN_PARENTHESIS) { |
|
84 | + } elseif ( $tokens[ $hasBrackets ][ 'code' ] === \T_OPEN_PARENTHESIS ) { |
|
85 | 85 | // Caught! |
86 | 86 | break; |
87 | 87 | |
@@ -92,11 +92,11 @@ discard block |
||
92 | 92 | } |
93 | 93 | |
94 | 94 | // Now let's also prevent false positives when used with self and static which still work fine. |
95 | - $classToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevToken - 1), null, true, null, true); |
|
96 | - if ($classToken !== false) { |
|
97 | - if ($tokens[$classToken]['code'] === \T_STATIC || $tokens[$classToken]['code'] === \T_SELF) { |
|
95 | + $classToken = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $prevToken - 1 ), null, true, null, true ); |
|
96 | + if ( $classToken !== false ) { |
|
97 | + if ( $tokens[ $classToken ][ 'code' ] === \T_STATIC || $tokens[ $classToken ][ 'code' ] === \T_SELF ) { |
|
98 | 98 | return; |
99 | - } elseif ($tokens[$classToken]['code'] === \T_STRING && $tokens[$classToken]['content'] === 'self') { |
|
99 | + } elseif ( $tokens[ $classToken ][ 'code' ] === \T_STRING && $tokens[ $classToken ][ 'content' ] === 'self' ) { |
|
100 | 100 | return; |
101 | 101 | } |
102 | 102 | } |
@@ -26,15 +26,13 @@ discard block |
||
26 | 26 | * @package PHPCompatibility |
27 | 27 | * @author Juliette Reinders Folmer <[email protected]> |
28 | 28 | */ |
29 | -class NewUniformVariableSyntaxSniff extends Sniff |
|
30 | -{ |
|
29 | +class NewUniformVariableSyntaxSniff extends Sniff { |
|
31 | 30 | /** |
32 | 31 | * Returns an array of tokens this test wants to listen for. |
33 | 32 | * |
34 | 33 | * @return array |
35 | 34 | */ |
36 | - public function register() |
|
37 | - { |
|
35 | + public function register() { |
|
38 | 36 | return array(\T_VARIABLE); |
39 | 37 | } |
40 | 38 | |
@@ -47,8 +45,7 @@ discard block |
||
47 | 45 | * |
48 | 46 | * @return void |
49 | 47 | */ |
50 | - public function process(File $phpcsFile, $stackPtr) |
|
51 | - { |
|
48 | + public function process(File $phpcsFile, $stackPtr) { |
|
52 | 49 | if ($this->supportsAbove('7.0') === false) { |
53 | 50 | return; |
54 | 51 | } |
@@ -65,7 +65,7 @@ |
||
65 | 65 | /** |
66 | 66 | * Returns an array of tokens this test wants to listen for. |
67 | 67 | * |
68 | - * @return array |
|
68 | + * @return integer[] |
|
69 | 69 | */ |
70 | 70 | public function register() |
71 | 71 | { |
@@ -29,96 +29,96 @@ |
||
29 | 29 | class ForbiddenGlobalVariableVariableSniff extends Sniff |
30 | 30 | { |
31 | 31 | |
32 | - /** |
|
33 | - * Returns an array of tokens this test wants to listen for. |
|
34 | - * |
|
35 | - * @return array |
|
36 | - */ |
|
37 | - public function register() |
|
38 | - { |
|
39 | - return array(\T_GLOBAL); |
|
40 | - } |
|
32 | + /** |
|
33 | + * Returns an array of tokens this test wants to listen for. |
|
34 | + * |
|
35 | + * @return array |
|
36 | + */ |
|
37 | + public function register() |
|
38 | + { |
|
39 | + return array(\T_GLOBAL); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Processes this test, when one of its tokens is encountered. |
|
44 | - * |
|
45 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
46 | - * @param int $stackPtr The position of the current token in the |
|
47 | - * stack passed in $tokens. |
|
48 | - * |
|
49 | - * @return void |
|
50 | - */ |
|
51 | - public function process(File $phpcsFile, $stackPtr) |
|
52 | - { |
|
53 | - if ($this->supportsAbove('7.0') === false) { |
|
54 | - return; |
|
55 | - } |
|
42 | + /** |
|
43 | + * Processes this test, when one of its tokens is encountered. |
|
44 | + * |
|
45 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
46 | + * @param int $stackPtr The position of the current token in the |
|
47 | + * stack passed in $tokens. |
|
48 | + * |
|
49 | + * @return void |
|
50 | + */ |
|
51 | + public function process(File $phpcsFile, $stackPtr) |
|
52 | + { |
|
53 | + if ($this->supportsAbove('7.0') === false) { |
|
54 | + return; |
|
55 | + } |
|
56 | 56 | |
57 | - $tokens = $phpcsFile->getTokens(); |
|
58 | - $endOfStatement = $phpcsFile->findNext(array(\T_SEMICOLON, \T_CLOSE_TAG), ($stackPtr + 1)); |
|
59 | - if ($endOfStatement === false) { |
|
60 | - // No semi-colon - live coding. |
|
61 | - return; |
|
62 | - } |
|
57 | + $tokens = $phpcsFile->getTokens(); |
|
58 | + $endOfStatement = $phpcsFile->findNext(array(\T_SEMICOLON, \T_CLOSE_TAG), ($stackPtr + 1)); |
|
59 | + if ($endOfStatement === false) { |
|
60 | + // No semi-colon - live coding. |
|
61 | + return; |
|
62 | + } |
|
63 | 63 | |
64 | - for ($ptr = ($stackPtr + 1); $ptr <= $endOfStatement; $ptr++) { |
|
65 | - $errorThrown = false; |
|
66 | - $nextComma = $phpcsFile->findNext(\T_COMMA, $ptr, $endOfStatement, false, null, true); |
|
67 | - $varEnd = ($nextComma === false) ? $endOfStatement : $nextComma; |
|
68 | - $variable = $phpcsFile->findNext(\T_VARIABLE, $ptr, $varEnd); |
|
69 | - $varString = trim($phpcsFile->getTokensAsString($ptr, ($varEnd - $ptr))); |
|
70 | - $data = array($varString); |
|
64 | + for ($ptr = ($stackPtr + 1); $ptr <= $endOfStatement; $ptr++) { |
|
65 | + $errorThrown = false; |
|
66 | + $nextComma = $phpcsFile->findNext(\T_COMMA, $ptr, $endOfStatement, false, null, true); |
|
67 | + $varEnd = ($nextComma === false) ? $endOfStatement : $nextComma; |
|
68 | + $variable = $phpcsFile->findNext(\T_VARIABLE, $ptr, $varEnd); |
|
69 | + $varString = trim($phpcsFile->getTokensAsString($ptr, ($varEnd - $ptr))); |
|
70 | + $data = array($varString); |
|
71 | 71 | |
72 | - if ($variable !== false) { |
|
72 | + if ($variable !== false) { |
|
73 | 73 | |
74 | - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($variable - 1), $ptr, true); |
|
74 | + $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($variable - 1), $ptr, true); |
|
75 | 75 | |
76 | - if ($prev !== false && $tokens[$prev]['type'] === 'T_DOLLAR') { |
|
76 | + if ($prev !== false && $tokens[$prev]['type'] === 'T_DOLLAR') { |
|
77 | 77 | |
78 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($variable + 1), $varEnd, true); |
|
78 | + $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($variable + 1), $varEnd, true); |
|
79 | 79 | |
80 | - if ($next !== false |
|
81 | - && \in_array($tokens[$next]['code'], array(\T_OPEN_SQUARE_BRACKET, \T_OBJECT_OPERATOR, \T_DOUBLE_COLON), true) === true |
|
82 | - ) { |
|
83 | - $phpcsFile->addError( |
|
84 | - 'Global with variable variables is not allowed since PHP 7.0. Found %s', |
|
85 | - $variable, |
|
86 | - 'Found', |
|
87 | - $data |
|
88 | - ); |
|
89 | - $errorThrown = true; |
|
90 | - } else { |
|
91 | - $phpcsFile->addWarning( |
|
92 | - 'Global with anything other than bare variables is discouraged since PHP 7.0. Found %s', |
|
93 | - $variable, |
|
94 | - 'NonBareVariableFound', |
|
95 | - $data |
|
96 | - ); |
|
97 | - $errorThrown = true; |
|
98 | - } |
|
99 | - } |
|
100 | - } |
|
80 | + if ($next !== false |
|
81 | + && \in_array($tokens[$next]['code'], array(\T_OPEN_SQUARE_BRACKET, \T_OBJECT_OPERATOR, \T_DOUBLE_COLON), true) === true |
|
82 | + ) { |
|
83 | + $phpcsFile->addError( |
|
84 | + 'Global with variable variables is not allowed since PHP 7.0. Found %s', |
|
85 | + $variable, |
|
86 | + 'Found', |
|
87 | + $data |
|
88 | + ); |
|
89 | + $errorThrown = true; |
|
90 | + } else { |
|
91 | + $phpcsFile->addWarning( |
|
92 | + 'Global with anything other than bare variables is discouraged since PHP 7.0. Found %s', |
|
93 | + $variable, |
|
94 | + 'NonBareVariableFound', |
|
95 | + $data |
|
96 | + ); |
|
97 | + $errorThrown = true; |
|
98 | + } |
|
99 | + } |
|
100 | + } |
|
101 | 101 | |
102 | - if ($errorThrown === false) { |
|
103 | - $dollar = $phpcsFile->findNext(\T_DOLLAR, $ptr, $varEnd); |
|
104 | - if ($dollar !== false) { |
|
105 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($dollar + 1), $varEnd, true); |
|
106 | - if ($tokens[$next]['code'] === \T_OPEN_CURLY_BRACKET) { |
|
107 | - $phpcsFile->addWarning( |
|
108 | - 'Global with anything other than bare variables is discouraged since PHP 7.0. Found %s', |
|
109 | - $dollar, |
|
110 | - 'NonBareVariableFound', |
|
111 | - $data |
|
112 | - ); |
|
113 | - } |
|
114 | - } |
|
115 | - } |
|
102 | + if ($errorThrown === false) { |
|
103 | + $dollar = $phpcsFile->findNext(\T_DOLLAR, $ptr, $varEnd); |
|
104 | + if ($dollar !== false) { |
|
105 | + $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($dollar + 1), $varEnd, true); |
|
106 | + if ($tokens[$next]['code'] === \T_OPEN_CURLY_BRACKET) { |
|
107 | + $phpcsFile->addWarning( |
|
108 | + 'Global with anything other than bare variables is discouraged since PHP 7.0. Found %s', |
|
109 | + $dollar, |
|
110 | + 'NonBareVariableFound', |
|
111 | + $data |
|
112 | + ); |
|
113 | + } |
|
114 | + } |
|
115 | + } |
|
116 | 116 | |
117 | - // Move the stack pointer forward to the next variable for multi-variable statements. |
|
118 | - if ($nextComma === false) { |
|
119 | - break; |
|
120 | - } |
|
121 | - $ptr = $nextComma; |
|
122 | - } |
|
123 | - } |
|
117 | + // Move the stack pointer forward to the next variable for multi-variable statements. |
|
118 | + if ($nextComma === false) { |
|
119 | + break; |
|
120 | + } |
|
121 | + $ptr = $nextComma; |
|
122 | + } |
|
123 | + } |
|
124 | 124 | } |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | */ |
37 | 37 | public function register() |
38 | 38 | { |
39 | - return array(\T_GLOBAL); |
|
39 | + return array( \T_GLOBAL ); |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | /** |
@@ -48,37 +48,37 @@ discard block |
||
48 | 48 | * |
49 | 49 | * @return void |
50 | 50 | */ |
51 | - public function process(File $phpcsFile, $stackPtr) |
|
51 | + public function process( File $phpcsFile, $stackPtr ) |
|
52 | 52 | { |
53 | - if ($this->supportsAbove('7.0') === false) { |
|
53 | + if ( $this->supportsAbove( '7.0' ) === false ) { |
|
54 | 54 | return; |
55 | 55 | } |
56 | 56 | |
57 | 57 | $tokens = $phpcsFile->getTokens(); |
58 | - $endOfStatement = $phpcsFile->findNext(array(\T_SEMICOLON, \T_CLOSE_TAG), ($stackPtr + 1)); |
|
59 | - if ($endOfStatement === false) { |
|
58 | + $endOfStatement = $phpcsFile->findNext( array( \T_SEMICOLON, \T_CLOSE_TAG ), ( $stackPtr + 1 ) ); |
|
59 | + if ( $endOfStatement === false ) { |
|
60 | 60 | // No semi-colon - live coding. |
61 | 61 | return; |
62 | 62 | } |
63 | 63 | |
64 | - for ($ptr = ($stackPtr + 1); $ptr <= $endOfStatement; $ptr++) { |
|
64 | + for ( $ptr = ( $stackPtr + 1 ); $ptr <= $endOfStatement; $ptr++ ) { |
|
65 | 65 | $errorThrown = false; |
66 | - $nextComma = $phpcsFile->findNext(\T_COMMA, $ptr, $endOfStatement, false, null, true); |
|
67 | - $varEnd = ($nextComma === false) ? $endOfStatement : $nextComma; |
|
68 | - $variable = $phpcsFile->findNext(\T_VARIABLE, $ptr, $varEnd); |
|
69 | - $varString = trim($phpcsFile->getTokensAsString($ptr, ($varEnd - $ptr))); |
|
70 | - $data = array($varString); |
|
66 | + $nextComma = $phpcsFile->findNext( \T_COMMA, $ptr, $endOfStatement, false, null, true ); |
|
67 | + $varEnd = ( $nextComma === false ) ? $endOfStatement : $nextComma; |
|
68 | + $variable = $phpcsFile->findNext( \T_VARIABLE, $ptr, $varEnd ); |
|
69 | + $varString = trim( $phpcsFile->getTokensAsString( $ptr, ( $varEnd - $ptr ) ) ); |
|
70 | + $data = array( $varString ); |
|
71 | 71 | |
72 | - if ($variable !== false) { |
|
72 | + if ( $variable !== false ) { |
|
73 | 73 | |
74 | - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($variable - 1), $ptr, true); |
|
74 | + $prev = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $variable - 1 ), $ptr, true ); |
|
75 | 75 | |
76 | - if ($prev !== false && $tokens[$prev]['type'] === 'T_DOLLAR') { |
|
76 | + if ( $prev !== false && $tokens[ $prev ][ 'type' ] === 'T_DOLLAR' ) { |
|
77 | 77 | |
78 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($variable + 1), $varEnd, true); |
|
78 | + $next = $phpcsFile->findNext( Tokens::$emptyTokens, ( $variable + 1 ), $varEnd, true ); |
|
79 | 79 | |
80 | - if ($next !== false |
|
81 | - && \in_array($tokens[$next]['code'], array(\T_OPEN_SQUARE_BRACKET, \T_OBJECT_OPERATOR, \T_DOUBLE_COLON), true) === true |
|
80 | + if ( $next !== false |
|
81 | + && \in_array( $tokens[ $next ][ 'code' ], array( \T_OPEN_SQUARE_BRACKET, \T_OBJECT_OPERATOR, \T_DOUBLE_COLON ), true ) === true |
|
82 | 82 | ) { |
83 | 83 | $phpcsFile->addError( |
84 | 84 | 'Global with variable variables is not allowed since PHP 7.0. Found %s', |
@@ -99,11 +99,11 @@ discard block |
||
99 | 99 | } |
100 | 100 | } |
101 | 101 | |
102 | - if ($errorThrown === false) { |
|
103 | - $dollar = $phpcsFile->findNext(\T_DOLLAR, $ptr, $varEnd); |
|
104 | - if ($dollar !== false) { |
|
105 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($dollar + 1), $varEnd, true); |
|
106 | - if ($tokens[$next]['code'] === \T_OPEN_CURLY_BRACKET) { |
|
102 | + if ( $errorThrown === false ) { |
|
103 | + $dollar = $phpcsFile->findNext( \T_DOLLAR, $ptr, $varEnd ); |
|
104 | + if ( $dollar !== false ) { |
|
105 | + $next = $phpcsFile->findNext( Tokens::$emptyTokens, ( $dollar + 1 ), $varEnd, true ); |
|
106 | + if ( $tokens[ $next ][ 'code' ] === \T_OPEN_CURLY_BRACKET ) { |
|
107 | 107 | $phpcsFile->addWarning( |
108 | 108 | 'Global with anything other than bare variables is discouraged since PHP 7.0. Found %s', |
109 | 109 | $dollar, |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | } |
116 | 116 | |
117 | 117 | // Move the stack pointer forward to the next variable for multi-variable statements. |
118 | - if ($nextComma === false) { |
|
118 | + if ( $nextComma === false ) { |
|
119 | 119 | break; |
120 | 120 | } |
121 | 121 | $ptr = $nextComma; |
@@ -26,16 +26,14 @@ discard block |
||
26 | 26 | * @package PHPCompatibility |
27 | 27 | * @author Wim Godden <[email protected]> |
28 | 28 | */ |
29 | -class ForbiddenGlobalVariableVariableSniff extends Sniff |
|
30 | -{ |
|
29 | +class ForbiddenGlobalVariableVariableSniff extends Sniff { |
|
31 | 30 | |
32 | 31 | /** |
33 | 32 | * Returns an array of tokens this test wants to listen for. |
34 | 33 | * |
35 | 34 | * @return array |
36 | 35 | */ |
37 | - public function register() |
|
38 | - { |
|
36 | + public function register() { |
|
39 | 37 | return array(\T_GLOBAL); |
40 | 38 | } |
41 | 39 | |
@@ -48,8 +46,7 @@ discard block |
||
48 | 46 | * |
49 | 47 | * @return void |
50 | 48 | */ |
51 | - public function process(File $phpcsFile, $stackPtr) |
|
52 | - { |
|
49 | + public function process(File $phpcsFile, $stackPtr) { |
|
53 | 50 | if ($this->supportsAbove('7.0') === false) { |
54 | 51 | return; |
55 | 52 | } |
@@ -65,7 +65,7 @@ |
||
65 | 65 | /** |
66 | 66 | * Returns an array of tokens this test wants to listen for. |
67 | 67 | * |
68 | - * @return array |
|
68 | + * @return integer[] |
|
69 | 69 | */ |
70 | 70 | public function register() |
71 | 71 | { |
@@ -26,268 +26,268 @@ |
||
26 | 26 | class RemovedPredefinedGlobalVariablesSniff extends AbstractRemovedFeatureSniff |
27 | 27 | { |
28 | 28 | |
29 | - /** |
|
30 | - * A list of removed global variables with their alternative, if any. |
|
31 | - * |
|
32 | - * The array lists : version number with false (deprecated) and true (removed). |
|
33 | - * If's sufficient to list the first version where the variable was deprecated/removed. |
|
34 | - * |
|
35 | - * @var array(string|null) |
|
36 | - */ |
|
37 | - protected $removedGlobalVariables = array( |
|
38 | - 'HTTP_POST_VARS' => array( |
|
39 | - '5.3' => false, |
|
40 | - '5.4' => true, |
|
41 | - 'alternative' => '$_POST', |
|
42 | - ), |
|
43 | - 'HTTP_GET_VARS' => array( |
|
44 | - '5.3' => false, |
|
45 | - '5.4' => true, |
|
46 | - 'alternative' => '$_GET', |
|
47 | - ), |
|
48 | - 'HTTP_ENV_VARS' => array( |
|
49 | - '5.3' => false, |
|
50 | - '5.4' => true, |
|
51 | - 'alternative' => '$_ENV', |
|
52 | - ), |
|
53 | - 'HTTP_SERVER_VARS' => array( |
|
54 | - '5.3' => false, |
|
55 | - '5.4' => true, |
|
56 | - 'alternative' => '$_SERVER', |
|
57 | - ), |
|
58 | - 'HTTP_COOKIE_VARS' => array( |
|
59 | - '5.3' => false, |
|
60 | - '5.4' => true, |
|
61 | - 'alternative' => '$_COOKIE', |
|
62 | - ), |
|
63 | - 'HTTP_SESSION_VARS' => array( |
|
64 | - '5.3' => false, |
|
65 | - '5.4' => true, |
|
66 | - 'alternative' => '$_SESSION', |
|
67 | - ), |
|
68 | - 'HTTP_POST_FILES' => array( |
|
69 | - '5.3' => false, |
|
70 | - '5.4' => true, |
|
71 | - 'alternative' => '$_FILES', |
|
72 | - ), |
|
73 | - |
|
74 | - 'HTTP_RAW_POST_DATA' => array( |
|
75 | - '5.6' => false, |
|
76 | - '7.0' => true, |
|
77 | - 'alternative' => 'php://input', |
|
78 | - ), |
|
79 | - |
|
80 | - 'php_errormsg' => array( |
|
81 | - '7.2' => false, |
|
82 | - 'alternative' => 'error_get_last()', |
|
83 | - ), |
|
84 | - ); |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * Returns an array of tokens this test wants to listen for. |
|
89 | - * |
|
90 | - * @return array |
|
91 | - */ |
|
92 | - public function register() |
|
93 | - { |
|
94 | - return array(\T_VARIABLE); |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * Processes this test, when one of its tokens is encountered. |
|
100 | - * |
|
101 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
102 | - * @param int $stackPtr The position of the current token in the |
|
103 | - * stack passed in $tokens. |
|
104 | - * |
|
105 | - * @return void |
|
106 | - */ |
|
107 | - public function process(File $phpcsFile, $stackPtr) |
|
108 | - { |
|
109 | - if ($this->supportsAbove('5.3') === false) { |
|
110 | - return; |
|
111 | - } |
|
112 | - |
|
113 | - $tokens = $phpcsFile->getTokens(); |
|
114 | - $varName = substr($tokens[$stackPtr]['content'], 1); |
|
115 | - |
|
116 | - if (isset($this->removedGlobalVariables[$varName]) === false) { |
|
117 | - return; |
|
118 | - } |
|
119 | - |
|
120 | - if ($this->isClassProperty($phpcsFile, $stackPtr) === true) { |
|
121 | - // Ok, so this was a class property declaration, not our concern. |
|
122 | - return; |
|
123 | - } |
|
124 | - |
|
125 | - // Check for static usage of class properties shadowing the removed global variables. |
|
126 | - if ($this->inClassScope($phpcsFile, $stackPtr, false) === true) { |
|
127 | - $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
|
128 | - if ($prevToken !== false && $tokens[$prevToken]['code'] === \T_DOUBLE_COLON) { |
|
129 | - return; |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - // Do some additional checks for the $php_errormsg variable. |
|
134 | - if ($varName === 'php_errormsg' |
|
135 | - && $this->isTargetPHPErrormsgVar($phpcsFile, $stackPtr, $tokens) === false |
|
136 | - ) { |
|
137 | - return; |
|
138 | - } |
|
139 | - |
|
140 | - // Still here, so throw an error/warning. |
|
141 | - $itemInfo = array( |
|
142 | - 'name' => $varName, |
|
143 | - ); |
|
144 | - $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * Get the relevant sub-array for a specific item from a multi-dimensional array. |
|
150 | - * |
|
151 | - * @param array $itemInfo Base information about the item. |
|
152 | - * |
|
153 | - * @return array Version and other information about the item. |
|
154 | - */ |
|
155 | - public function getItemArray(array $itemInfo) |
|
156 | - { |
|
157 | - return $this->removedGlobalVariables[$itemInfo['name']]; |
|
158 | - } |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * Get the error message template for this sniff. |
|
163 | - * |
|
164 | - * @return string |
|
165 | - */ |
|
166 | - protected function getErrorMsgTemplate() |
|
167 | - { |
|
168 | - return "Global variable '\$%s' is "; |
|
169 | - } |
|
170 | - |
|
171 | - |
|
172 | - /** |
|
173 | - * Filter the error message before it's passed to PHPCS. |
|
174 | - * |
|
175 | - * @param string $error The error message which was created. |
|
176 | - * @param array $itemInfo Base information about the item this error message applies to. |
|
177 | - * @param array $errorInfo Detail information about an item this error message applies to. |
|
178 | - * |
|
179 | - * @return string |
|
180 | - */ |
|
181 | - protected function filterErrorMsg($error, array $itemInfo, array $errorInfo) |
|
182 | - { |
|
183 | - if ($itemInfo['name'] === 'php_errormsg') { |
|
184 | - $error = str_replace('Global', 'The', $error); |
|
185 | - } |
|
186 | - return $error; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Run some additional checks for the `$php_errormsg` variable. |
|
191 | - * |
|
192 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
193 | - * @param int $stackPtr The position of the current token in the |
|
194 | - * stack passed in $tokens. |
|
195 | - * @param array $tokens Token array of the current file. |
|
196 | - * |
|
197 | - * @return bool |
|
198 | - */ |
|
199 | - private function isTargetPHPErrormsgVar(File $phpcsFile, $stackPtr, array $tokens) |
|
200 | - { |
|
201 | - $scopeStart = 0; |
|
202 | - |
|
203 | - /* |
|
29 | + /** |
|
30 | + * A list of removed global variables with their alternative, if any. |
|
31 | + * |
|
32 | + * The array lists : version number with false (deprecated) and true (removed). |
|
33 | + * If's sufficient to list the first version where the variable was deprecated/removed. |
|
34 | + * |
|
35 | + * @var array(string|null) |
|
36 | + */ |
|
37 | + protected $removedGlobalVariables = array( |
|
38 | + 'HTTP_POST_VARS' => array( |
|
39 | + '5.3' => false, |
|
40 | + '5.4' => true, |
|
41 | + 'alternative' => '$_POST', |
|
42 | + ), |
|
43 | + 'HTTP_GET_VARS' => array( |
|
44 | + '5.3' => false, |
|
45 | + '5.4' => true, |
|
46 | + 'alternative' => '$_GET', |
|
47 | + ), |
|
48 | + 'HTTP_ENV_VARS' => array( |
|
49 | + '5.3' => false, |
|
50 | + '5.4' => true, |
|
51 | + 'alternative' => '$_ENV', |
|
52 | + ), |
|
53 | + 'HTTP_SERVER_VARS' => array( |
|
54 | + '5.3' => false, |
|
55 | + '5.4' => true, |
|
56 | + 'alternative' => '$_SERVER', |
|
57 | + ), |
|
58 | + 'HTTP_COOKIE_VARS' => array( |
|
59 | + '5.3' => false, |
|
60 | + '5.4' => true, |
|
61 | + 'alternative' => '$_COOKIE', |
|
62 | + ), |
|
63 | + 'HTTP_SESSION_VARS' => array( |
|
64 | + '5.3' => false, |
|
65 | + '5.4' => true, |
|
66 | + 'alternative' => '$_SESSION', |
|
67 | + ), |
|
68 | + 'HTTP_POST_FILES' => array( |
|
69 | + '5.3' => false, |
|
70 | + '5.4' => true, |
|
71 | + 'alternative' => '$_FILES', |
|
72 | + ), |
|
73 | + |
|
74 | + 'HTTP_RAW_POST_DATA' => array( |
|
75 | + '5.6' => false, |
|
76 | + '7.0' => true, |
|
77 | + 'alternative' => 'php://input', |
|
78 | + ), |
|
79 | + |
|
80 | + 'php_errormsg' => array( |
|
81 | + '7.2' => false, |
|
82 | + 'alternative' => 'error_get_last()', |
|
83 | + ), |
|
84 | + ); |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * Returns an array of tokens this test wants to listen for. |
|
89 | + * |
|
90 | + * @return array |
|
91 | + */ |
|
92 | + public function register() |
|
93 | + { |
|
94 | + return array(\T_VARIABLE); |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * Processes this test, when one of its tokens is encountered. |
|
100 | + * |
|
101 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
102 | + * @param int $stackPtr The position of the current token in the |
|
103 | + * stack passed in $tokens. |
|
104 | + * |
|
105 | + * @return void |
|
106 | + */ |
|
107 | + public function process(File $phpcsFile, $stackPtr) |
|
108 | + { |
|
109 | + if ($this->supportsAbove('5.3') === false) { |
|
110 | + return; |
|
111 | + } |
|
112 | + |
|
113 | + $tokens = $phpcsFile->getTokens(); |
|
114 | + $varName = substr($tokens[$stackPtr]['content'], 1); |
|
115 | + |
|
116 | + if (isset($this->removedGlobalVariables[$varName]) === false) { |
|
117 | + return; |
|
118 | + } |
|
119 | + |
|
120 | + if ($this->isClassProperty($phpcsFile, $stackPtr) === true) { |
|
121 | + // Ok, so this was a class property declaration, not our concern. |
|
122 | + return; |
|
123 | + } |
|
124 | + |
|
125 | + // Check for static usage of class properties shadowing the removed global variables. |
|
126 | + if ($this->inClassScope($phpcsFile, $stackPtr, false) === true) { |
|
127 | + $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
|
128 | + if ($prevToken !== false && $tokens[$prevToken]['code'] === \T_DOUBLE_COLON) { |
|
129 | + return; |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + // Do some additional checks for the $php_errormsg variable. |
|
134 | + if ($varName === 'php_errormsg' |
|
135 | + && $this->isTargetPHPErrormsgVar($phpcsFile, $stackPtr, $tokens) === false |
|
136 | + ) { |
|
137 | + return; |
|
138 | + } |
|
139 | + |
|
140 | + // Still here, so throw an error/warning. |
|
141 | + $itemInfo = array( |
|
142 | + 'name' => $varName, |
|
143 | + ); |
|
144 | + $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * Get the relevant sub-array for a specific item from a multi-dimensional array. |
|
150 | + * |
|
151 | + * @param array $itemInfo Base information about the item. |
|
152 | + * |
|
153 | + * @return array Version and other information about the item. |
|
154 | + */ |
|
155 | + public function getItemArray(array $itemInfo) |
|
156 | + { |
|
157 | + return $this->removedGlobalVariables[$itemInfo['name']]; |
|
158 | + } |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * Get the error message template for this sniff. |
|
163 | + * |
|
164 | + * @return string |
|
165 | + */ |
|
166 | + protected function getErrorMsgTemplate() |
|
167 | + { |
|
168 | + return "Global variable '\$%s' is "; |
|
169 | + } |
|
170 | + |
|
171 | + |
|
172 | + /** |
|
173 | + * Filter the error message before it's passed to PHPCS. |
|
174 | + * |
|
175 | + * @param string $error The error message which was created. |
|
176 | + * @param array $itemInfo Base information about the item this error message applies to. |
|
177 | + * @param array $errorInfo Detail information about an item this error message applies to. |
|
178 | + * |
|
179 | + * @return string |
|
180 | + */ |
|
181 | + protected function filterErrorMsg($error, array $itemInfo, array $errorInfo) |
|
182 | + { |
|
183 | + if ($itemInfo['name'] === 'php_errormsg') { |
|
184 | + $error = str_replace('Global', 'The', $error); |
|
185 | + } |
|
186 | + return $error; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Run some additional checks for the `$php_errormsg` variable. |
|
191 | + * |
|
192 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
193 | + * @param int $stackPtr The position of the current token in the |
|
194 | + * stack passed in $tokens. |
|
195 | + * @param array $tokens Token array of the current file. |
|
196 | + * |
|
197 | + * @return bool |
|
198 | + */ |
|
199 | + private function isTargetPHPErrormsgVar(File $phpcsFile, $stackPtr, array $tokens) |
|
200 | + { |
|
201 | + $scopeStart = 0; |
|
202 | + |
|
203 | + /* |
|
204 | 204 | * If the variable is detected within the scope of a function/closure, limit the checking. |
205 | 205 | */ |
206 | - $function = $phpcsFile->getCondition($stackPtr, \T_CLOSURE); |
|
207 | - if ($function === false) { |
|
208 | - $function = $phpcsFile->getCondition($stackPtr, \T_FUNCTION); |
|
209 | - } |
|
210 | - |
|
211 | - // It could also be a function param, which is not in the function scope. |
|
212 | - if ($function === false && isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
213 | - $nestedParentheses = $tokens[$stackPtr]['nested_parenthesis']; |
|
214 | - $parenthesisCloser = end($nestedParentheses); |
|
215 | - if (isset($tokens[$parenthesisCloser]['parenthesis_owner']) |
|
216 | - && ($tokens[$tokens[$parenthesisCloser]['parenthesis_owner']]['code'] === \T_FUNCTION |
|
217 | - || $tokens[$tokens[$parenthesisCloser]['parenthesis_owner']]['code'] === \T_CLOSURE) |
|
218 | - ) { |
|
219 | - $function = $tokens[$parenthesisCloser]['parenthesis_owner']; |
|
220 | - } |
|
221 | - } |
|
222 | - |
|
223 | - if ($function !== false) { |
|
224 | - $scopeStart = $tokens[$function]['scope_opener']; |
|
225 | - } |
|
226 | - |
|
227 | - /* |
|
206 | + $function = $phpcsFile->getCondition($stackPtr, \T_CLOSURE); |
|
207 | + if ($function === false) { |
|
208 | + $function = $phpcsFile->getCondition($stackPtr, \T_FUNCTION); |
|
209 | + } |
|
210 | + |
|
211 | + // It could also be a function param, which is not in the function scope. |
|
212 | + if ($function === false && isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
213 | + $nestedParentheses = $tokens[$stackPtr]['nested_parenthesis']; |
|
214 | + $parenthesisCloser = end($nestedParentheses); |
|
215 | + if (isset($tokens[$parenthesisCloser]['parenthesis_owner']) |
|
216 | + && ($tokens[$tokens[$parenthesisCloser]['parenthesis_owner']]['code'] === \T_FUNCTION |
|
217 | + || $tokens[$tokens[$parenthesisCloser]['parenthesis_owner']]['code'] === \T_CLOSURE) |
|
218 | + ) { |
|
219 | + $function = $tokens[$parenthesisCloser]['parenthesis_owner']; |
|
220 | + } |
|
221 | + } |
|
222 | + |
|
223 | + if ($function !== false) { |
|
224 | + $scopeStart = $tokens[$function]['scope_opener']; |
|
225 | + } |
|
226 | + |
|
227 | + /* |
|
228 | 228 | * Now, let's do some additional checks. |
229 | 229 | */ |
230 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
231 | - |
|
232 | - // Is the variable being used as an array ? |
|
233 | - if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === \T_OPEN_SQUARE_BRACKET) { |
|
234 | - // The PHP native variable is a string, so this is probably not it |
|
235 | - // (except for array access to string, but why would you in this case ?). |
|
236 | - return false; |
|
237 | - } |
|
238 | - |
|
239 | - // Is this a variable assignment ? |
|
240 | - if ($nextNonEmpty !== false |
|
241 | - && isset(Tokens::$assignmentTokens[$tokens[$nextNonEmpty]['code']]) === true |
|
242 | - ) { |
|
243 | - return false; |
|
244 | - } |
|
245 | - |
|
246 | - // Is this a function param shadowing the PHP native one ? |
|
247 | - if ($function !== false) { |
|
248 | - $parameters = PHPCSHelper::getMethodParameters($phpcsFile, $function); |
|
249 | - if (\is_array($parameters) === true && empty($parameters) === false) { |
|
250 | - foreach ($parameters as $param) { |
|
251 | - if ($param['name'] === '$php_errormsg') { |
|
252 | - return false; |
|
253 | - } |
|
254 | - } |
|
255 | - } |
|
256 | - } |
|
257 | - |
|
258 | - $skipPast = array( |
|
259 | - 'T_CLASS' => true, |
|
260 | - 'T_ANON_CLASS' => true, |
|
261 | - 'T_INTERFACE' => true, |
|
262 | - 'T_TRAIT' => true, |
|
263 | - 'T_FUNCTION' => true, |
|
264 | - 'T_CLOSURE' => true, |
|
265 | - ); |
|
266 | - |
|
267 | - // Walk back and see if there is an assignment to the variable within the same scope. |
|
268 | - for ($i = ($stackPtr - 1); $i >= $scopeStart; $i--) { |
|
269 | - if ($tokens[$i]['code'] === \T_CLOSE_CURLY_BRACKET |
|
270 | - && isset($tokens[$i]['scope_condition']) |
|
271 | - && isset($skipPast[$tokens[$tokens[$i]['scope_condition']]['type']]) |
|
272 | - ) { |
|
273 | - // Skip past functions, classes etc. |
|
274 | - $i = $tokens[$i]['scope_condition']; |
|
275 | - continue; |
|
276 | - } |
|
277 | - |
|
278 | - if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$php_errormsg') { |
|
279 | - continue; |
|
280 | - } |
|
281 | - |
|
282 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
|
283 | - |
|
284 | - if ($nextNonEmpty !== false |
|
285 | - && isset(Tokens::$assignmentTokens[$tokens[$nextNonEmpty]['code']]) === true |
|
286 | - ) { |
|
287 | - return false; |
|
288 | - } |
|
289 | - } |
|
290 | - |
|
291 | - return true; |
|
292 | - } |
|
230 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
231 | + |
|
232 | + // Is the variable being used as an array ? |
|
233 | + if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === \T_OPEN_SQUARE_BRACKET) { |
|
234 | + // The PHP native variable is a string, so this is probably not it |
|
235 | + // (except for array access to string, but why would you in this case ?). |
|
236 | + return false; |
|
237 | + } |
|
238 | + |
|
239 | + // Is this a variable assignment ? |
|
240 | + if ($nextNonEmpty !== false |
|
241 | + && isset(Tokens::$assignmentTokens[$tokens[$nextNonEmpty]['code']]) === true |
|
242 | + ) { |
|
243 | + return false; |
|
244 | + } |
|
245 | + |
|
246 | + // Is this a function param shadowing the PHP native one ? |
|
247 | + if ($function !== false) { |
|
248 | + $parameters = PHPCSHelper::getMethodParameters($phpcsFile, $function); |
|
249 | + if (\is_array($parameters) === true && empty($parameters) === false) { |
|
250 | + foreach ($parameters as $param) { |
|
251 | + if ($param['name'] === '$php_errormsg') { |
|
252 | + return false; |
|
253 | + } |
|
254 | + } |
|
255 | + } |
|
256 | + } |
|
257 | + |
|
258 | + $skipPast = array( |
|
259 | + 'T_CLASS' => true, |
|
260 | + 'T_ANON_CLASS' => true, |
|
261 | + 'T_INTERFACE' => true, |
|
262 | + 'T_TRAIT' => true, |
|
263 | + 'T_FUNCTION' => true, |
|
264 | + 'T_CLOSURE' => true, |
|
265 | + ); |
|
266 | + |
|
267 | + // Walk back and see if there is an assignment to the variable within the same scope. |
|
268 | + for ($i = ($stackPtr - 1); $i >= $scopeStart; $i--) { |
|
269 | + if ($tokens[$i]['code'] === \T_CLOSE_CURLY_BRACKET |
|
270 | + && isset($tokens[$i]['scope_condition']) |
|
271 | + && isset($skipPast[$tokens[$tokens[$i]['scope_condition']]['type']]) |
|
272 | + ) { |
|
273 | + // Skip past functions, classes etc. |
|
274 | + $i = $tokens[$i]['scope_condition']; |
|
275 | + continue; |
|
276 | + } |
|
277 | + |
|
278 | + if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$php_errormsg') { |
|
279 | + continue; |
|
280 | + } |
|
281 | + |
|
282 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
|
283 | + |
|
284 | + if ($nextNonEmpty !== false |
|
285 | + && isset(Tokens::$assignmentTokens[$tokens[$nextNonEmpty]['code']]) === true |
|
286 | + ) { |
|
287 | + return false; |
|
288 | + } |
|
289 | + } |
|
290 | + |
|
291 | + return true; |
|
292 | + } |
|
293 | 293 | } |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | */ |
92 | 92 | public function register() |
93 | 93 | { |
94 | - return array(\T_VARIABLE); |
|
94 | + return array( \T_VARIABLE ); |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | |
@@ -104,35 +104,35 @@ discard block |
||
104 | 104 | * |
105 | 105 | * @return void |
106 | 106 | */ |
107 | - public function process(File $phpcsFile, $stackPtr) |
|
107 | + public function process( File $phpcsFile, $stackPtr ) |
|
108 | 108 | { |
109 | - if ($this->supportsAbove('5.3') === false) { |
|
109 | + if ( $this->supportsAbove( '5.3' ) === false ) { |
|
110 | 110 | return; |
111 | 111 | } |
112 | 112 | |
113 | 113 | $tokens = $phpcsFile->getTokens(); |
114 | - $varName = substr($tokens[$stackPtr]['content'], 1); |
|
114 | + $varName = substr( $tokens[ $stackPtr ][ 'content' ], 1 ); |
|
115 | 115 | |
116 | - if (isset($this->removedGlobalVariables[$varName]) === false) { |
|
116 | + if ( isset( $this->removedGlobalVariables[ $varName ] ) === false ) { |
|
117 | 117 | return; |
118 | 118 | } |
119 | 119 | |
120 | - if ($this->isClassProperty($phpcsFile, $stackPtr) === true) { |
|
120 | + if ( $this->isClassProperty( $phpcsFile, $stackPtr ) === true ) { |
|
121 | 121 | // Ok, so this was a class property declaration, not our concern. |
122 | 122 | return; |
123 | 123 | } |
124 | 124 | |
125 | 125 | // Check for static usage of class properties shadowing the removed global variables. |
126 | - if ($this->inClassScope($phpcsFile, $stackPtr, false) === true) { |
|
127 | - $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
|
128 | - if ($prevToken !== false && $tokens[$prevToken]['code'] === \T_DOUBLE_COLON) { |
|
126 | + if ( $this->inClassScope( $phpcsFile, $stackPtr, false ) === true ) { |
|
127 | + $prevToken = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true, null, true ); |
|
128 | + if ( $prevToken !== false && $tokens[ $prevToken ][ 'code' ] === \T_DOUBLE_COLON ) { |
|
129 | 129 | return; |
130 | 130 | } |
131 | 131 | } |
132 | 132 | |
133 | 133 | // Do some additional checks for the $php_errormsg variable. |
134 | - if ($varName === 'php_errormsg' |
|
135 | - && $this->isTargetPHPErrormsgVar($phpcsFile, $stackPtr, $tokens) === false |
|
134 | + if ( $varName === 'php_errormsg' |
|
135 | + && $this->isTargetPHPErrormsgVar( $phpcsFile, $stackPtr, $tokens ) === false |
|
136 | 136 | ) { |
137 | 137 | return; |
138 | 138 | } |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | $itemInfo = array( |
142 | 142 | 'name' => $varName, |
143 | 143 | ); |
144 | - $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
144 | + $this->handleFeature( $phpcsFile, $stackPtr, $itemInfo ); |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | |
@@ -152,9 +152,9 @@ discard block |
||
152 | 152 | * |
153 | 153 | * @return array Version and other information about the item. |
154 | 154 | */ |
155 | - public function getItemArray(array $itemInfo) |
|
155 | + public function getItemArray( array $itemInfo ) |
|
156 | 156 | { |
157 | - return $this->removedGlobalVariables[$itemInfo['name']]; |
|
157 | + return $this->removedGlobalVariables[ $itemInfo[ 'name' ] ]; |
|
158 | 158 | } |
159 | 159 | |
160 | 160 | |
@@ -178,10 +178,10 @@ discard block |
||
178 | 178 | * |
179 | 179 | * @return string |
180 | 180 | */ |
181 | - protected function filterErrorMsg($error, array $itemInfo, array $errorInfo) |
|
181 | + protected function filterErrorMsg( $error, array $itemInfo, array $errorInfo ) |
|
182 | 182 | { |
183 | - if ($itemInfo['name'] === 'php_errormsg') { |
|
184 | - $error = str_replace('Global', 'The', $error); |
|
183 | + if ( $itemInfo[ 'name' ] === 'php_errormsg' ) { |
|
184 | + $error = str_replace( 'Global', 'The', $error ); |
|
185 | 185 | } |
186 | 186 | return $error; |
187 | 187 | } |
@@ -196,59 +196,59 @@ discard block |
||
196 | 196 | * |
197 | 197 | * @return bool |
198 | 198 | */ |
199 | - private function isTargetPHPErrormsgVar(File $phpcsFile, $stackPtr, array $tokens) |
|
199 | + private function isTargetPHPErrormsgVar( File $phpcsFile, $stackPtr, array $tokens ) |
|
200 | 200 | { |
201 | 201 | $scopeStart = 0; |
202 | 202 | |
203 | 203 | /* |
204 | 204 | * If the variable is detected within the scope of a function/closure, limit the checking. |
205 | 205 | */ |
206 | - $function = $phpcsFile->getCondition($stackPtr, \T_CLOSURE); |
|
207 | - if ($function === false) { |
|
208 | - $function = $phpcsFile->getCondition($stackPtr, \T_FUNCTION); |
|
206 | + $function = $phpcsFile->getCondition( $stackPtr, \T_CLOSURE ); |
|
207 | + if ( $function === false ) { |
|
208 | + $function = $phpcsFile->getCondition( $stackPtr, \T_FUNCTION ); |
|
209 | 209 | } |
210 | 210 | |
211 | 211 | // It could also be a function param, which is not in the function scope. |
212 | - if ($function === false && isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
213 | - $nestedParentheses = $tokens[$stackPtr]['nested_parenthesis']; |
|
214 | - $parenthesisCloser = end($nestedParentheses); |
|
215 | - if (isset($tokens[$parenthesisCloser]['parenthesis_owner']) |
|
216 | - && ($tokens[$tokens[$parenthesisCloser]['parenthesis_owner']]['code'] === \T_FUNCTION |
|
217 | - || $tokens[$tokens[$parenthesisCloser]['parenthesis_owner']]['code'] === \T_CLOSURE) |
|
212 | + if ( $function === false && isset( $tokens[ $stackPtr ][ 'nested_parenthesis' ] ) === true ) { |
|
213 | + $nestedParentheses = $tokens[ $stackPtr ][ 'nested_parenthesis' ]; |
|
214 | + $parenthesisCloser = end( $nestedParentheses ); |
|
215 | + if ( isset( $tokens[ $parenthesisCloser ][ 'parenthesis_owner' ] ) |
|
216 | + && ( $tokens[ $tokens[ $parenthesisCloser ][ 'parenthesis_owner' ] ][ 'code' ] === \T_FUNCTION |
|
217 | + || $tokens[ $tokens[ $parenthesisCloser ][ 'parenthesis_owner' ] ][ 'code' ] === \T_CLOSURE ) |
|
218 | 218 | ) { |
219 | - $function = $tokens[$parenthesisCloser]['parenthesis_owner']; |
|
219 | + $function = $tokens[ $parenthesisCloser ][ 'parenthesis_owner' ]; |
|
220 | 220 | } |
221 | 221 | } |
222 | 222 | |
223 | - if ($function !== false) { |
|
224 | - $scopeStart = $tokens[$function]['scope_opener']; |
|
223 | + if ( $function !== false ) { |
|
224 | + $scopeStart = $tokens[ $function ][ 'scope_opener' ]; |
|
225 | 225 | } |
226 | 226 | |
227 | 227 | /* |
228 | 228 | * Now, let's do some additional checks. |
229 | 229 | */ |
230 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
230 | + $nextNonEmpty = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); |
|
231 | 231 | |
232 | 232 | // Is the variable being used as an array ? |
233 | - if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === \T_OPEN_SQUARE_BRACKET) { |
|
233 | + if ( $nextNonEmpty !== false && $tokens[ $nextNonEmpty ][ 'code' ] === \T_OPEN_SQUARE_BRACKET ) { |
|
234 | 234 | // The PHP native variable is a string, so this is probably not it |
235 | 235 | // (except for array access to string, but why would you in this case ?). |
236 | 236 | return false; |
237 | 237 | } |
238 | 238 | |
239 | 239 | // Is this a variable assignment ? |
240 | - if ($nextNonEmpty !== false |
|
241 | - && isset(Tokens::$assignmentTokens[$tokens[$nextNonEmpty]['code']]) === true |
|
240 | + if ( $nextNonEmpty !== false |
|
241 | + && isset( Tokens::$assignmentTokens[ $tokens[ $nextNonEmpty ][ 'code' ] ] ) === true |
|
242 | 242 | ) { |
243 | 243 | return false; |
244 | 244 | } |
245 | 245 | |
246 | 246 | // Is this a function param shadowing the PHP native one ? |
247 | - if ($function !== false) { |
|
248 | - $parameters = PHPCSHelper::getMethodParameters($phpcsFile, $function); |
|
249 | - if (\is_array($parameters) === true && empty($parameters) === false) { |
|
250 | - foreach ($parameters as $param) { |
|
251 | - if ($param['name'] === '$php_errormsg') { |
|
247 | + if ( $function !== false ) { |
|
248 | + $parameters = PHPCSHelper::getMethodParameters( $phpcsFile, $function ); |
|
249 | + if ( \is_array( $parameters ) === true && empty( $parameters ) === false ) { |
|
250 | + foreach ( $parameters as $param ) { |
|
251 | + if ( $param[ 'name' ] === '$php_errormsg' ) { |
|
252 | 252 | return false; |
253 | 253 | } |
254 | 254 | } |
@@ -265,24 +265,24 @@ discard block |
||
265 | 265 | ); |
266 | 266 | |
267 | 267 | // Walk back and see if there is an assignment to the variable within the same scope. |
268 | - for ($i = ($stackPtr - 1); $i >= $scopeStart; $i--) { |
|
269 | - if ($tokens[$i]['code'] === \T_CLOSE_CURLY_BRACKET |
|
270 | - && isset($tokens[$i]['scope_condition']) |
|
271 | - && isset($skipPast[$tokens[$tokens[$i]['scope_condition']]['type']]) |
|
268 | + for ( $i = ( $stackPtr - 1 ); $i >= $scopeStart; $i-- ) { |
|
269 | + if ( $tokens[ $i ][ 'code' ] === \T_CLOSE_CURLY_BRACKET |
|
270 | + && isset( $tokens[ $i ][ 'scope_condition' ] ) |
|
271 | + && isset( $skipPast[ $tokens[ $tokens[ $i ][ 'scope_condition' ] ][ 'type' ] ] ) |
|
272 | 272 | ) { |
273 | 273 | // Skip past functions, classes etc. |
274 | - $i = $tokens[$i]['scope_condition']; |
|
274 | + $i = $tokens[ $i ][ 'scope_condition' ]; |
|
275 | 275 | continue; |
276 | 276 | } |
277 | 277 | |
278 | - if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$php_errormsg') { |
|
278 | + if ( $tokens[ $i ][ 'code' ] !== \T_VARIABLE || $tokens[ $i ][ 'content' ] !== '$php_errormsg' ) { |
|
279 | 279 | continue; |
280 | 280 | } |
281 | 281 | |
282 | - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
|
282 | + $nextNonEmpty = $phpcsFile->findNext( Tokens::$emptyTokens, ( $i + 1 ), null, true ); |
|
283 | 283 | |
284 | - if ($nextNonEmpty !== false |
|
285 | - && isset(Tokens::$assignmentTokens[$tokens[$nextNonEmpty]['code']]) === true |
|
284 | + if ( $nextNonEmpty !== false |
|
285 | + && isset( Tokens::$assignmentTokens[ $tokens[ $nextNonEmpty ][ 'code' ] ] ) === true |
|
286 | 286 | ) { |
287 | 287 | return false; |
288 | 288 | } |
@@ -23,8 +23,7 @@ discard block |
||
23 | 23 | * @package PHPCompatibility |
24 | 24 | * @author Wim Godden <[email protected]> |
25 | 25 | */ |
26 | -class RemovedPredefinedGlobalVariablesSniff extends AbstractRemovedFeatureSniff |
|
27 | -{ |
|
26 | +class RemovedPredefinedGlobalVariablesSniff extends AbstractRemovedFeatureSniff { |
|
28 | 27 | |
29 | 28 | /** |
30 | 29 | * A list of removed global variables with their alternative, if any. |
@@ -89,8 +88,7 @@ discard block |
||
89 | 88 | * |
90 | 89 | * @return array |
91 | 90 | */ |
92 | - public function register() |
|
93 | - { |
|
91 | + public function register() { |
|
94 | 92 | return array(\T_VARIABLE); |
95 | 93 | } |
96 | 94 | |
@@ -104,8 +102,7 @@ discard block |
||
104 | 102 | * |
105 | 103 | * @return void |
106 | 104 | */ |
107 | - public function process(File $phpcsFile, $stackPtr) |
|
108 | - { |
|
105 | + public function process(File $phpcsFile, $stackPtr) { |
|
109 | 106 | if ($this->supportsAbove('5.3') === false) { |
110 | 107 | return; |
111 | 108 | } |
@@ -152,8 +149,7 @@ discard block |
||
152 | 149 | * |
153 | 150 | * @return array Version and other information about the item. |
154 | 151 | */ |
155 | - public function getItemArray(array $itemInfo) |
|
156 | - { |
|
152 | + public function getItemArray(array $itemInfo) { |
|
157 | 153 | return $this->removedGlobalVariables[$itemInfo['name']]; |
158 | 154 | } |
159 | 155 | |
@@ -163,8 +159,7 @@ discard block |
||
163 | 159 | * |
164 | 160 | * @return string |
165 | 161 | */ |
166 | - protected function getErrorMsgTemplate() |
|
167 | - { |
|
162 | + protected function getErrorMsgTemplate() { |
|
168 | 163 | return "Global variable '\$%s' is "; |
169 | 164 | } |
170 | 165 | |
@@ -178,8 +173,7 @@ discard block |
||
178 | 173 | * |
179 | 174 | * @return string |
180 | 175 | */ |
181 | - protected function filterErrorMsg($error, array $itemInfo, array $errorInfo) |
|
182 | - { |
|
176 | + protected function filterErrorMsg($error, array $itemInfo, array $errorInfo) { |
|
183 | 177 | if ($itemInfo['name'] === 'php_errormsg') { |
184 | 178 | $error = str_replace('Global', 'The', $error); |
185 | 179 | } |
@@ -196,8 +190,7 @@ discard block |
||
196 | 190 | * |
197 | 191 | * @return bool |
198 | 192 | */ |
199 | - private function isTargetPHPErrormsgVar(File $phpcsFile, $stackPtr, array $tokens) |
|
200 | - { |
|
193 | + private function isTargetPHPErrormsgVar(File $phpcsFile, $stackPtr, array $tokens) { |
|
201 | 194 | $scopeStart = 0; |
202 | 195 | |
203 | 196 | /* |
@@ -65,7 +65,7 @@ |
||
65 | 65 | /** |
66 | 66 | * Returns an array of tokens this test wants to listen for. |
67 | 67 | * |
68 | - * @return array |
|
68 | + * @return integer[] |
|
69 | 69 | */ |
70 | 70 | public function register() |
71 | 71 | { |
@@ -30,191 +30,191 @@ discard block |
||
30 | 30 | class ArgumentFunctionsReportCurrentValueSniff extends Sniff |
31 | 31 | { |
32 | 32 | |
33 | - /** |
|
34 | - * A list of functions that, when called, can behave differently in PHP 7 |
|
35 | - * when dealing with parameters of the function they're called in. |
|
36 | - * |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - protected $changedFunctions = array( |
|
40 | - 'func_get_arg' => true, |
|
41 | - 'func_get_args' => true, |
|
42 | - 'debug_backtrace' => true, |
|
43 | - 'debug_print_backtrace' => true, |
|
44 | - ); |
|
45 | - |
|
46 | - /** |
|
47 | - * Tokens to look out for to allow us to skip past nested scoped structures. |
|
48 | - * |
|
49 | - * @var array |
|
50 | - */ |
|
51 | - private $skipPastNested = array( |
|
52 | - 'T_CLASS' => true, |
|
53 | - 'T_ANON_CLASS' => true, |
|
54 | - 'T_INTERFACE' => true, |
|
55 | - 'T_TRAIT' => true, |
|
56 | - 'T_FUNCTION' => true, |
|
57 | - 'T_CLOSURE' => true, |
|
58 | - ); |
|
59 | - |
|
60 | - /** |
|
61 | - * List of tokens which when they preceed a T_STRING *within a function* indicate |
|
62 | - * this is not a call to a PHP native function. |
|
63 | - * |
|
64 | - * This list already takes into account that nested scoped structures are being |
|
65 | - * skipped over, so doesn't check for those again. |
|
66 | - * Similarly, as constants won't have parentheses, those don't need to be checked |
|
67 | - * for either. |
|
68 | - * |
|
69 | - * @var array |
|
70 | - */ |
|
71 | - private $noneFunctionCallIndicators = array( |
|
72 | - \T_DOUBLE_COLON => true, |
|
73 | - \T_OBJECT_OPERATOR => true, |
|
74 | - ); |
|
75 | - |
|
76 | - /** |
|
77 | - * The tokens for variable incrementing/decrementing. |
|
78 | - * |
|
79 | - * @var array |
|
80 | - */ |
|
81 | - private $plusPlusMinusMinus = array( |
|
82 | - \T_DEC => true, |
|
83 | - \T_INC => true, |
|
84 | - ); |
|
85 | - |
|
86 | - /** |
|
87 | - * Tokens to ignore when determining the start of a statement. |
|
88 | - * |
|
89 | - * @var array |
|
90 | - */ |
|
91 | - private $ignoreForStartOfStatement = array( |
|
92 | - \T_COMMA, |
|
93 | - \T_DOUBLE_ARROW, |
|
94 | - \T_OPEN_SQUARE_BRACKET, |
|
95 | - \T_OPEN_PARENTHESIS, |
|
96 | - ); |
|
97 | - |
|
98 | - /** |
|
99 | - * Returns an array of tokens this test wants to listen for. |
|
100 | - * |
|
101 | - * @return array |
|
102 | - */ |
|
103 | - public function register() |
|
104 | - { |
|
105 | - return array( |
|
106 | - \T_FUNCTION, |
|
107 | - \T_CLOSURE, |
|
108 | - ); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Processes this test, when one of its tokens is encountered. |
|
113 | - * |
|
114 | - * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
115 | - * @param int $stackPtr The position of the current token |
|
116 | - * in the stack passed in $tokens. |
|
117 | - * |
|
118 | - * @return void |
|
119 | - */ |
|
120 | - public function process(File $phpcsFile, $stackPtr) |
|
121 | - { |
|
122 | - if ($this->supportsAbove('7.0') === false) { |
|
123 | - return; |
|
124 | - } |
|
125 | - |
|
126 | - $tokens = $phpcsFile->getTokens(); |
|
127 | - |
|
128 | - if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { |
|
129 | - // Abstract function, interface function, live coding or parse error. |
|
130 | - return; |
|
131 | - } |
|
132 | - |
|
133 | - $scopeOpener = $tokens[$stackPtr]['scope_opener']; |
|
134 | - $scopeCloser = $tokens[$stackPtr]['scope_closer']; |
|
135 | - |
|
136 | - // Does the function declaration have parameters ? |
|
137 | - $params = PHPCSHelper::getMethodParameters($phpcsFile, $stackPtr); |
|
138 | - if (empty($params)) { |
|
139 | - // No named arguments found, so no risk of them being changed. |
|
140 | - return; |
|
141 | - } |
|
142 | - |
|
143 | - $paramNames = array(); |
|
144 | - foreach ($params as $param) { |
|
145 | - $paramNames[] = $param['name']; |
|
146 | - } |
|
147 | - |
|
148 | - for ($i = ($scopeOpener + 1); $i < $scopeCloser; $i++) { |
|
149 | - if (isset($this->skipPastNested[$tokens[$i]['type']]) && isset($tokens[$i]['scope_closer'])) { |
|
150 | - // Skip past nested structures. |
|
151 | - $i = $tokens[$i]['scope_closer']; |
|
152 | - continue; |
|
153 | - } |
|
154 | - |
|
155 | - if ($tokens[$i]['code'] !== \T_STRING) { |
|
156 | - continue; |
|
157 | - } |
|
158 | - |
|
159 | - $foundFunctionName = strtolower($tokens[$i]['content']); |
|
160 | - |
|
161 | - if (isset($this->changedFunctions[$foundFunctionName]) === false) { |
|
162 | - // Not one of the target functions. |
|
163 | - continue; |
|
164 | - } |
|
165 | - |
|
166 | - /* |
|
33 | + /** |
|
34 | + * A list of functions that, when called, can behave differently in PHP 7 |
|
35 | + * when dealing with parameters of the function they're called in. |
|
36 | + * |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + protected $changedFunctions = array( |
|
40 | + 'func_get_arg' => true, |
|
41 | + 'func_get_args' => true, |
|
42 | + 'debug_backtrace' => true, |
|
43 | + 'debug_print_backtrace' => true, |
|
44 | + ); |
|
45 | + |
|
46 | + /** |
|
47 | + * Tokens to look out for to allow us to skip past nested scoped structures. |
|
48 | + * |
|
49 | + * @var array |
|
50 | + */ |
|
51 | + private $skipPastNested = array( |
|
52 | + 'T_CLASS' => true, |
|
53 | + 'T_ANON_CLASS' => true, |
|
54 | + 'T_INTERFACE' => true, |
|
55 | + 'T_TRAIT' => true, |
|
56 | + 'T_FUNCTION' => true, |
|
57 | + 'T_CLOSURE' => true, |
|
58 | + ); |
|
59 | + |
|
60 | + /** |
|
61 | + * List of tokens which when they preceed a T_STRING *within a function* indicate |
|
62 | + * this is not a call to a PHP native function. |
|
63 | + * |
|
64 | + * This list already takes into account that nested scoped structures are being |
|
65 | + * skipped over, so doesn't check for those again. |
|
66 | + * Similarly, as constants won't have parentheses, those don't need to be checked |
|
67 | + * for either. |
|
68 | + * |
|
69 | + * @var array |
|
70 | + */ |
|
71 | + private $noneFunctionCallIndicators = array( |
|
72 | + \T_DOUBLE_COLON => true, |
|
73 | + \T_OBJECT_OPERATOR => true, |
|
74 | + ); |
|
75 | + |
|
76 | + /** |
|
77 | + * The tokens for variable incrementing/decrementing. |
|
78 | + * |
|
79 | + * @var array |
|
80 | + */ |
|
81 | + private $plusPlusMinusMinus = array( |
|
82 | + \T_DEC => true, |
|
83 | + \T_INC => true, |
|
84 | + ); |
|
85 | + |
|
86 | + /** |
|
87 | + * Tokens to ignore when determining the start of a statement. |
|
88 | + * |
|
89 | + * @var array |
|
90 | + */ |
|
91 | + private $ignoreForStartOfStatement = array( |
|
92 | + \T_COMMA, |
|
93 | + \T_DOUBLE_ARROW, |
|
94 | + \T_OPEN_SQUARE_BRACKET, |
|
95 | + \T_OPEN_PARENTHESIS, |
|
96 | + ); |
|
97 | + |
|
98 | + /** |
|
99 | + * Returns an array of tokens this test wants to listen for. |
|
100 | + * |
|
101 | + * @return array |
|
102 | + */ |
|
103 | + public function register() |
|
104 | + { |
|
105 | + return array( |
|
106 | + \T_FUNCTION, |
|
107 | + \T_CLOSURE, |
|
108 | + ); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Processes this test, when one of its tokens is encountered. |
|
113 | + * |
|
114 | + * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
115 | + * @param int $stackPtr The position of the current token |
|
116 | + * in the stack passed in $tokens. |
|
117 | + * |
|
118 | + * @return void |
|
119 | + */ |
|
120 | + public function process(File $phpcsFile, $stackPtr) |
|
121 | + { |
|
122 | + if ($this->supportsAbove('7.0') === false) { |
|
123 | + return; |
|
124 | + } |
|
125 | + |
|
126 | + $tokens = $phpcsFile->getTokens(); |
|
127 | + |
|
128 | + if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { |
|
129 | + // Abstract function, interface function, live coding or parse error. |
|
130 | + return; |
|
131 | + } |
|
132 | + |
|
133 | + $scopeOpener = $tokens[$stackPtr]['scope_opener']; |
|
134 | + $scopeCloser = $tokens[$stackPtr]['scope_closer']; |
|
135 | + |
|
136 | + // Does the function declaration have parameters ? |
|
137 | + $params = PHPCSHelper::getMethodParameters($phpcsFile, $stackPtr); |
|
138 | + if (empty($params)) { |
|
139 | + // No named arguments found, so no risk of them being changed. |
|
140 | + return; |
|
141 | + } |
|
142 | + |
|
143 | + $paramNames = array(); |
|
144 | + foreach ($params as $param) { |
|
145 | + $paramNames[] = $param['name']; |
|
146 | + } |
|
147 | + |
|
148 | + for ($i = ($scopeOpener + 1); $i < $scopeCloser; $i++) { |
|
149 | + if (isset($this->skipPastNested[$tokens[$i]['type']]) && isset($tokens[$i]['scope_closer'])) { |
|
150 | + // Skip past nested structures. |
|
151 | + $i = $tokens[$i]['scope_closer']; |
|
152 | + continue; |
|
153 | + } |
|
154 | + |
|
155 | + if ($tokens[$i]['code'] !== \T_STRING) { |
|
156 | + continue; |
|
157 | + } |
|
158 | + |
|
159 | + $foundFunctionName = strtolower($tokens[$i]['content']); |
|
160 | + |
|
161 | + if (isset($this->changedFunctions[$foundFunctionName]) === false) { |
|
162 | + // Not one of the target functions. |
|
163 | + continue; |
|
164 | + } |
|
165 | + |
|
166 | + /* |
|
167 | 167 | * Ok, so is this really a function call to one of the PHP native functions ? |
168 | 168 | */ |
169 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
|
170 | - if ($next === false || $tokens[$next]['code'] !== \T_OPEN_PARENTHESIS) { |
|
171 | - // Live coding, parse error or not a function call. |
|
172 | - continue; |
|
173 | - } |
|
174 | - |
|
175 | - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true); |
|
176 | - if ($prev !== false) { |
|
177 | - if (isset($this->noneFunctionCallIndicators[$tokens[$prev]['code']])) { |
|
178 | - continue; |
|
179 | - } |
|
180 | - |
|
181 | - // Check for namespaced functions, ie: \foo\bar() not \bar(). |
|
182 | - if ($tokens[ $prev ]['code'] === \T_NS_SEPARATOR) { |
|
183 | - $pprev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true); |
|
184 | - if ($pprev !== false && $tokens[ $pprev ]['code'] === \T_STRING) { |
|
185 | - continue; |
|
186 | - } |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - /* |
|
169 | + $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
|
170 | + if ($next === false || $tokens[$next]['code'] !== \T_OPEN_PARENTHESIS) { |
|
171 | + // Live coding, parse error or not a function call. |
|
172 | + continue; |
|
173 | + } |
|
174 | + |
|
175 | + $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true); |
|
176 | + if ($prev !== false) { |
|
177 | + if (isset($this->noneFunctionCallIndicators[$tokens[$prev]['code']])) { |
|
178 | + continue; |
|
179 | + } |
|
180 | + |
|
181 | + // Check for namespaced functions, ie: \foo\bar() not \bar(). |
|
182 | + if ($tokens[ $prev ]['code'] === \T_NS_SEPARATOR) { |
|
183 | + $pprev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true); |
|
184 | + if ($pprev !== false && $tokens[ $pprev ]['code'] === \T_STRING) { |
|
185 | + continue; |
|
186 | + } |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + /* |
|
191 | 191 | * Address some special cases. |
192 | 192 | */ |
193 | - if ($foundFunctionName !== 'func_get_args') { |
|
194 | - $paramOne = $this->getFunctionCallParameter($phpcsFile, $i, 1); |
|
195 | - if ($paramOne !== false) { |
|
196 | - switch ($foundFunctionName) { |
|
197 | - /* |
|
193 | + if ($foundFunctionName !== 'func_get_args') { |
|
194 | + $paramOne = $this->getFunctionCallParameter($phpcsFile, $i, 1); |
|
195 | + if ($paramOne !== false) { |
|
196 | + switch ($foundFunctionName) { |
|
197 | + /* |
|
198 | 198 | * Check if `debug_(print_)backtrace()` is called with the |
199 | 199 | * `DEBUG_BACKTRACE_IGNORE_ARGS` option. |
200 | 200 | */ |
201 | - case 'debug_backtrace': |
|
202 | - case 'debug_print_backtrace': |
|
203 | - $hasIgnoreArgs = $phpcsFile->findNext( |
|
204 | - \T_STRING, |
|
205 | - $paramOne['start'], |
|
206 | - ($paramOne['end'] + 1), |
|
207 | - false, |
|
208 | - 'DEBUG_BACKTRACE_IGNORE_ARGS' |
|
209 | - ); |
|
210 | - |
|
211 | - if ($hasIgnoreArgs !== false) { |
|
212 | - // Debug_backtrace() called with ignore args option. |
|
213 | - continue 2; |
|
214 | - } |
|
215 | - break; |
|
216 | - |
|
217 | - /* |
|
201 | + case 'debug_backtrace': |
|
202 | + case 'debug_print_backtrace': |
|
203 | + $hasIgnoreArgs = $phpcsFile->findNext( |
|
204 | + \T_STRING, |
|
205 | + $paramOne['start'], |
|
206 | + ($paramOne['end'] + 1), |
|
207 | + false, |
|
208 | + 'DEBUG_BACKTRACE_IGNORE_ARGS' |
|
209 | + ); |
|
210 | + |
|
211 | + if ($hasIgnoreArgs !== false) { |
|
212 | + // Debug_backtrace() called with ignore args option. |
|
213 | + continue 2; |
|
214 | + } |
|
215 | + break; |
|
216 | + |
|
217 | + /* |
|
218 | 218 | * Collect the necessary information to only throw a notice if the argument |
219 | 219 | * touched/changed is in line with the passed $arg_num. |
220 | 220 | * |
@@ -224,21 +224,21 @@ discard block |
||
224 | 224 | * {@internal Note: This does not take calculations into account! |
225 | 225 | * Should be exceptionally rare and can - if needs be - be addressed at a later stage.}} |
226 | 226 | */ |
227 | - case 'func_get_arg': |
|
228 | - $number = $phpcsFile->findNext(\T_LNUMBER, $paramOne['start'], ($paramOne['end'] + 1)); |
|
229 | - if ($number !== false) { |
|
230 | - $argNumber = $tokens[$number]['content']; |
|
231 | - |
|
232 | - if (isset($paramNames[$argNumber]) === false) { |
|
233 | - // Requesting a non-named additional parameter. Ignore. |
|
234 | - continue 2; |
|
235 | - } |
|
236 | - } |
|
237 | - break; |
|
238 | - } |
|
239 | - } |
|
240 | - } else { |
|
241 | - /* |
|
227 | + case 'func_get_arg': |
|
228 | + $number = $phpcsFile->findNext(\T_LNUMBER, $paramOne['start'], ($paramOne['end'] + 1)); |
|
229 | + if ($number !== false) { |
|
230 | + $argNumber = $tokens[$number]['content']; |
|
231 | + |
|
232 | + if (isset($paramNames[$argNumber]) === false) { |
|
233 | + // Requesting a non-named additional parameter. Ignore. |
|
234 | + continue 2; |
|
235 | + } |
|
236 | + } |
|
237 | + break; |
|
238 | + } |
|
239 | + } |
|
240 | + } else { |
|
241 | + /* |
|
242 | 242 | * Check if the call to func_get_args() happens to be in an array_slice() or |
243 | 243 | * array_splice() with an $offset higher than the number of named parameters. |
244 | 244 | * In that case, we can ignore it. |
@@ -246,33 +246,33 @@ discard block |
||
246 | 246 | * {@internal Note: This does not take offset calculations into account! |
247 | 247 | * Should be exceptionally rare and can - if needs be - be addressed at a later stage.}} |
248 | 248 | */ |
249 | - if ($prev !== false && $tokens[$prev]['code'] === \T_OPEN_PARENTHESIS) { |
|
250 | - |
|
251 | - $maybeFunctionCall = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true); |
|
252 | - if ($maybeFunctionCall !== false |
|
253 | - && $tokens[$maybeFunctionCall]['code'] === \T_STRING |
|
254 | - && ($tokens[$maybeFunctionCall]['content'] === 'array_slice' |
|
255 | - || $tokens[$maybeFunctionCall]['content'] === 'array_splice') |
|
256 | - ) { |
|
257 | - $parentFuncParamTwo = $this->getFunctionCallParameter($phpcsFile, $maybeFunctionCall, 2); |
|
258 | - $number = $phpcsFile->findNext( |
|
259 | - \T_LNUMBER, |
|
260 | - $parentFuncParamTwo['start'], |
|
261 | - ($parentFuncParamTwo['end'] + 1) |
|
262 | - ); |
|
263 | - |
|
264 | - if ($number !== false && isset($paramNames[$tokens[$number]['content']]) === false) { |
|
265 | - // Requesting non-named additional parameters. Ignore. |
|
266 | - continue ; |
|
267 | - } |
|
268 | - |
|
269 | - // Slice starts at a named argument, but we know which params are being accessed. |
|
270 | - $paramNamesSubset = \array_slice($paramNames, $tokens[$number]['content']); |
|
271 | - } |
|
272 | - } |
|
273 | - } |
|
274 | - |
|
275 | - /* |
|
249 | + if ($prev !== false && $tokens[$prev]['code'] === \T_OPEN_PARENTHESIS) { |
|
250 | + |
|
251 | + $maybeFunctionCall = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true); |
|
252 | + if ($maybeFunctionCall !== false |
|
253 | + && $tokens[$maybeFunctionCall]['code'] === \T_STRING |
|
254 | + && ($tokens[$maybeFunctionCall]['content'] === 'array_slice' |
|
255 | + || $tokens[$maybeFunctionCall]['content'] === 'array_splice') |
|
256 | + ) { |
|
257 | + $parentFuncParamTwo = $this->getFunctionCallParameter($phpcsFile, $maybeFunctionCall, 2); |
|
258 | + $number = $phpcsFile->findNext( |
|
259 | + \T_LNUMBER, |
|
260 | + $parentFuncParamTwo['start'], |
|
261 | + ($parentFuncParamTwo['end'] + 1) |
|
262 | + ); |
|
263 | + |
|
264 | + if ($number !== false && isset($paramNames[$tokens[$number]['content']]) === false) { |
|
265 | + // Requesting non-named additional parameters. Ignore. |
|
266 | + continue ; |
|
267 | + } |
|
268 | + |
|
269 | + // Slice starts at a named argument, but we know which params are being accessed. |
|
270 | + $paramNamesSubset = \array_slice($paramNames, $tokens[$number]['content']); |
|
271 | + } |
|
272 | + } |
|
273 | + } |
|
274 | + |
|
275 | + /* |
|
276 | 276 | * For debug_backtrace(), check if the result is being dereferenced and if so, |
277 | 277 | * whether the `args` index is used. |
278 | 278 | * I.e. whether `$index` in `debug_backtrace()[$stackFrame][$index]` is a string |
@@ -280,160 +280,160 @@ discard block |
||
280 | 280 | * |
281 | 281 | * Note: We already know that $next is the open parenthesis of the function call. |
282 | 282 | */ |
283 | - if ($foundFunctionName === 'debug_backtrace' && isset($tokens[$next]['parenthesis_closer'])) { |
|
284 | - $afterParenthesis = $phpcsFile->findNext( |
|
285 | - Tokens::$emptyTokens, |
|
286 | - ($tokens[$next]['parenthesis_closer'] + 1), |
|
287 | - null, |
|
288 | - true |
|
289 | - ); |
|
290 | - |
|
291 | - if ($tokens[$afterParenthesis]['code'] === \T_OPEN_SQUARE_BRACKET |
|
292 | - && isset($tokens[$afterParenthesis]['bracket_closer']) |
|
293 | - ) { |
|
294 | - $afterStackFrame = $phpcsFile->findNext( |
|
295 | - Tokens::$emptyTokens, |
|
296 | - ($tokens[$afterParenthesis]['bracket_closer'] + 1), |
|
297 | - null, |
|
298 | - true |
|
299 | - ); |
|
300 | - |
|
301 | - if ($tokens[$afterStackFrame]['code'] === \T_OPEN_SQUARE_BRACKET |
|
302 | - && isset($tokens[$afterStackFrame]['bracket_closer']) |
|
303 | - ) { |
|
304 | - $arrayIndex = $phpcsFile->findNext( |
|
305 | - \T_CONSTANT_ENCAPSED_STRING, |
|
306 | - ($afterStackFrame + 1), |
|
307 | - $tokens[$afterStackFrame]['bracket_closer'] |
|
308 | - ); |
|
309 | - |
|
310 | - if ($arrayIndex !== false && $this->stripQuotes($tokens[$arrayIndex]['content']) !== 'args') { |
|
311 | - continue; |
|
312 | - } |
|
313 | - } |
|
314 | - } |
|
315 | - } |
|
316 | - |
|
317 | - /* |
|
283 | + if ($foundFunctionName === 'debug_backtrace' && isset($tokens[$next]['parenthesis_closer'])) { |
|
284 | + $afterParenthesis = $phpcsFile->findNext( |
|
285 | + Tokens::$emptyTokens, |
|
286 | + ($tokens[$next]['parenthesis_closer'] + 1), |
|
287 | + null, |
|
288 | + true |
|
289 | + ); |
|
290 | + |
|
291 | + if ($tokens[$afterParenthesis]['code'] === \T_OPEN_SQUARE_BRACKET |
|
292 | + && isset($tokens[$afterParenthesis]['bracket_closer']) |
|
293 | + ) { |
|
294 | + $afterStackFrame = $phpcsFile->findNext( |
|
295 | + Tokens::$emptyTokens, |
|
296 | + ($tokens[$afterParenthesis]['bracket_closer'] + 1), |
|
297 | + null, |
|
298 | + true |
|
299 | + ); |
|
300 | + |
|
301 | + if ($tokens[$afterStackFrame]['code'] === \T_OPEN_SQUARE_BRACKET |
|
302 | + && isset($tokens[$afterStackFrame]['bracket_closer']) |
|
303 | + ) { |
|
304 | + $arrayIndex = $phpcsFile->findNext( |
|
305 | + \T_CONSTANT_ENCAPSED_STRING, |
|
306 | + ($afterStackFrame + 1), |
|
307 | + $tokens[$afterStackFrame]['bracket_closer'] |
|
308 | + ); |
|
309 | + |
|
310 | + if ($arrayIndex !== false && $this->stripQuotes($tokens[$arrayIndex]['content']) !== 'args') { |
|
311 | + continue; |
|
312 | + } |
|
313 | + } |
|
314 | + } |
|
315 | + } |
|
316 | + |
|
317 | + /* |
|
318 | 318 | * Only check for variables before the start of the statement to |
319 | 319 | * prevent false positives on the return value of the function call |
320 | 320 | * being assigned to one of the parameters, i.e.: |
321 | 321 | * `$param = func_get_args();`. |
322 | 322 | */ |
323 | - $startOfStatement = PHPCSHelper::findStartOfStatement($phpcsFile, $i, $this->ignoreForStartOfStatement); |
|
323 | + $startOfStatement = PHPCSHelper::findStartOfStatement($phpcsFile, $i, $this->ignoreForStartOfStatement); |
|
324 | 324 | |
325 | - /* |
|
325 | + /* |
|
326 | 326 | * Ok, so we've found one of the target functions in the right scope. |
327 | 327 | * Now, let's check if any of the passed parameters were touched. |
328 | 328 | */ |
329 | - $scanResult = 'clean'; |
|
330 | - for ($j = ($scopeOpener + 1); $j < $startOfStatement; $j++) { |
|
331 | - if (isset($this->skipPastNested[$tokens[$j]['type']]) |
|
332 | - && isset($tokens[$j]['scope_closer']) |
|
333 | - ) { |
|
334 | - // Skip past nested structures. |
|
335 | - $j = $tokens[$j]['scope_closer']; |
|
336 | - continue; |
|
337 | - } |
|
338 | - |
|
339 | - if ($tokens[$j]['code'] !== \T_VARIABLE) { |
|
340 | - continue; |
|
341 | - } |
|
342 | - |
|
343 | - if ($foundFunctionName === 'func_get_arg' && isset($argNumber)) { |
|
344 | - if (isset($paramNames[$argNumber]) |
|
345 | - && $tokens[$j]['content'] !== $paramNames[$argNumber] |
|
346 | - ) { |
|
347 | - // Different param than the one requested by func_get_arg(). |
|
348 | - continue; |
|
349 | - } |
|
350 | - } elseif ($foundFunctionName === 'func_get_args' && isset($paramNamesSubset)) { |
|
351 | - if (\in_array($tokens[$j]['content'], $paramNamesSubset, true) === false) { |
|
352 | - // Different param than the ones requested by func_get_args(). |
|
353 | - continue; |
|
354 | - } |
|
355 | - } elseif (\in_array($tokens[$j]['content'], $paramNames, true) === false) { |
|
356 | - // Variable is not one of the function parameters. |
|
357 | - continue; |
|
358 | - } |
|
359 | - |
|
360 | - /* |
|
329 | + $scanResult = 'clean'; |
|
330 | + for ($j = ($scopeOpener + 1); $j < $startOfStatement; $j++) { |
|
331 | + if (isset($this->skipPastNested[$tokens[$j]['type']]) |
|
332 | + && isset($tokens[$j]['scope_closer']) |
|
333 | + ) { |
|
334 | + // Skip past nested structures. |
|
335 | + $j = $tokens[$j]['scope_closer']; |
|
336 | + continue; |
|
337 | + } |
|
338 | + |
|
339 | + if ($tokens[$j]['code'] !== \T_VARIABLE) { |
|
340 | + continue; |
|
341 | + } |
|
342 | + |
|
343 | + if ($foundFunctionName === 'func_get_arg' && isset($argNumber)) { |
|
344 | + if (isset($paramNames[$argNumber]) |
|
345 | + && $tokens[$j]['content'] !== $paramNames[$argNumber] |
|
346 | + ) { |
|
347 | + // Different param than the one requested by func_get_arg(). |
|
348 | + continue; |
|
349 | + } |
|
350 | + } elseif ($foundFunctionName === 'func_get_args' && isset($paramNamesSubset)) { |
|
351 | + if (\in_array($tokens[$j]['content'], $paramNamesSubset, true) === false) { |
|
352 | + // Different param than the ones requested by func_get_args(). |
|
353 | + continue; |
|
354 | + } |
|
355 | + } elseif (\in_array($tokens[$j]['content'], $paramNames, true) === false) { |
|
356 | + // Variable is not one of the function parameters. |
|
357 | + continue; |
|
358 | + } |
|
359 | + |
|
360 | + /* |
|
361 | 361 | * Ok, so we've found a variable which was passed as one of the parameters. |
362 | 362 | * Now, is this variable being changed, i.e. incremented, decremented or |
363 | 363 | * assigned something ? |
364 | 364 | */ |
365 | - $scanResult = 'warning'; |
|
366 | - if (isset($variableToken) === false) { |
|
367 | - $variableToken = $j; |
|
368 | - } |
|
369 | - |
|
370 | - $beforeVar = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($j - 1), null, true); |
|
371 | - if ($beforeVar !== false && isset($this->plusPlusMinusMinus[$tokens[$beforeVar]['code']])) { |
|
372 | - // Variable is being (pre-)incremented/decremented. |
|
373 | - $scanResult = 'error'; |
|
374 | - $variableToken = $j; |
|
375 | - break; |
|
376 | - } |
|
377 | - |
|
378 | - $afterVar = $phpcsFile->findNext(Tokens::$emptyTokens, ($j + 1), null, true); |
|
379 | - if ($afterVar === false) { |
|
380 | - // Shouldn't be possible, but just in case. |
|
381 | - continue; |
|
382 | - } |
|
383 | - |
|
384 | - if (isset($this->plusPlusMinusMinus[$tokens[$afterVar]['code']])) { |
|
385 | - // Variable is being (post-)incremented/decremented. |
|
386 | - $scanResult = 'error'; |
|
387 | - $variableToken = $j; |
|
388 | - break; |
|
389 | - } |
|
390 | - |
|
391 | - if ($tokens[$afterVar]['code'] === \T_OPEN_SQUARE_BRACKET |
|
392 | - && isset($tokens[$afterVar]['bracket_closer']) |
|
393 | - ) { |
|
394 | - // Skip past array access on the variable. |
|
395 | - while (($afterVar = $phpcsFile->findNext(Tokens::$emptyTokens, ($tokens[$afterVar]['bracket_closer'] + 1), null, true)) !== false) { |
|
396 | - if ($tokens[$afterVar]['code'] !== \T_OPEN_SQUARE_BRACKET |
|
397 | - || isset($tokens[$afterVar]['bracket_closer']) === false |
|
398 | - ) { |
|
399 | - break; |
|
400 | - } |
|
401 | - } |
|
402 | - } |
|
403 | - |
|
404 | - if ($afterVar !== false |
|
405 | - && isset(Tokens::$assignmentTokens[$tokens[$afterVar]['code']]) |
|
406 | - ) { |
|
407 | - // Variable is being assigned something. |
|
408 | - $scanResult = 'error'; |
|
409 | - $variableToken = $j; |
|
410 | - break; |
|
411 | - } |
|
412 | - } |
|
413 | - |
|
414 | - unset($argNumber, $paramNamesSubset); |
|
415 | - |
|
416 | - if ($scanResult === 'clean') { |
|
417 | - continue; |
|
418 | - } |
|
419 | - |
|
420 | - $error = 'Since PHP 7.0, functions inspecting arguments, like %1$s(), no longer report the original value as passed to a parameter, but will instead provide the current value. The parameter "%2$s" was %4$s on line %3$s.'; |
|
421 | - $data = array( |
|
422 | - $foundFunctionName, |
|
423 | - $tokens[$variableToken]['content'], |
|
424 | - $tokens[$variableToken]['line'], |
|
425 | - ); |
|
426 | - |
|
427 | - if ($scanResult === 'error') { |
|
428 | - $data[] = 'changed'; |
|
429 | - $phpcsFile->addError($error, $i, 'Changed', $data); |
|
430 | - |
|
431 | - } elseif ($scanResult === 'warning') { |
|
432 | - $data[] = 'used, and possibly changed (by reference),'; |
|
433 | - $phpcsFile->addWarning($error, $i, 'NeedsInspection', $data); |
|
434 | - } |
|
435 | - |
|
436 | - unset($variableToken); |
|
437 | - } |
|
438 | - } |
|
365 | + $scanResult = 'warning'; |
|
366 | + if (isset($variableToken) === false) { |
|
367 | + $variableToken = $j; |
|
368 | + } |
|
369 | + |
|
370 | + $beforeVar = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($j - 1), null, true); |
|
371 | + if ($beforeVar !== false && isset($this->plusPlusMinusMinus[$tokens[$beforeVar]['code']])) { |
|
372 | + // Variable is being (pre-)incremented/decremented. |
|
373 | + $scanResult = 'error'; |
|
374 | + $variableToken = $j; |
|
375 | + break; |
|
376 | + } |
|
377 | + |
|
378 | + $afterVar = $phpcsFile->findNext(Tokens::$emptyTokens, ($j + 1), null, true); |
|
379 | + if ($afterVar === false) { |
|
380 | + // Shouldn't be possible, but just in case. |
|
381 | + continue; |
|
382 | + } |
|
383 | + |
|
384 | + if (isset($this->plusPlusMinusMinus[$tokens[$afterVar]['code']])) { |
|
385 | + // Variable is being (post-)incremented/decremented. |
|
386 | + $scanResult = 'error'; |
|
387 | + $variableToken = $j; |
|
388 | + break; |
|
389 | + } |
|
390 | + |
|
391 | + if ($tokens[$afterVar]['code'] === \T_OPEN_SQUARE_BRACKET |
|
392 | + && isset($tokens[$afterVar]['bracket_closer']) |
|
393 | + ) { |
|
394 | + // Skip past array access on the variable. |
|
395 | + while (($afterVar = $phpcsFile->findNext(Tokens::$emptyTokens, ($tokens[$afterVar]['bracket_closer'] + 1), null, true)) !== false) { |
|
396 | + if ($tokens[$afterVar]['code'] !== \T_OPEN_SQUARE_BRACKET |
|
397 | + || isset($tokens[$afterVar]['bracket_closer']) === false |
|
398 | + ) { |
|
399 | + break; |
|
400 | + } |
|
401 | + } |
|
402 | + } |
|
403 | + |
|
404 | + if ($afterVar !== false |
|
405 | + && isset(Tokens::$assignmentTokens[$tokens[$afterVar]['code']]) |
|
406 | + ) { |
|
407 | + // Variable is being assigned something. |
|
408 | + $scanResult = 'error'; |
|
409 | + $variableToken = $j; |
|
410 | + break; |
|
411 | + } |
|
412 | + } |
|
413 | + |
|
414 | + unset($argNumber, $paramNamesSubset); |
|
415 | + |
|
416 | + if ($scanResult === 'clean') { |
|
417 | + continue; |
|
418 | + } |
|
419 | + |
|
420 | + $error = 'Since PHP 7.0, functions inspecting arguments, like %1$s(), no longer report the original value as passed to a parameter, but will instead provide the current value. The parameter "%2$s" was %4$s on line %3$s.'; |
|
421 | + $data = array( |
|
422 | + $foundFunctionName, |
|
423 | + $tokens[$variableToken]['content'], |
|
424 | + $tokens[$variableToken]['line'], |
|
425 | + ); |
|
426 | + |
|
427 | + if ($scanResult === 'error') { |
|
428 | + $data[] = 'changed'; |
|
429 | + $phpcsFile->addError($error, $i, 'Changed', $data); |
|
430 | + |
|
431 | + } elseif ($scanResult === 'warning') { |
|
432 | + $data[] = 'used, and possibly changed (by reference),'; |
|
433 | + $phpcsFile->addWarning($error, $i, 'NeedsInspection', $data); |
|
434 | + } |
|
435 | + |
|
436 | + unset($variableToken); |
|
437 | + } |
|
438 | + } |
|
439 | 439 | } |
@@ -117,48 +117,48 @@ discard block |
||
117 | 117 | * |
118 | 118 | * @return void |
119 | 119 | */ |
120 | - public function process(File $phpcsFile, $stackPtr) |
|
120 | + public function process( File $phpcsFile, $stackPtr ) |
|
121 | 121 | { |
122 | - if ($this->supportsAbove('7.0') === false) { |
|
122 | + if ( $this->supportsAbove( '7.0' ) === false ) { |
|
123 | 123 | return; |
124 | 124 | } |
125 | 125 | |
126 | 126 | $tokens = $phpcsFile->getTokens(); |
127 | 127 | |
128 | - if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { |
|
128 | + if ( isset( $tokens[ $stackPtr ][ 'scope_opener' ], $tokens[ $stackPtr ][ 'scope_closer' ] ) === false ) { |
|
129 | 129 | // Abstract function, interface function, live coding or parse error. |
130 | 130 | return; |
131 | 131 | } |
132 | 132 | |
133 | - $scopeOpener = $tokens[$stackPtr]['scope_opener']; |
|
134 | - $scopeCloser = $tokens[$stackPtr]['scope_closer']; |
|
133 | + $scopeOpener = $tokens[ $stackPtr ][ 'scope_opener' ]; |
|
134 | + $scopeCloser = $tokens[ $stackPtr ][ 'scope_closer' ]; |
|
135 | 135 | |
136 | 136 | // Does the function declaration have parameters ? |
137 | - $params = PHPCSHelper::getMethodParameters($phpcsFile, $stackPtr); |
|
138 | - if (empty($params)) { |
|
137 | + $params = PHPCSHelper::getMethodParameters( $phpcsFile, $stackPtr ); |
|
138 | + if ( empty( $params ) ) { |
|
139 | 139 | // No named arguments found, so no risk of them being changed. |
140 | 140 | return; |
141 | 141 | } |
142 | 142 | |
143 | 143 | $paramNames = array(); |
144 | - foreach ($params as $param) { |
|
145 | - $paramNames[] = $param['name']; |
|
144 | + foreach ( $params as $param ) { |
|
145 | + $paramNames[ ] = $param[ 'name' ]; |
|
146 | 146 | } |
147 | 147 | |
148 | - for ($i = ($scopeOpener + 1); $i < $scopeCloser; $i++) { |
|
149 | - if (isset($this->skipPastNested[$tokens[$i]['type']]) && isset($tokens[$i]['scope_closer'])) { |
|
148 | + for ( $i = ( $scopeOpener + 1 ); $i < $scopeCloser; $i++ ) { |
|
149 | + if ( isset( $this->skipPastNested[ $tokens[ $i ][ 'type' ] ] ) && isset( $tokens[ $i ][ 'scope_closer' ] ) ) { |
|
150 | 150 | // Skip past nested structures. |
151 | - $i = $tokens[$i]['scope_closer']; |
|
151 | + $i = $tokens[ $i ][ 'scope_closer' ]; |
|
152 | 152 | continue; |
153 | 153 | } |
154 | 154 | |
155 | - if ($tokens[$i]['code'] !== \T_STRING) { |
|
155 | + if ( $tokens[ $i ][ 'code' ] !== \T_STRING ) { |
|
156 | 156 | continue; |
157 | 157 | } |
158 | 158 | |
159 | - $foundFunctionName = strtolower($tokens[$i]['content']); |
|
159 | + $foundFunctionName = strtolower( $tokens[ $i ][ 'content' ] ); |
|
160 | 160 | |
161 | - if (isset($this->changedFunctions[$foundFunctionName]) === false) { |
|
161 | + if ( isset( $this->changedFunctions[ $foundFunctionName ] ) === false ) { |
|
162 | 162 | // Not one of the target functions. |
163 | 163 | continue; |
164 | 164 | } |
@@ -166,22 +166,22 @@ discard block |
||
166 | 166 | /* |
167 | 167 | * Ok, so is this really a function call to one of the PHP native functions ? |
168 | 168 | */ |
169 | - $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
|
170 | - if ($next === false || $tokens[$next]['code'] !== \T_OPEN_PARENTHESIS) { |
|
169 | + $next = $phpcsFile->findNext( Tokens::$emptyTokens, ( $i + 1 ), null, true ); |
|
170 | + if ( $next === false || $tokens[ $next ][ 'code' ] !== \T_OPEN_PARENTHESIS ) { |
|
171 | 171 | // Live coding, parse error or not a function call. |
172 | 172 | continue; |
173 | 173 | } |
174 | 174 | |
175 | - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true); |
|
176 | - if ($prev !== false) { |
|
177 | - if (isset($this->noneFunctionCallIndicators[$tokens[$prev]['code']])) { |
|
175 | + $prev = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $i - 1 ), null, true ); |
|
176 | + if ( $prev !== false ) { |
|
177 | + if ( isset( $this->noneFunctionCallIndicators[ $tokens[ $prev ][ 'code' ] ] ) ) { |
|
178 | 178 | continue; |
179 | 179 | } |
180 | 180 | |
181 | 181 | // Check for namespaced functions, ie: \foo\bar() not \bar(). |
182 | - if ($tokens[ $prev ]['code'] === \T_NS_SEPARATOR) { |
|
183 | - $pprev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true); |
|
184 | - if ($pprev !== false && $tokens[ $pprev ]['code'] === \T_STRING) { |
|
182 | + if ( $tokens[ $prev ][ 'code' ] === \T_NS_SEPARATOR ) { |
|
183 | + $pprev = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $prev - 1 ), null, true ); |
|
184 | + if ( $pprev !== false && $tokens[ $pprev ][ 'code' ] === \T_STRING ) { |
|
185 | 185 | continue; |
186 | 186 | } |
187 | 187 | } |
@@ -190,10 +190,10 @@ discard block |
||
190 | 190 | /* |
191 | 191 | * Address some special cases. |
192 | 192 | */ |
193 | - if ($foundFunctionName !== 'func_get_args') { |
|
194 | - $paramOne = $this->getFunctionCallParameter($phpcsFile, $i, 1); |
|
195 | - if ($paramOne !== false) { |
|
196 | - switch ($foundFunctionName) { |
|
193 | + if ( $foundFunctionName !== 'func_get_args' ) { |
|
194 | + $paramOne = $this->getFunctionCallParameter( $phpcsFile, $i, 1 ); |
|
195 | + if ( $paramOne !== false ) { |
|
196 | + switch ( $foundFunctionName ) { |
|
197 | 197 | /* |
198 | 198 | * Check if `debug_(print_)backtrace()` is called with the |
199 | 199 | * `DEBUG_BACKTRACE_IGNORE_ARGS` option. |
@@ -202,13 +202,13 @@ discard block |
||
202 | 202 | case 'debug_print_backtrace': |
203 | 203 | $hasIgnoreArgs = $phpcsFile->findNext( |
204 | 204 | \T_STRING, |
205 | - $paramOne['start'], |
|
206 | - ($paramOne['end'] + 1), |
|
205 | + $paramOne[ 'start' ], |
|
206 | + ( $paramOne[ 'end' ] + 1 ), |
|
207 | 207 | false, |
208 | 208 | 'DEBUG_BACKTRACE_IGNORE_ARGS' |
209 | 209 | ); |
210 | 210 | |
211 | - if ($hasIgnoreArgs !== false) { |
|
211 | + if ( $hasIgnoreArgs !== false ) { |
|
212 | 212 | // Debug_backtrace() called with ignore args option. |
213 | 213 | continue 2; |
214 | 214 | } |
@@ -225,11 +225,11 @@ discard block |
||
225 | 225 | * Should be exceptionally rare and can - if needs be - be addressed at a later stage.}} |
226 | 226 | */ |
227 | 227 | case 'func_get_arg': |
228 | - $number = $phpcsFile->findNext(\T_LNUMBER, $paramOne['start'], ($paramOne['end'] + 1)); |
|
229 | - if ($number !== false) { |
|
230 | - $argNumber = $tokens[$number]['content']; |
|
228 | + $number = $phpcsFile->findNext( \T_LNUMBER, $paramOne[ 'start' ], ( $paramOne[ 'end' ] + 1 ) ); |
|
229 | + if ( $number !== false ) { |
|
230 | + $argNumber = $tokens[ $number ][ 'content' ]; |
|
231 | 231 | |
232 | - if (isset($paramNames[$argNumber]) === false) { |
|
232 | + if ( isset( $paramNames[ $argNumber ] ) === false ) { |
|
233 | 233 | // Requesting a non-named additional parameter. Ignore. |
234 | 234 | continue 2; |
235 | 235 | } |
@@ -246,28 +246,28 @@ discard block |
||
246 | 246 | * {@internal Note: This does not take offset calculations into account! |
247 | 247 | * Should be exceptionally rare and can - if needs be - be addressed at a later stage.}} |
248 | 248 | */ |
249 | - if ($prev !== false && $tokens[$prev]['code'] === \T_OPEN_PARENTHESIS) { |
|
249 | + if ( $prev !== false && $tokens[ $prev ][ 'code' ] === \T_OPEN_PARENTHESIS ) { |
|
250 | 250 | |
251 | - $maybeFunctionCall = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true); |
|
252 | - if ($maybeFunctionCall !== false |
|
253 | - && $tokens[$maybeFunctionCall]['code'] === \T_STRING |
|
254 | - && ($tokens[$maybeFunctionCall]['content'] === 'array_slice' |
|
255 | - || $tokens[$maybeFunctionCall]['content'] === 'array_splice') |
|
251 | + $maybeFunctionCall = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $prev - 1 ), null, true ); |
|
252 | + if ( $maybeFunctionCall !== false |
|
253 | + && $tokens[ $maybeFunctionCall ][ 'code' ] === \T_STRING |
|
254 | + && ( $tokens[ $maybeFunctionCall ][ 'content' ] === 'array_slice' |
|
255 | + || $tokens[ $maybeFunctionCall ][ 'content' ] === 'array_splice' ) |
|
256 | 256 | ) { |
257 | - $parentFuncParamTwo = $this->getFunctionCallParameter($phpcsFile, $maybeFunctionCall, 2); |
|
257 | + $parentFuncParamTwo = $this->getFunctionCallParameter( $phpcsFile, $maybeFunctionCall, 2 ); |
|
258 | 258 | $number = $phpcsFile->findNext( |
259 | 259 | \T_LNUMBER, |
260 | - $parentFuncParamTwo['start'], |
|
261 | - ($parentFuncParamTwo['end'] + 1) |
|
260 | + $parentFuncParamTwo[ 'start' ], |
|
261 | + ( $parentFuncParamTwo[ 'end' ] + 1 ) |
|
262 | 262 | ); |
263 | 263 | |
264 | - if ($number !== false && isset($paramNames[$tokens[$number]['content']]) === false) { |
|
264 | + if ( $number !== false && isset( $paramNames[ $tokens[ $number ][ 'content' ] ] ) === false ) { |
|
265 | 265 | // Requesting non-named additional parameters. Ignore. |
266 | - continue ; |
|
266 | + continue; |
|
267 | 267 | } |
268 | 268 | |
269 | 269 | // Slice starts at a named argument, but we know which params are being accessed. |
270 | - $paramNamesSubset = \array_slice($paramNames, $tokens[$number]['content']); |
|
270 | + $paramNamesSubset = \array_slice( $paramNames, $tokens[ $number ][ 'content' ] ); |
|
271 | 271 | } |
272 | 272 | } |
273 | 273 | } |
@@ -280,34 +280,34 @@ discard block |
||
280 | 280 | * |
281 | 281 | * Note: We already know that $next is the open parenthesis of the function call. |
282 | 282 | */ |
283 | - if ($foundFunctionName === 'debug_backtrace' && isset($tokens[$next]['parenthesis_closer'])) { |
|
283 | + if ( $foundFunctionName === 'debug_backtrace' && isset( $tokens[ $next ][ 'parenthesis_closer' ] ) ) { |
|
284 | 284 | $afterParenthesis = $phpcsFile->findNext( |
285 | 285 | Tokens::$emptyTokens, |
286 | - ($tokens[$next]['parenthesis_closer'] + 1), |
|
286 | + ( $tokens[ $next ][ 'parenthesis_closer' ] + 1 ), |
|
287 | 287 | null, |
288 | 288 | true |
289 | 289 | ); |
290 | 290 | |
291 | - if ($tokens[$afterParenthesis]['code'] === \T_OPEN_SQUARE_BRACKET |
|
292 | - && isset($tokens[$afterParenthesis]['bracket_closer']) |
|
291 | + if ( $tokens[ $afterParenthesis ][ 'code' ] === \T_OPEN_SQUARE_BRACKET |
|
292 | + && isset( $tokens[ $afterParenthesis ][ 'bracket_closer' ] ) |
|
293 | 293 | ) { |
294 | 294 | $afterStackFrame = $phpcsFile->findNext( |
295 | 295 | Tokens::$emptyTokens, |
296 | - ($tokens[$afterParenthesis]['bracket_closer'] + 1), |
|
296 | + ( $tokens[ $afterParenthesis ][ 'bracket_closer' ] + 1 ), |
|
297 | 297 | null, |
298 | 298 | true |
299 | 299 | ); |
300 | 300 | |
301 | - if ($tokens[$afterStackFrame]['code'] === \T_OPEN_SQUARE_BRACKET |
|
302 | - && isset($tokens[$afterStackFrame]['bracket_closer']) |
|
301 | + if ( $tokens[ $afterStackFrame ][ 'code' ] === \T_OPEN_SQUARE_BRACKET |
|
302 | + && isset( $tokens[ $afterStackFrame ][ 'bracket_closer' ] ) |
|
303 | 303 | ) { |
304 | 304 | $arrayIndex = $phpcsFile->findNext( |
305 | 305 | \T_CONSTANT_ENCAPSED_STRING, |
306 | - ($afterStackFrame + 1), |
|
307 | - $tokens[$afterStackFrame]['bracket_closer'] |
|
306 | + ( $afterStackFrame + 1 ), |
|
307 | + $tokens[ $afterStackFrame ][ 'bracket_closer' ] |
|
308 | 308 | ); |
309 | 309 | |
310 | - if ($arrayIndex !== false && $this->stripQuotes($tokens[$arrayIndex]['content']) !== 'args') { |
|
310 | + if ( $arrayIndex !== false && $this->stripQuotes( $tokens[ $arrayIndex ][ 'content' ] ) !== 'args' ) { |
|
311 | 311 | continue; |
312 | 312 | } |
313 | 313 | } |
@@ -320,39 +320,39 @@ discard block |
||
320 | 320 | * being assigned to one of the parameters, i.e.: |
321 | 321 | * `$param = func_get_args();`. |
322 | 322 | */ |
323 | - $startOfStatement = PHPCSHelper::findStartOfStatement($phpcsFile, $i, $this->ignoreForStartOfStatement); |
|
323 | + $startOfStatement = PHPCSHelper::findStartOfStatement( $phpcsFile, $i, $this->ignoreForStartOfStatement ); |
|
324 | 324 | |
325 | 325 | /* |
326 | 326 | * Ok, so we've found one of the target functions in the right scope. |
327 | 327 | * Now, let's check if any of the passed parameters were touched. |
328 | 328 | */ |
329 | 329 | $scanResult = 'clean'; |
330 | - for ($j = ($scopeOpener + 1); $j < $startOfStatement; $j++) { |
|
331 | - if (isset($this->skipPastNested[$tokens[$j]['type']]) |
|
332 | - && isset($tokens[$j]['scope_closer']) |
|
330 | + for ( $j = ( $scopeOpener + 1 ); $j < $startOfStatement; $j++ ) { |
|
331 | + if ( isset( $this->skipPastNested[ $tokens[ $j ][ 'type' ] ] ) |
|
332 | + && isset( $tokens[ $j ][ 'scope_closer' ] ) |
|
333 | 333 | ) { |
334 | 334 | // Skip past nested structures. |
335 | - $j = $tokens[$j]['scope_closer']; |
|
335 | + $j = $tokens[ $j ][ 'scope_closer' ]; |
|
336 | 336 | continue; |
337 | 337 | } |
338 | 338 | |
339 | - if ($tokens[$j]['code'] !== \T_VARIABLE) { |
|
339 | + if ( $tokens[ $j ][ 'code' ] !== \T_VARIABLE ) { |
|
340 | 340 | continue; |
341 | 341 | } |
342 | 342 | |
343 | - if ($foundFunctionName === 'func_get_arg' && isset($argNumber)) { |
|
344 | - if (isset($paramNames[$argNumber]) |
|
345 | - && $tokens[$j]['content'] !== $paramNames[$argNumber] |
|
343 | + if ( $foundFunctionName === 'func_get_arg' && isset( $argNumber ) ) { |
|
344 | + if ( isset( $paramNames[ $argNumber ] ) |
|
345 | + && $tokens[ $j ][ 'content' ] !== $paramNames[ $argNumber ] |
|
346 | 346 | ) { |
347 | 347 | // Different param than the one requested by func_get_arg(). |
348 | 348 | continue; |
349 | 349 | } |
350 | - } elseif ($foundFunctionName === 'func_get_args' && isset($paramNamesSubset)) { |
|
351 | - if (\in_array($tokens[$j]['content'], $paramNamesSubset, true) === false) { |
|
350 | + } elseif ( $foundFunctionName === 'func_get_args' && isset( $paramNamesSubset ) ) { |
|
351 | + if ( \in_array( $tokens[ $j ][ 'content' ], $paramNamesSubset, true ) === false ) { |
|
352 | 352 | // Different param than the ones requested by func_get_args(). |
353 | 353 | continue; |
354 | 354 | } |
355 | - } elseif (\in_array($tokens[$j]['content'], $paramNames, true) === false) { |
|
355 | + } elseif ( \in_array( $tokens[ $j ][ 'content' ], $paramNames, true ) === false ) { |
|
356 | 356 | // Variable is not one of the function parameters. |
357 | 357 | continue; |
358 | 358 | } |
@@ -363,46 +363,46 @@ discard block |
||
363 | 363 | * assigned something ? |
364 | 364 | */ |
365 | 365 | $scanResult = 'warning'; |
366 | - if (isset($variableToken) === false) { |
|
366 | + if ( isset( $variableToken ) === false ) { |
|
367 | 367 | $variableToken = $j; |
368 | 368 | } |
369 | 369 | |
370 | - $beforeVar = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($j - 1), null, true); |
|
371 | - if ($beforeVar !== false && isset($this->plusPlusMinusMinus[$tokens[$beforeVar]['code']])) { |
|
370 | + $beforeVar = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $j - 1 ), null, true ); |
|
371 | + if ( $beforeVar !== false && isset( $this->plusPlusMinusMinus[ $tokens[ $beforeVar ][ 'code' ] ] ) ) { |
|
372 | 372 | // Variable is being (pre-)incremented/decremented. |
373 | 373 | $scanResult = 'error'; |
374 | 374 | $variableToken = $j; |
375 | 375 | break; |
376 | 376 | } |
377 | 377 | |
378 | - $afterVar = $phpcsFile->findNext(Tokens::$emptyTokens, ($j + 1), null, true); |
|
379 | - if ($afterVar === false) { |
|
378 | + $afterVar = $phpcsFile->findNext( Tokens::$emptyTokens, ( $j + 1 ), null, true ); |
|
379 | + if ( $afterVar === false ) { |
|
380 | 380 | // Shouldn't be possible, but just in case. |
381 | 381 | continue; |
382 | 382 | } |
383 | 383 | |
384 | - if (isset($this->plusPlusMinusMinus[$tokens[$afterVar]['code']])) { |
|
384 | + if ( isset( $this->plusPlusMinusMinus[ $tokens[ $afterVar ][ 'code' ] ] ) ) { |
|
385 | 385 | // Variable is being (post-)incremented/decremented. |
386 | 386 | $scanResult = 'error'; |
387 | 387 | $variableToken = $j; |
388 | 388 | break; |
389 | 389 | } |
390 | 390 | |
391 | - if ($tokens[$afterVar]['code'] === \T_OPEN_SQUARE_BRACKET |
|
392 | - && isset($tokens[$afterVar]['bracket_closer']) |
|
391 | + if ( $tokens[ $afterVar ][ 'code' ] === \T_OPEN_SQUARE_BRACKET |
|
392 | + && isset( $tokens[ $afterVar ][ 'bracket_closer' ] ) |
|
393 | 393 | ) { |
394 | 394 | // Skip past array access on the variable. |
395 | - while (($afterVar = $phpcsFile->findNext(Tokens::$emptyTokens, ($tokens[$afterVar]['bracket_closer'] + 1), null, true)) !== false) { |
|
396 | - if ($tokens[$afterVar]['code'] !== \T_OPEN_SQUARE_BRACKET |
|
397 | - || isset($tokens[$afterVar]['bracket_closer']) === false |
|
395 | + while ( ( $afterVar = $phpcsFile->findNext( Tokens::$emptyTokens, ( $tokens[ $afterVar ][ 'bracket_closer' ] + 1 ), null, true ) ) !== false ) { |
|
396 | + if ( $tokens[ $afterVar ][ 'code' ] !== \T_OPEN_SQUARE_BRACKET |
|
397 | + || isset( $tokens[ $afterVar ][ 'bracket_closer' ] ) === false |
|
398 | 398 | ) { |
399 | 399 | break; |
400 | 400 | } |
401 | 401 | } |
402 | 402 | } |
403 | 403 | |
404 | - if ($afterVar !== false |
|
405 | - && isset(Tokens::$assignmentTokens[$tokens[$afterVar]['code']]) |
|
404 | + if ( $afterVar !== false |
|
405 | + && isset( Tokens::$assignmentTokens[ $tokens[ $afterVar ][ 'code' ] ] ) |
|
406 | 406 | ) { |
407 | 407 | // Variable is being assigned something. |
408 | 408 | $scanResult = 'error'; |
@@ -411,29 +411,29 @@ discard block |
||
411 | 411 | } |
412 | 412 | } |
413 | 413 | |
414 | - unset($argNumber, $paramNamesSubset); |
|
414 | + unset( $argNumber, $paramNamesSubset ); |
|
415 | 415 | |
416 | - if ($scanResult === 'clean') { |
|
416 | + if ( $scanResult === 'clean' ) { |
|
417 | 417 | continue; |
418 | 418 | } |
419 | 419 | |
420 | 420 | $error = 'Since PHP 7.0, functions inspecting arguments, like %1$s(), no longer report the original value as passed to a parameter, but will instead provide the current value. The parameter "%2$s" was %4$s on line %3$s.'; |
421 | 421 | $data = array( |
422 | 422 | $foundFunctionName, |
423 | - $tokens[$variableToken]['content'], |
|
424 | - $tokens[$variableToken]['line'], |
|
423 | + $tokens[ $variableToken ][ 'content' ], |
|
424 | + $tokens[ $variableToken ][ 'line' ], |
|
425 | 425 | ); |
426 | 426 | |
427 | - if ($scanResult === 'error') { |
|
428 | - $data[] = 'changed'; |
|
429 | - $phpcsFile->addError($error, $i, 'Changed', $data); |
|
427 | + if ( $scanResult === 'error' ) { |
|
428 | + $data[ ] = 'changed'; |
|
429 | + $phpcsFile->addError( $error, $i, 'Changed', $data ); |
|
430 | 430 | |
431 | - } elseif ($scanResult === 'warning') { |
|
432 | - $data[] = 'used, and possibly changed (by reference),'; |
|
433 | - $phpcsFile->addWarning($error, $i, 'NeedsInspection', $data); |
|
431 | + } elseif ( $scanResult === 'warning' ) { |
|
432 | + $data[ ] = 'used, and possibly changed (by reference),'; |
|
433 | + $phpcsFile->addWarning( $error, $i, 'NeedsInspection', $data ); |
|
434 | 434 | } |
435 | 435 | |
436 | - unset($variableToken); |
|
436 | + unset( $variableToken ); |
|
437 | 437 | } |
438 | 438 | } |
439 | 439 | } |
@@ -27,8 +27,7 @@ discard block |
||
27 | 27 | * |
28 | 28 | * @since 9.1.0 |
29 | 29 | */ |
30 | -class ArgumentFunctionsReportCurrentValueSniff extends Sniff |
|
31 | -{ |
|
30 | +class ArgumentFunctionsReportCurrentValueSniff extends Sniff { |
|
32 | 31 | |
33 | 32 | /** |
34 | 33 | * A list of functions that, when called, can behave differently in PHP 7 |
@@ -100,8 +99,7 @@ discard block |
||
100 | 99 | * |
101 | 100 | * @return array |
102 | 101 | */ |
103 | - public function register() |
|
104 | - { |
|
102 | + public function register() { |
|
105 | 103 | return array( |
106 | 104 | \T_FUNCTION, |
107 | 105 | \T_CLOSURE, |
@@ -117,8 +115,7 @@ discard block |
||
117 | 115 | * |
118 | 116 | * @return void |
119 | 117 | */ |
120 | - public function process(File $phpcsFile, $stackPtr) |
|
121 | - { |
|
118 | + public function process(File $phpcsFile, $stackPtr) { |
|
122 | 119 | if ($this->supportsAbove('7.0') === false) { |
123 | 120 | return; |
124 | 121 | } |