Skip to content

Commit 2367a62

Browse files
authored
Merge pull request #554 from PHPCSStandards/feature/552-generic-uselessoverridingmethod-bugfix-close-tag
Generic/UselessOverridingMethod: improve handling of PHP open/close tags between statements
2 parents eb3454f + 7642636 commit 2367a62

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,20 @@ public function process(File $phpcsFile, $stackPtr)
153153
}//end for
154154

155155
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
156-
if ($tokens[$next]['code'] !== T_SEMICOLON) {
156+
if ($tokens[$next]['code'] !== T_SEMICOLON && $tokens[$next]['code'] !== T_CLOSE_TAG) {
157157
return;
158158
}
159159

160+
// This list deliberately does not include the `T_OPEN_TAG_WITH_ECHO` as that token implicitly is an echo statement, i.e. content.
161+
$nonContent = Tokens::$emptyTokens;
162+
$nonContent[T_OPEN_TAG] = T_OPEN_TAG;
163+
$nonContent[T_CLOSE_TAG] = T_CLOSE_TAG;
164+
160165
// Check rest of the scope.
161166
for (++$next; $next <= $end; ++$next) {
162167
$code = $tokens[$next]['code'];
163168
// Skip for any other content.
164-
if (isset(Tokens::$emptyTokens[$code]) === false) {
169+
if (isset($nonContent[$code]) === false) {
165170
return;
166171
}
167172
}

src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.1.inc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,26 @@ function foo() {
148148
}
149149
};
150150
}
151+
152+
class SniffShouldHandlePHPOpenCloseTagsCorrectly {
153+
public function thisIsStillAUselessOverride($a, $b) {
154+
return parent::thisIsStillAUselessOverride($a, $b) ?><?php
155+
// Even with a comment here.
156+
}
157+
158+
public function butNotWithANewLineBetweenThePHPTagsAsThenWeEchoOutTheNewLine($a, $b) {
159+
parent::butNotWithANewLineBetweenThePHPTagsAsThenWeEchoOutTheNewLine($a, $b) ?>
160+
<?php
161+
}
162+
163+
public function embeddedHTMLAfterCallingParent() {
164+
parent::embeddedHTMLAfterCallingParent() ?>
165+
<div>HTML</div>
166+
<?php
167+
}
168+
169+
public function contentAfterUselessEmbedBlock() {
170+
parent::contentAfterUselessEmbedBlock() ?><?php
171+
return 1;
172+
}
173+
}

src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function getWarningList($testFile='')
6060
116 => 1,
6161
134 => 1,
6262
146 => 1,
63+
153 => 1,
6364
];
6465
default:
6566
return [];

0 commit comments

Comments
 (0)