From 321d698c9c8bca598eec6feedc9d9bfa66f2f865 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Fri, 26 May 2023 11:59:18 +0100 Subject: [PATCH] Improve line detection/reporting --- Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php | 4 ++-- Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php | 11 +++++++++-- Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php | 3 ++- Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php | 3 ++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php b/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php index 81d048f3..3610cb39 100644 --- a/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php +++ b/Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php @@ -83,7 +83,7 @@ public function process(File $phpcsFile, $stackPtr): void foreach ($matches as $match) { if (in_array($match[1], self::HTML_VOID_ELEMENTS)) { $ptr = $this->findPointer($phpcsFile, $match[0]); - if ($ptr) { + if (!str_contains($match[0], "\n")) { $fix = $phpcsFile->addFixableWarning( sprintf(self::WARNING_MESSAGE, $match[0]), $ptr, @@ -103,7 +103,7 @@ public function process(File $phpcsFile, $stackPtr): void } else { $phpcsFile->addWarning( sprintf(self::WARNING_MESSAGE, $match[0]), - null, + $ptr, self::WARNING_CODE ); } diff --git a/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php b/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php index f6b5dff8..c026e67f 100644 --- a/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php +++ b/Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php @@ -72,7 +72,7 @@ public function process(File $phpcsFile, $stackPtr) foreach ($matches as $match) { if (!in_array($match[1], self::HTML_VOID_ELEMENTS)) { $ptr = $this->findPointer($phpcsFile, $match[0]); - if ($ptr) { + if (!str_contains($match[0], "\n")) { $fix = $phpcsFile->addFixableError( 'Avoid using self-closing tag with non-void html element' . ' - "' . $match[0] . PHP_EOL, @@ -95,7 +95,7 @@ public function process(File $phpcsFile, $stackPtr) $phpcsFile->addError( 'Avoid using self-closing tag with non-void html element' . ' - "' . $match[0] . PHP_EOL, - null, + $ptr, 'HtmlSelfClosingNonVoidTag' ); } @@ -113,6 +113,13 @@ public function process(File $phpcsFile, $stackPtr) */ protected function findPointer(File $phpcsFile, string $needle): ?int { + if (str_contains($needle, "\n")) { + foreach (explode("\n", $needle) as $line) { + $result = $this->findPointer($phpcsFile, $line); + } + return $result; + } + foreach ($phpcsFile->getTokens() as $ptr => $token) { if ($ptr < $this->lastPointer) { continue; diff --git a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php index 2cf45e46..1e72c4b9 100644 --- a/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php +++ b/Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php @@ -23,7 +23,6 @@ public function getErrorList() public function getWarningList() { return [ - 1 => 2, 10 => 1, 11 => 1, 14 => 1, @@ -33,6 +32,8 @@ public function getWarningList() 22 => 1, 23 => 1, 24 => 1, + 28 => 1, + 31 => 1, 32 => 1, 33 => 1, 35 => 1, diff --git a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php index f54850b1..869498d9 100644 --- a/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php +++ b/Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php @@ -15,8 +15,9 @@ class HtmlSelfClosingTagsUnitTest extends AbstractSniffUnitTest public function getErrorList() { return [ - 1 => 2, 40 => 1, + 43 => 1, + 45 => 1, 46 => 1, 47 => 1, 48 => 1,