Skip to content

Commit

Permalink
Fix issue where nowdocs with whitespace before labels are not detected
Browse files Browse the repository at this point in the history
  • Loading branch information
lkrms committed Jan 20, 2025
1 parent 7b1f97a commit 46f7109
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 16 deletions.
12 changes: 3 additions & 9 deletions src/Filter/EvaluateStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Lkrms\PrettyPHP\Contract\Filter;
use Lkrms\PrettyPHP\TokenUtil;
use Salient\Utility\Exception\ShouldNotHappenException;
use Salient\Utility\Regex;

/**
* Evaluate strings for comparison
Expand Down Expand Up @@ -67,17 +66,12 @@ public function filterTokens(array $tokens): array
$text = TokenUtil::unescapeBackticks($token->text);
eval("\$string = \"{$text}\";");
} elseif ($lastString->id === \T_START_HEREDOC) {
$start = trim($lastString->text);
// Remove prefix if present, e.g. `b<<<EOF`
if ($start[0] !== '<') {
/** @var string */
$start = substr($start, 1);
}
$start = rtrim($lastString->text);
// Ignore nowdocs
if (substr($start, 0, 4) === "<<<'") {
if ($start[-1] === "'") {
continue;
}
$end = Regex::replace('/[^a-zA-Z0-9_]+/', '', $start);
$end = trim(ltrim($start, 'bB'), "< \t\"'");
eval("\$string = {$start}\n{$token->text}\n{$end};");
} else {
// @codeCoverageIgnoreStart
Expand Down
11 changes: 4 additions & 7 deletions src/Rule/NormaliseStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Lkrms\PrettyPHP\TokenUtil;
use Salient\Utility\Exception\ShouldNotHappenException;
use Salient\Utility\Regex;
use Salient\Utility\Str;

/**
* Normalise strings
Expand Down Expand Up @@ -75,12 +74,10 @@ public function processTokens(array $tokens): void
if ($token->id === \T_ENCAPSED_AND_WHITESPACE) {
/** @var Token */
$openedBy = $token->String;
if ($openedBy->id === \T_START_HEREDOC && (
Str::startsWith($openedBy->text, "<<<'") || (
$openedBy->text[0] !== '<'
&& Str::startsWith(substr($openedBy->text, 1), "<<<'")
)
)) {
if (
$openedBy->id === \T_START_HEREDOC
&& rtrim($openedBy->text)[-1] === "'"
) {
continue;
}
} else {
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/Formatter/in/issues/0195-nowdoc-with-space.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
$str = sprintf(
<<< 'EOP'
Lorem ipsum... %1$s
EOP,
'You know'
);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 46f7109

Please sign in to comment.