|
@@ -53,7 +53,7 @@ final class DeclareStrictTypesFixer extends AbstractFixer implements Whitespaces
|
|
|
|
|
|
public function isCandidate(Tokens $tokens): bool
|
|
|
{
|
|
|
- return isset($tokens[0]) && $tokens[0]->isGivenKind(T_OPEN_TAG);
|
|
|
+ return $tokens->isMonolithicPhp() && !$tokens->isTokenKindFound(T_OPEN_TAG_WITH_ECHO);
|
|
|
}
|
|
|
|
|
|
public function isRisky(): bool
|
|
@@ -63,17 +63,11 @@ final class DeclareStrictTypesFixer extends AbstractFixer implements Whitespaces
|
|
|
|
|
|
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
|
|
|
{
|
|
|
- // check if the declaration is already done
|
|
|
- $searchIndex = $tokens->getNextMeaningfulToken(0);
|
|
|
- if (null === $searchIndex) {
|
|
|
- $this->insertSequence($tokens); // declaration not found, insert one
|
|
|
+ $openTagIndex = $tokens[0]->isGivenKind(T_INLINE_HTML) ? 1 : 0;
|
|
|
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- $sequenceLocation = $tokens->findSequence([[T_DECLARE, 'declare'], '(', [T_STRING, 'strict_types'], '=', [T_LNUMBER], ')'], $searchIndex, null, false);
|
|
|
+ $sequenceLocation = $tokens->findSequence([[T_DECLARE, 'declare'], '(', [T_STRING, 'strict_types'], '=', [T_LNUMBER], ')'], $openTagIndex, null, false);
|
|
|
if (null === $sequenceLocation) {
|
|
|
- $this->insertSequence($tokens); // declaration not found, insert one
|
|
|
+ $this->insertSequence($openTagIndex, $tokens); // declaration not found, insert one
|
|
|
|
|
|
return;
|
|
|
}
|
|
@@ -102,7 +96,7 @@ final class DeclareStrictTypesFixer extends AbstractFixer implements Whitespaces
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private function insertSequence(Tokens $tokens): void
|
|
|
+ private function insertSequence(int $openTagIndex, Tokens $tokens): void
|
|
|
{
|
|
|
$sequence = [
|
|
|
new Token([T_DECLARE, 'declare']),
|
|
@@ -113,28 +107,26 @@ final class DeclareStrictTypesFixer extends AbstractFixer implements Whitespaces
|
|
|
new Token(')'),
|
|
|
new Token(';'),
|
|
|
];
|
|
|
- $endIndex = \count($sequence);
|
|
|
+ $nextIndex = $openTagIndex + \count($sequence) + 1;
|
|
|
|
|
|
- $tokens->insertAt(1, $sequence);
|
|
|
+ $tokens->insertAt($openTagIndex + 1, $sequence);
|
|
|
|
|
|
- // start index of the sequence is always 1 here, 0 is always open tag
|
|
|
- // transform "<?php\n" to "<?php " if needed
|
|
|
- if (str_contains($tokens[0]->getContent(), "\n")) {
|
|
|
- $tokens[0] = new Token([$tokens[0]->getId(), trim($tokens[0]->getContent()).' ']);
|
|
|
+ // transform "<?php" or "<?php\n" to "<?php " if needed
|
|
|
+ $content = $tokens[$openTagIndex]->getContent();
|
|
|
+ if (!str_contains($content, ' ') || str_contains($content, "\n")) {
|
|
|
+ $tokens[$openTagIndex] = new Token([$tokens[$openTagIndex]->getId(), trim($tokens[$openTagIndex]->getContent()).' ']);
|
|
|
}
|
|
|
|
|
|
- if ($endIndex === \count($tokens) - 1) {
|
|
|
+ if (\count($tokens) === $nextIndex) {
|
|
|
return; // no more tokens after sequence, single_blank_line_at_eof might add a line
|
|
|
}
|
|
|
|
|
|
$lineEnding = $this->whitespacesConfig->getLineEnding();
|
|
|
- if (!$tokens[1 + $endIndex]->isWhitespace()) {
|
|
|
- $tokens->insertAt(1 + $endIndex, new Token([T_WHITESPACE, $lineEnding]));
|
|
|
-
|
|
|
- return;
|
|
|
+ if ($tokens[$nextIndex]->isWhitespace()) {
|
|
|
+ $content = $tokens[$nextIndex]->getContent();
|
|
|
+ $tokens[$nextIndex] = new Token([T_WHITESPACE, $lineEnding.ltrim($content, " \t")]);
|
|
|
+ } else {
|
|
|
+ $tokens->insertAt($nextIndex, new Token([T_WHITESPACE, $lineEnding]));
|
|
|
}
|
|
|
-
|
|
|
- $content = $tokens[1 + $endIndex]->getContent();
|
|
|
- $tokens[1 + $endIndex] = new Token([T_WHITESPACE, $lineEnding.ltrim($content, " \t")]);
|
|
|
}
|
|
|
}
|