Skip to content

Commit

Permalink
T_OPEN_TAG_WITH_ECHO should not be considered monotonic
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 8, 2024
1 parent 953425a commit d453856
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 34 deletions.
6 changes: 1 addition & 5 deletions src/Fixer/Comment/HeaderCommentFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,7 @@ private function findHeaderCommentCurrentIndex(Tokens $tokens, int $headerNewInd
*/
private function findHeaderCommentInsertionIndex(Tokens $tokens, string $location): int
{
$openTagIndex = $tokens[0]->isGivenKind(T_OPEN_TAG) ? 0 : $tokens->getNextTokenOfKind(0, [[T_OPEN_TAG]]);

if (null === $openTagIndex) {
return 1;
}
$openTagIndex = $tokens[0]->isGivenKind(T_INLINE_HTML) ? 1 : 0;

if ('after_open' === $location) {
return $openTagIndex + 1;
Expand Down
7 changes: 1 addition & 6 deletions src/Fixer/PhpTag/BlankLineAfterOpeningTagFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,13 @@ public function getPriority(): int

public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_OPEN_TAG);
return $tokens->isMonolithicPhp() && $tokens->isTokenKindFound(T_OPEN_TAG);
}

protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
$lineEnding = $this->whitespacesConfig->getLineEnding();

// ignore files with short open tag and ignore non-monolithic files
if (!$tokens[0]->isGivenKind(T_OPEN_TAG) || !$tokens->isMonolithicPhp()) {
return;
}

$newlineFound = false;

/** @var Token $token */
Expand Down
7 changes: 1 addition & 6 deletions src/Fixer/PhpTag/LinebreakAfterOpeningTagFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,11 @@ public function getDefinition(): FixerDefinitionInterface

public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_OPEN_TAG);
return $tokens->isMonolithicPhp() && $tokens->isTokenKindFound(T_OPEN_TAG);
}

protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
// ignore files with short open tag and ignore non-monolithic files
if (!$tokens[0]->isGivenKind(T_OPEN_TAG) || !$tokens->isMonolithicPhp()) {
return;
}

// ignore if linebreak already present
if (str_contains($tokens[0]->getContent(), "\n")) {
return;
Expand Down
6 changes: 1 addition & 5 deletions src/Fixer/PhpTag/NoClosingTagFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,11 @@ public function getDefinition(): FixerDefinitionInterface

public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_CLOSE_TAG);
return \count($tokens) >= 2 && $tokens->isMonolithicPhp() && $tokens->isTokenKindFound(T_CLOSE_TAG);
}

protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
if (\count($tokens) < 2 || !$tokens->isMonolithicPhp() || !$tokens->isTokenKindFound(T_CLOSE_TAG)) {
return;
}

$closeTags = $tokens->findGivenKind(T_CLOSE_TAG);
$index = array_key_first($closeTags);

Expand Down
2 changes: 1 addition & 1 deletion src/Tokenizer/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ public function clearRange(int $indexStart, int $indexEnd): void
*/
public function isMonolithicPhp(): bool
{
if (1 !== ($this->countTokenKind(T_OPEN_TAG) + $this->countTokenKind(T_OPEN_TAG_WITH_ECHO))) {
if (1 !== $this->countTokenKind(T_OPEN_TAG) || 0 !== $this->countTokenKind(T_OPEN_TAG_WITH_ECHO)) {
return false;
}

Expand Down
9 changes: 0 additions & 9 deletions tests/Fixer/PhpTag/NoClosingTagFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,5 @@ public static function provideWithShortOpenTagCases(): iterable
yield [
'<? /**/', '<? /**/?>',
];

yield [
'<?= "somestring"; ?> <?= "anotherstring"; ?>',
];

yield [
'<?= 1;',
'<?= 1; ?>',
];
}
}
4 changes: 2 additions & 2 deletions tests/Tokenizer/TokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ public static function provideMonolithicPhpDetectionCases(): iterable
yield [false, "<?=' '\n?><?\n"];

// short open tag echo
yield [true, "<?=' ';\n"];
yield [false, "<?=' ';\n"];

yield [true, "<?=' '?>"];
yield [false, "<?=' '?>"];

yield [false, " <?=' ';\n"];

Expand Down

0 comments on commit d453856

Please sign in to comment.