Skip to content

Commit 872a9c4

Browse files
committed
Generic/DisallowMultipleStatements: add fixed file + minor bug fix
The sniff contain a fixer, but didn't have a `fixed` file. The fixer did not yet take the new PHPCS annotations into account. The only one which is problematic if it's moved, is the `T_PHPCS_IGNORE` token, so if that token would be encountered, the error will still be thrown, but will no longer be fixable. Includes additional unit test covering this.
1 parent b53f64e commit 872a9c4

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
589589
</dir>
590590
<dir name="Formatting">
591591
<file baseinstalldir="PHP/CodeSniffer" name="DisallowMultipleStatementsUnitTest.inc" role="test" />
592+
<file baseinstalldir="PHP/CodeSniffer" name="DisallowMultipleStatementsUnitTest.inc.fixed" role="test" />
592593
<file baseinstalldir="PHP/CodeSniffer" name="DisallowMultipleStatementsUnitTest.php" role="test" />
593594
<file baseinstalldir="PHP/CodeSniffer" name="MultipleStatementAlignmentUnitTest.inc" role="test" />
594595
<file baseinstalldir="PHP/CodeSniffer" name="MultipleStatementAlignmentUnitTest.inc.fixed" role="test" />

src/Standards/Generic/Sniffs/Formatting/DisallowMultipleStatementsSniff.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,24 @@ public function register()
3939
*/
4040
public function process(File $phpcsFile, $stackPtr)
4141
{
42-
$tokens = $phpcsFile->getTokens();
42+
$tokens = $phpcsFile->getTokens();
43+
$fixable = true;
44+
$prev = $stackPtr;
45+
46+
do {
47+
$prev = $phpcsFile->findPrevious([T_SEMICOLON, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, T_PHPCS_IGNORE], ($prev - 1));
48+
if ($prev === false
49+
|| $tokens[$prev]['code'] === T_OPEN_TAG
50+
|| $tokens[$prev]['code'] === T_OPEN_TAG_WITH_ECHO
51+
) {
52+
$phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'no');
53+
return;
54+
}
4355

44-
$prev = $phpcsFile->findPrevious([T_SEMICOLON, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO], ($stackPtr - 1));
45-
if ($prev === false
46-
|| $tokens[$prev]['code'] === T_OPEN_TAG
47-
|| $tokens[$prev]['code'] === T_OPEN_TAG_WITH_ECHO
48-
) {
49-
$phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'no');
50-
return;
51-
}
56+
if ($tokens[$prev]['code'] === T_PHPCS_IGNORE) {
57+
$fixable = false;
58+
}
59+
} while ($tokens[$prev]['code'] === T_PHPCS_IGNORE);
5260

5361
// Ignore multiple statements in a FOR condition.
5462
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
@@ -69,7 +77,13 @@ public function process(File $phpcsFile, $stackPtr)
6977
$phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'yes');
7078

7179
$error = 'Each PHP statement must be on a line by itself';
72-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SameLine');
80+
$code = 'SameLine';
81+
if ($fixable === false) {
82+
$phpcsFile->addError($error, $stackPtr, $code);
83+
return;
84+
}
85+
86+
$fix = $phpcsFile->addFixableError($error, $stackPtr, $code);
7387
if ($fix === true) {
7488
$phpcsFile->fixer->beginChangeset();
7589
$phpcsFile->fixer->addNewline($prev);
@@ -81,7 +95,7 @@ public function process(File $phpcsFile, $stackPtr)
8195
}
8296
} else {
8397
$phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'no');
84-
}
98+
}//end if
8599

86100
}//end process()
87101

src/Standards/Generic/Tests/Formatting/DisallowMultipleStatementsUnitTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ $this->wizardid = 10; $this->paint(); echo 'x';
1111
<div class="<?php echo $class ?>" id="<?php echo $id ?>"></div>
1212
<div class="<?= $class; ?>" id="<?= $id; ?>"></div>
1313
<div class="<?= $class ?>" id="<?= $id ?>"></div>
14+
15+
<?php
16+
echo 'x'; /* phpcs:ignore Standard */ echo $y; /* phpcs:ignore OtherStandard */;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
$y = 2;
3+
;
4+
echo $y;
5+
for ($i = 1; $i < $length; $i++) {}
6+
for (; $i < $length; $i++) {}
7+
echo 'x';
8+
echo $y;
9+
$x = 10;
10+
echo $y;
11+
$this->wizardid = 10;
12+
$this->paint();
13+
echo 'x';
14+
?>
15+
<div class="<?php echo $class; ?>" id="<?php echo $id; ?>"></div>
16+
<div class="<?php echo $class ?>" id="<?php echo $id ?>"></div>
17+
<div class="<?= $class; ?>" id="<?= $id; ?>"></div>
18+
<div class="<?= $class ?>" id="<?= $id ?>"></div>
19+
20+
<?php
21+
echo 'x'; /* phpcs:ignore Standard */ echo $y; /* phpcs:ignore OtherStandard */;

src/Standards/Generic/Tests/Formatting/DisallowMultipleStatementsUnitTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ class DisallowMultipleStatementsUnitTest extends AbstractSniffUnitTest
2626
public function getErrorList()
2727
{
2828
return [
29-
2 => 1,
30-
6 => 1,
31-
7 => 1,
32-
8 => 2,
29+
2 => 1,
30+
6 => 1,
31+
7 => 1,
32+
8 => 2,
33+
16 => 2,
3334
];
3435

3536
}//end getErrorList()

0 commit comments

Comments
 (0)