Skip to content

Commit

Permalink
Improve line detection/reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed May 26, 2023
1 parent 48ff5f2 commit 321d698
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
);
}
Expand Down
11 changes: 9 additions & 2 deletions Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'
);
}
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion Magento2/Tests/Html/HtmlClosingVoidTagsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function getErrorList()
public function getWarningList()
{
return [
1 => 2,
10 => 1,
11 => 1,
14 => 1,
Expand All @@ -33,6 +32,8 @@ public function getWarningList()
22 => 1,
23 => 1,
24 => 1,
28 => 1,
31 => 1,
32 => 1,
33 => 1,
35 => 1,
Expand Down
3 changes: 2 additions & 1 deletion Magento2/Tests/Html/HtmlSelfClosingTagsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 321d698

Please sign in to comment.