Skip to content

Commit

Permalink
Use built-in feature to skip remaining tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed Jan 2, 2024
1 parent bee870f commit 2b33847
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 40 deletions.
18 changes: 8 additions & 10 deletions Magento2/Sniffs/Html/HtmlClosingVoidTagsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,16 @@ public function register(): array
*
* @param File $phpcsFile
* @param int $stackPtr
* @return void
*
* @return int
*/
public function process(File $phpcsFile, $stackPtr): void
public function process(File $phpcsFile, $stackPtr): int
{
static $lastFile;
if ($lastFile === $phpcsFile->getFilename()) {
return;
}
$lastFile = $phpcsFile->getFilename();

$html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()) - $stackPtr);
$tokenCount = count($phpcsFile->getTokens());
$html = $phpcsFile->getTokensAsString($stackPtr, $tokenCount - $stackPtr);

if (empty($html)) {
return;
return $tokenCount + 1;
}

if (preg_match_all('$<(\w{2,})\s?[^<]*\/>$', $html, $matches, PREG_SET_ORDER)) {
Expand All @@ -95,5 +91,7 @@ public function process(File $phpcsFile, $stackPtr): void
}
}
}

return $tokenCount + 1;
}
}
18 changes: 8 additions & 10 deletions Magento2/Sniffs/Html/HtmlCollapsibleAttributeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@ public function register()
*
* @param File $phpcsFile
* @param int $stackPtr
* @return int|void
*
* @return int
*/
public function process(File $phpcsFile, $stackPtr)
public function process(File $phpcsFile, $stackPtr): int
{
static $lastFile;
if ($lastFile === $phpcsFile->getFilename()) {
return;
}
$lastFile = $phpcsFile->getFilename();

$html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()) - $stackPtr);
$tokenCount = count($phpcsFile->getTokens());
$html = $phpcsFile->getTokensAsString($stackPtr, $tokenCount - $stackPtr);

if (empty($html)) {
return;
return $tokenCount + 1;
}

$pattern = '$<\w+.*?\s*(?=.*?\s*data-toggle="collapse")[^>]*?>.*?$';
Expand All @@ -57,5 +53,7 @@ public function process(File $phpcsFile, $stackPtr)
);
}
}

return $tokenCount + 1;
}
}
18 changes: 8 additions & 10 deletions Magento2/Sniffs/Html/HtmlDirectiveSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,19 @@ public function register()
*
* @param File $phpcsFile
* @param int $stackPtr
* @return int|void
*
* @return int
*/
public function process(File $phpcsFile, $stackPtr)
public function process(File $phpcsFile, $stackPtr): int
{
$this->usedVariables = [];
$this->unfilteredVariables = [];

static $lastFile;
if ($lastFile === $phpcsFile->getFilename()) {
return;
}
$lastFile = $phpcsFile->getFilename();

$html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()) - $stackPtr);
$tokenCount = count($phpcsFile->getTokens());
$html = $phpcsFile->getTokensAsString($stackPtr, $tokenCount - $stackPtr);

if (empty($html)) {
return;
return $tokenCount + 1;
}

$html = $this->processIfDirectives($html, $phpcsFile);
Expand All @@ -69,6 +65,8 @@ public function process(File $phpcsFile, $stackPtr)
$html = $this->processVarDirectivesAndParams($html, $phpcsFile);

$this->validateDefinedVariables($phpcsFile, $html);

return $tokenCount + 1;
}

/**
Expand Down
18 changes: 8 additions & 10 deletions Magento2/Sniffs/Html/HtmlSelfClosingTagsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,16 @@ public function register()
*
* @param File $phpcsFile
* @param int $stackPtr
* @return int|void
*
* @return int
*/
public function process(File $phpcsFile, $stackPtr)
public function process(File $phpcsFile, $stackPtr): int
{
static $lastFile;
if ($lastFile === $phpcsFile->getFilename()) {
return;
}
$lastFile = $phpcsFile->getFilename();

$html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()) - $stackPtr);
$tokenCount = count($phpcsFile->getTokens());
$html = $phpcsFile->getTokensAsString($stackPtr, $tokenCount - $stackPtr);

if (empty($html)) {
return;
return $tokenCount + 1;
}

if (preg_match_all('$<(\w{2,})\s?[^<]*\/>$', $html, $matches, PREG_SET_ORDER)) {
Expand All @@ -83,5 +79,7 @@ public function process(File $phpcsFile, $stackPtr)
}
}
}

return $tokenCount + 1;
}
}

0 comments on commit 2b33847

Please sign in to comment.