Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Tokens::isMonolithicPhp #6233

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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());
kubawerlos marked this conversation as resolved.
Show resolved Hide resolved
}

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

/**
Expand Down
14 changes: 13 additions & 1 deletion tests/Tokenizer/TokensTest.php
Expand Up @@ -353,12 +353,24 @@ public function provideMonolithicPhpDetectionCases(): iterable
{
yield [true, "<?php\n"];
yield [true, "<?php\n?>"];
kubawerlos marked this conversation as resolved.
Show resolved Hide resolved
kubawerlos marked this conversation as resolved.
Show resolved Hide resolved
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"];
kubawerlos marked this conversation as resolved.
Show resolved Hide resolved
yield [true, "#!/usr/bin/php7.4-cli\n<?php\n"];
kubawerlos marked this conversation as resolved.
Show resolved Hide resolved
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"];
kubawerlos marked this conversation as resolved.
Show resolved Hide resolved
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"];
kubawerlos marked this conversation as resolved.
Show resolved Hide resolved
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