Skip to content

Commit 321d698

Browse files
committed
Improve line detection/reporting
1 parent 48ff5f2 commit 321d698

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

Diff for: Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function process(File $phpcsFile, $stackPtr): void
8383
foreach ($matches as $match) {
8484
if (in_array($match[1], self::HTML_VOID_ELEMENTS)) {
8585
$ptr = $this->findPointer($phpcsFile, $match[0]);
86-
if ($ptr) {
86+
if (!str_contains($match[0], "\n")) {
8787
$fix = $phpcsFile->addFixableWarning(
8888
sprintf(self::WARNING_MESSAGE, $match[0]),
8989
$ptr,
@@ -103,7 +103,7 @@ public function process(File $phpcsFile, $stackPtr): void
103103
} else {
104104
$phpcsFile->addWarning(
105105
sprintf(self::WARNING_MESSAGE, $match[0]),
106-
null,
106+
$ptr,
107107
self::WARNING_CODE
108108
);
109109
}

Diff for: Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function process(File $phpcsFile, $stackPtr)
7272
foreach ($matches as $match) {
7373
if (!in_array($match[1], self::HTML_VOID_ELEMENTS)) {
7474
$ptr = $this->findPointer($phpcsFile, $match[0]);
75-
if ($ptr) {
75+
if (!str_contains($match[0], "\n")) {
7676
$fix = $phpcsFile->addFixableError(
7777
'Avoid using self-closing tag with non-void html element'
7878
. ' - "' . $match[0] . PHP_EOL,
@@ -95,7 +95,7 @@ public function process(File $phpcsFile, $stackPtr)
9595
$phpcsFile->addError(
9696
'Avoid using self-closing tag with non-void html element'
9797
. ' - "' . $match[0] . PHP_EOL,
98-
null,
98+
$ptr,
9999
'HtmlSelfClosingNonVoidTag'
100100
);
101101
}
@@ -113,6 +113,13 @@ public function process(File $phpcsFile, $stackPtr)
113113
*/
114114
protected function findPointer(File $phpcsFile, string $needle): ?int
115115
{
116+
if (str_contains($needle, "\n")) {
117+
foreach (explode("\n", $needle) as $line) {
118+
$result = $this->findPointer($phpcsFile, $line);
119+
}
120+
return $result;
121+
}
122+
116123
foreach ($phpcsFile->getTokens() as $ptr => $token) {
117124
if ($ptr < $this->lastPointer) {
118125
continue;

Diff for: Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public function getErrorList()
2323
public function getWarningList()
2424
{
2525
return [
26-
1 => 2,
2726
10 => 1,
2827
11 => 1,
2928
14 => 1,
@@ -33,6 +32,8 @@ public function getWarningList()
3332
22 => 1,
3433
23 => 1,
3534
24 => 1,
35+
28 => 1,
36+
31 => 1,
3637
32 => 1,
3738
33 => 1,
3839
35 => 1,

Diff for: Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ class HtmlSelfClosingTagsUnitTest extends AbstractSniffUnitTest
1515
public function getErrorList()
1616
{
1717
return [
18-
1 => 2,
1918
40 => 1,
19+
43 => 1,
20+
45 => 1,
2021
46 => 1,
2122
47 => 1,
2223
48 => 1,

0 commit comments

Comments
 (0)