Skip to content

Commit

Permalink
qa: Remove redundant code in WhiteSpace\ScopeIndentSniff as upstream …
Browse files Browse the repository at this point in the history
…is fixed (#207)
  • Loading branch information
michalbundyra authored Oct 16, 2024
1 parent 4197fd6 commit 6f6a1a9
Showing 1 changed file with 3 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ public function process(File $phpcsFile, $stackPtr)
if ($tokens[$prev]['line'] === $tokens[$i]['line']
&& ! ($fp = $this->findPrevious($phpcsFile, $i, [T_OBJECT_OPERATOR]))
) {
$endOfStatement = min($this->findEndOfStatement($phpcsFile, $i), $this->findNext($phpcsFile, $i));
$endOfStatement = min($phpcsFile->findEndOfStatement($i), $this->findNext($phpcsFile, $i));
$newLine = $this->hasContainNewLine($phpcsFile, $i, $endOfStatement);

if ($newLine) {
Expand Down Expand Up @@ -618,7 +618,7 @@ public function process(File $phpcsFile, $stackPtr)
],
true
)) {
$endOfStatement = $this->findEndOfStatement($phpcsFile, $i);
$endOfStatement = $phpcsFile->findEndOfStatement($i);
$newLine = $this->hasContainNewLine($phpcsFile, $i, $endOfStatement);

if ($newLine) {
Expand All @@ -640,7 +640,7 @@ public function process(File $phpcsFile, $stackPtr)
&& isset($tokens[$next]['scope_closer'])
&& $tokens[$next]['scope_closer'] === $next
) {
$endOfStatement = $this->findEndOfStatement($phpcsFile, $next);
$endOfStatement = $phpcsFile->findEndOfStatement($next);
$this->extras($endOfStatement, $this->indent);

$extraIndent += $this->indent;
Expand Down Expand Up @@ -946,70 +946,6 @@ private function findNext(File $phpcsFile, int $ptr, array $search = []) : ?int
return null;
}

/**
* Overrides File::findEndOfStatement as temporary solution until
* https://github.com/squizlabs/PHP_CodeSniffer/issues/2748
* is fixed.
*/
private function findEndOfStatement(File $phpcsFile, int $ptr) : int
{
$closingBracket = [
T_CLOSE_PARENTHESIS,
T_CLOSE_SQUARE_BRACKET,
T_CLOSE_CURLY_BRACKET,
T_CLOSE_SHORT_ARRAY,
];

$tokens = $phpcsFile->getTokens();
$lastToken = $phpcsFile->numTokens;

if ($tokens[$ptr]['code'] === T_DOUBLE_ARROW && $ptr < $lastToken) {
++$ptr;
}

while ($ptr < $lastToken) {
if ($tokens[$ptr]['code'] === T_OPEN_PARENTHESIS) {
$ptr = $tokens[$ptr]['parenthesis_closer'] + 1;
continue;
}

if ($tokens[$ptr]['code'] === T_OPEN_CURLY_BRACKET
|| $tokens[$ptr]['code'] === T_OPEN_SQUARE_BRACKET
|| $tokens[$ptr]['code'] === T_OPEN_SHORT_ARRAY
) {
$ptr = $tokens[$ptr]['bracket_closer'] + 1;
continue;
}

if (isset($tokens[$ptr]['scope_closer']) && $ptr < $tokens[$ptr]['scope_closer']) {
$ptr = $tokens[$ptr]['scope_closer'];
if (in_array($tokens[$ptr]['code'], $closingBracket, true)) {
++$ptr;
}
} elseif (isset($tokens[$ptr]['parenthesis_closer']) && $ptr < $tokens[$ptr]['parenthesis_closer']) {
$ptr = $tokens[$ptr]['parenthesis_closer'];
if (in_array($tokens[$ptr]['code'], $closingBracket, true)) {
++$ptr;
}
}

if ($tokens[$ptr]['code'] === T_COMMA
|| $tokens[$ptr]['code'] === T_SEMICOLON
|| $tokens[$ptr]['code'] === T_DOUBLE_ARROW
) {
return $ptr;
}

if (in_array($tokens[$ptr]['code'], $closingBracket, true)) {
return $phpcsFile->findPrevious(Tokens::$emptyTokens, $ptr - 1, null, true);
}

++$ptr;
}

return $lastToken;
}

/**
* Checks if there is another object operator
* before $ptr token.
Expand Down

0 comments on commit 6f6a1a9

Please sign in to comment.