Skip to content

Commit 9716919

Browse files
committed
Generic/InlineControlStructure: bug fix
Prevent creating incorrect fixes and getting into a fixer conflict when both short and long open tags are used in combination with alternative control structures and the short open tags are not recognized because the system on which the scan is run has `short_open_tag=Off`. Solved by bowing out early when an alternative control structure is detected which was not recognized as such (`scope_opener` not set). Fixes 2565
1 parent 886c162 commit 9716919

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
503503
<file baseinstalldir="PHP/CodeSniffer" name="InlineControlStructureUnitTest.4.inc" role="test" />
504504
<file baseinstalldir="PHP/CodeSniffer" name="InlineControlStructureUnitTest.5.inc" role="test" />
505505
<file baseinstalldir="PHP/CodeSniffer" name="InlineControlStructureUnitTest.6.inc" role="test" />
506+
<file baseinstalldir="PHP/CodeSniffer" name="InlineControlStructureUnitTest.7.inc" role="test" />
506507
<file baseinstalldir="PHP/CodeSniffer" name="InlineControlStructureUnitTest.js" role="test" />
507508
<file baseinstalldir="PHP/CodeSniffer" name="InlineControlStructureUnitTest.js.fixed" role="test" />
508509
<file baseinstalldir="PHP/CodeSniffer" name="InlineControlStructureUnitTest.php" role="test" />

src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ public function process(File $phpcsFile, $stackPtr)
140140
return;
141141
}
142142

143+
if ($tokens[$nextNonEmpty]['code'] === T_COLON) {
144+
var_dump($tokens[$nextNonEmpty]);
145+
// Alternative control structure.
146+
// T_END... missing. Either live coding, parse error or end
147+
// tag in short open tags and scan run with short_open_tag=Off.
148+
// Bow out completely as any further detection will be unreliable
149+
// and create incorrect fixes or cause fixer conflicts.
150+
return ($phpcsFile->numTokens + 1);
151+
}
152+
143153
unset($nextNonEmpty, $start);
144154

145155
// This is a control structure without an opening brace,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
// Issue #2565.
3+
// Important note: this file intentionally does not have a ".fixed" version as
4+
// when `short_open_tag=Off`, no fixes will be made, while when `short_open_tag=On`
5+
// no fixes are necessary.
6+
?>
7+
8+
<?php if (true) : ?>
9+
<? endif ?>
10+
<?php if (true) { ?>
11+
<? } ?>
12+
13+
<? if (false) : ?>
14+
<? endif ?>
15+
<?php if (false) { ?>
16+
<?php } ?>

0 commit comments

Comments
 (0)