From 89b949672a935da21e66d65bbca9d9c0b0955c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= Date: Mon, 10 Jan 2022 22:20:31 +0100 Subject: [PATCH 1/5] Update Tokens::isMonolithicPhp --- src/Tokenizer/Tokens.php | 10 ++++++---- tests/Tokenizer/TokensTest.php | 5 ++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Tokenizer/Tokens.php b/src/Tokenizer/Tokens.php index 15044fef033..9a8142f326f 100644 --- a/src/Tokenizer/Tokens.php +++ b/src/Tokenizer/Tokens.php @@ -1088,16 +1088,18 @@ 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; } + if (1 === $this->countTokenKind(T_INLINE_HTML)) { + return 1 === Preg::match('/^#!.*$/m', $this[0]->getContent()); + } + return 1 >= ($this->countTokenKind(T_OPEN_TAG) + $this->countTokenKind(T_OPEN_TAG_WITH_ECHO)); } diff --git a/tests/Tokenizer/TokensTest.php b/tests/Tokenizer/TokensTest.php index 458c448ac98..28da0240074 100644 --- a/tests/Tokenizer/TokensTest.php +++ b/tests/Tokenizer/TokensTest.php @@ -353,9 +353,12 @@ public function provideMonolithicPhpDetectionCases(): iterable { yield [true, ""]; + yield [true, "#!/usr/bin/php\n "]; yield [false, " Date: Thu, 13 Jan 2022 22:14:06 +0100 Subject: [PATCH 2/5] Update tests/Tokenizer/TokensTest.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dariusz Rumiński --- tests/Tokenizer/TokensTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Tokenizer/TokensTest.php b/tests/Tokenizer/TokensTest.php index 28da0240074..7a1796489e7 100644 --- a/tests/Tokenizer/TokensTest.php +++ b/tests/Tokenizer/TokensTest.php @@ -362,6 +362,8 @@ public function provideMonolithicPhpDetectionCases(): iterable yield [false, " "]; yield [false, "']; + yield [false, ' World!']; // short open tag yield [(bool) ini_get('short_open_tag'), ""]; From a406b754f428daefa9cbd374632c4079107ac61f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= Date: Thu, 13 Jan 2022 22:21:42 +0100 Subject: [PATCH 3/5] Update multiline handling --- src/Tokenizer/Tokens.php | 2 +- tests/Tokenizer/TokensTest.php | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Tokenizer/Tokens.php b/src/Tokenizer/Tokens.php index 9a8142f326f..473f74b8cff 100644 --- a/src/Tokenizer/Tokens.php +++ b/src/Tokenizer/Tokens.php @@ -1097,7 +1097,7 @@ public function isMonolithicPhp(): bool } if (1 === $this->countTokenKind(T_INLINE_HTML)) { - return 1 === Preg::match('/^#!.*$/m', $this[0]->getContent()); + return 1 === Preg::match('/^#!.*$/', $this[0]->getContent()); } return 1 >= ($this->countTokenKind(T_OPEN_TAG) + $this->countTokenKind(T_OPEN_TAG_WITH_ECHO)); diff --git a/tests/Tokenizer/TokensTest.php b/tests/Tokenizer/TokensTest.php index 7a1796489e7..f8f88fc49a9 100644 --- a/tests/Tokenizer/TokensTest.php +++ b/tests/Tokenizer/TokensTest.php @@ -356,7 +356,9 @@ public function provideMonolithicPhpDetectionCases(): iterable yield [true, "#!/usr/bin/php\n Date: Thu, 13 Jan 2022 22:25:36 +0100 Subject: [PATCH 4/5] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dariusz Rumiński --- src/Tokenizer/Tokens.php | 2 +- tests/Tokenizer/TokensTest.php | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Tokenizer/Tokens.php b/src/Tokenizer/Tokens.php index 473f74b8cff..4c9165ccb4d 100644 --- a/src/Tokenizer/Tokens.php +++ b/src/Tokenizer/Tokens.php @@ -1100,7 +1100,7 @@ public function isMonolithicPhp(): bool return 1 === Preg::match('/^#!.*$/', $this[0]->getContent()); } - return 1 >= ($this->countTokenKind(T_OPEN_TAG) + $this->countTokenKind(T_OPEN_TAG_WITH_ECHO)); + return 1 === ($this->countTokenKind(T_OPEN_TAG) + $this->countTokenKind(T_OPEN_TAG_WITH_ECHO)); } /** diff --git a/tests/Tokenizer/TokensTest.php b/tests/Tokenizer/TokensTest.php index f8f88fc49a9..3b8fa38dea3 100644 --- a/tests/Tokenizer/TokensTest.php +++ b/tests/Tokenizer/TokensTest.php @@ -353,9 +353,14 @@ public function provideMonolithicPhpDetectionCases(): iterable { yield [true, ""]; + yield [false, "#!/usr/bin/bash\ncat Date: Thu, 13 Jan 2022 23:44:30 +0100 Subject: [PATCH 5/5] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dariusz Rumiński --- src/Tokenizer/Tokens.php | 2 +- tests/Tokenizer/TokensTest.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Tokenizer/Tokens.php b/src/Tokenizer/Tokens.php index 4c9165ccb4d..edb321167f5 100644 --- a/src/Tokenizer/Tokens.php +++ b/src/Tokenizer/Tokens.php @@ -1097,7 +1097,7 @@ public function isMonolithicPhp(): bool } if (1 === $this->countTokenKind(T_INLINE_HTML)) { - return 1 === Preg::match('/^#!.*$/', $this[0]->getContent()); + return 1 === Preg::match('/^#!.+$/', $this[0]->getContent()); } return 1 === ($this->countTokenKind(T_OPEN_TAG) + $this->countTokenKind(T_OPEN_TAG_WITH_ECHO)); diff --git a/tests/Tokenizer/TokensTest.php b/tests/Tokenizer/TokensTest.php index 3b8fa38dea3..202a9220b00 100644 --- a/tests/Tokenizer/TokensTest.php +++ b/tests/Tokenizer/TokensTest.php @@ -353,10 +353,12 @@ public function provideMonolithicPhpDetectionCases(): iterable { yield [true, ""]; + yield [false, "#!\n