Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for PHP tokenizing of T_INLINE_ELSE colons. Fixes bug #…

…19666 : PSR1.Files.SideEffects throws a notice Undefined index: scope_closer
  • Loading branch information...
commit 5ea94def4cb705abcc51f1adf9d116021715f935 1 parent 5369695
@gsherwood gsherwood authored
View
9 CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/InlineIfDeclarationSniff.php
@@ -108,13 +108,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterThen', $data);
}
- // If there is an else in this condition, make sure it has correct spacing.
- $inlineElse = $phpcsFile->findNext(T_COLON, ($stackPtr + 1), $statementEnd, false);
- if ($inlineElse === false) {
- // No else condition.
- return;
- }
-
+ // Make sure the ELSE has the correct spacing.
+ $inlineElse = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1), $statementEnd, false);
$contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($inlineElse - 1), null, true);
$contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($inlineElse + 1), null, true);
View
2  CodeSniffer/Standards/Squiz/Sniffs/Objects/ObjectInstantiationSniff.php
@@ -67,7 +67,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
T_THROW,
T_RETURN,
T_INLINE_THEN,
- T_COLON,
+ T_INLINE_ELSE,
);
if (in_array($tokens[$prev]['code'], $allowedTokens) === false) {
View
9 CodeSniffer/Standards/Squiz/Sniffs/PHP/DisallowMultipleAssignmentsSniff.php
@@ -146,12 +146,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
return;
}
- if ($tokens[$i]['code'] === T_COLON) {
- $then = $phpcsFile->findPrevious(T_INLINE_THEN, ($i - 1), null, false, null, true);
- if ($then !== false) {
- // We reached the end of the inline ELSE statement.
- return;
- }
+ if ($tokens[$i]['code'] === T_INLINE_ELSE) {
+ // We reached the end of the inline ELSE statement.
+ return;
}
if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
View
17 CodeSniffer/Tokenizers/PHP.php
@@ -243,6 +243,9 @@ public function tokenizeString($string, $eolChar='\n')
$newStackPtr = 0;
$numTokens = count($tokens);
+
+ $insideInlineIf = false;
+
for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) {
$token = $tokens[$stackPtr];
$tokenIsArray = is_array($token);
@@ -453,8 +456,18 @@ public function tokenizeString($string, $eolChar='\n')
} else {
$newToken = PHP_CodeSniffer::standardiseToken($token);
- // This is a special condition for T_ARRAY tokens use to
- // type hint function arguments as being arrays. We want to keep
+ // Convert colons that are actually the ELSE component of an
+ // inline IF statement.
+ if ($newToken['code'] === T_INLINE_THEN) {
+ $insideInlineIf = true;
+ } else if ($insideInlineIf === true && $newToken['code'] === T_COLON) {
+ $insideInlineIf = false;
+ $newToken['code'] = T_INLINE_ELSE;
+ $newToken['type'] = 'T_INLINE_ELSE';
+ }
+
+ // This is a special condition for T_ARRAY tokens used for
+ // type hinting function arguments as being arrays. We want to keep
// the parenthsis map clean, so let's tag these tokens as
// T_ARRAY_HINT.
if ($newToken['code'] === T_ARRAY) {
View
11 CodeSniffer/Tokens.php
@@ -24,11 +24,12 @@
define('T_COLON', 1006);
define('T_STRING_CONCAT', 1007);
define('T_INLINE_THEN', 1008);
-define('T_NULL', 1009);
-define('T_FALSE', 1010);
-define('T_TRUE', 1011);
-define('T_SEMICOLON', 1012);
-define('T_EQUAL', 1013);
+define('T_INLINE_ELSE', 1009);
+define('T_NULL', 1010);
+define('T_FALSE', 1011);
+define('T_TRUE', 1012);
+define('T_SEMICOLON', 1013);
+define('T_EQUAL', 1014);
define('T_MULTIPLY', 1015);
define('T_DIVIDE', 1016);
define('T_PLUS', 1017);
View
2  package.xml
@@ -37,6 +37,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Sniffs inside PHP 5.3 namespaces are now supported, along with the existing underscore-style emulated namespaces
-- For example: namespace MyStandard\Sniffs\Arrays; class ArrayDeclarationSniff implements \PHP_CodeSniffer_Sniff { ...
-- Thanks to Till Klampaeckel for the patch
+ - Added support for PHP tokenizing of T_INLINE_ELSE colons, so this token type is now available
+ -- Fixes bug #19666 : PSR1.Files.SideEffects throws a notice Undefined index: scope_closer
- Added Generic DisallowSpaceIndent to check that files are indented using tabs
- Added Generic OneClassPerFileSniff to check that only one class is defined in each file
-- Thanks to Andy Grunwald for the contribution
Please sign in to comment.
Something went wrong with that request. Please try again.