Skip to content

Commit

Permalink
Update Tokens::isMonolithicPhp
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos authored and keradus committed Jan 14, 2022
1 parent 6fe4c69 commit 738972d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/Tokenizer/Tokens.php
Expand Up @@ -1088,17 +1088,19 @@ public function clearRange(int $indexStart, int $indexEnd): void
*/
public function isMonolithicPhp(): bool
{
$size = $this->count();

if (0 === $size) {
if (0 === $this->count()) {
return false;
}

if ($this->isTokenKindFound(T_INLINE_HTML)) {
if ($this->countTokenKind(T_INLINE_HTML) > 1) {
return false;
}

return 1 >= ($this->countTokenKind(T_OPEN_TAG) + $this->countTokenKind(T_OPEN_TAG_WITH_ECHO));
if (1 === $this->countTokenKind(T_INLINE_HTML)) {
return 1 === Preg::match('/^#!.+$/', $this[0]->getContent());
}

return 1 === ($this->countTokenKind(T_OPEN_TAG) + $this->countTokenKind(T_OPEN_TAG_WITH_ECHO));
}

/**
Expand Down
16 changes: 15 additions & 1 deletion tests/Tokenizer/TokensTest.php
Expand Up @@ -353,12 +353,26 @@ public function provideMonolithicPhpDetectionCases(): iterable
{
yield [true, "<?php\n"];
yield [true, "<?php\n?>"];
yield [false, "#!\n<?php\n"];
yield [false, "#!/usr/bin/bash\ncat <?php\n"];
yield [false, "#!/usr/bin/env bash\ncat <?php\n"];
yield [true, "#!/usr/bin/php\n<?php\n"];
yield [true, "#!/usr/bin/php7.4-cli\n<?php\n"];
yield [false, "#!/usr/bin/php\n\n<?php\n"]; // empty line after shebang would be printed to console before PHP executes
yield [true, "#!/usr/bin/php8\n<?php\n"];
yield [true, "#!/usr/bin/env php\n<?php\n"];
yield [true, "#!/usr/bin/env php7.4\n<?php\n"];
yield [true, "#!/usr/bin/env php7.4-cli\n<?php\n"];
yield [false, "#!/usr/bin/env this-is\ntoo-much\n<?php\n"];
yield [false, "#!/usr/bin/php\nFoo bar<?php\n"];
yield [false, "#!/usr/bin/env php -n \nFoo bar\n<?php\n"];
yield [false, ''];
yield [false, ' '];
yield [false, "#!/usr/bin/env php\n<?php\n"];
yield [false, " <?php\n"];
yield [false, "<?php\n?> "];
yield [false, "<?php\n?><?php\n"];
yield [false, 'Hello<?php echo "World!"; ?>'];
yield [false, '<?php echo "Hello"; ?> World!'];
// short open tag
yield [(bool) ini_get('short_open_tag'), "<?\n"];
yield [(bool) ini_get('short_open_tag'), "<?\n?>"];
Expand Down

0 comments on commit 738972d

Please sign in to comment.