Skip to content

Commit

Permalink
Fixed bug squizlabs#772 : Syntax error when using PHPCBF on alternati…
Browse files Browse the repository at this point in the history
…ve style foreach loops
  • Loading branch information
gsherwood committed Nov 18, 2015
1 parent 9f195e4 commit e16cf78
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
for ($next = ($opener + 1); $next < $phpcsFile->numTokens; $next++) {
$code = $tokens[$next]['code'];

if ($code === T_WHITESPACE) {
if ($code === T_WHITESPACE
|| ($code === T_INLINE_HTML
&& trim($tokens[$next]['content']) === '')
) {
continue;
}

Expand All @@ -167,7 +170,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
// We found the first bit of a code, or a comment on the
// following line.
break;
}
}//end for

$found = ($tokens[$next]['line'] - $tokens[$opener]['line']);
if ($found !== 1) {
Expand All @@ -181,13 +184,18 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
break;
}

if (trim($tokens[$i]['content']) !== '') {
break;
}

// Remove whitespace.
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->addContent($opener, $phpcsFile->eolChar);
$phpcsFile->fixer->endChangeset();
}
}
}//end if
} else if ($tokens[$stackPtr]['code'] === T_WHILE) {
// Zero spaces after parenthesis closer.
$closer = $tokens[$stackPtr]['parenthesis_closer'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,17 @@ if ($foo === 1) {
</td></tr></table>
<?php
}

?>
<?php foreach($formset['Fieldset'] as $fieldset): ?>
<?php foreach($fieldset['Field'] as $field): ?>
<?php endforeach; ?>
<?php endforeach; ?>

<?php foreach ($formset['Fieldset'] as $fieldset) : ?> hello
<?php endforeach; ?>

<?php foreach ($formset['Fieldset'] as $fieldset) :
?>
hello
<?php endforeach; ?>
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,18 @@ if ($foo === 1) {
</td></tr></table>
<?php
}

?>
<?php foreach ($formset['Fieldset'] as $fieldset) : ?>
<?php foreach ($fieldset['Field'] as $field) : ?>
<?php endforeach; ?>
<?php endforeach; ?>

<?php foreach ($formset['Fieldset'] as $fieldset) :
?> hello
<?php endforeach; ?>

<?php foreach ($formset['Fieldset'] as $fieldset) :
?>
hello
<?php endforeach; ?>
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public function getErrorList($testFile='ControlSignatureUnitTest.inc')
$errors[158] = 2;
$errors[165] = 1;
$errors[170] = 2;
$errors[186] = 2;
$errors[187] = 2;
$errors[191] = 1;
}

return $errors;
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #768 : PEAR FunctionCallSignature sniff forbids comments after opening parenthesis of a multiline call
- Fixed bug #769 : Incorrect detection of variable reference operator when used with short array syntax
-- Thanks to Klaus Purer for the patch
- Fixed bug #772 : Syntax error when using PHPCBF on alternative style foreach loops
</notes>
<contents>
<dir name="/">
Expand Down

0 comments on commit e16cf78

Please sign in to comment.