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

Fix issue where nowdocs with whitespace before labels are not detected #198

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/Rules.md

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

20 changes: 11 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 All @@ -31,8 +30,16 @@ public function filterTokens(array $tokens): array
if (!$lastString) {
$stack[] = $token;
$lastString = $token;
// `b"` -> `"`
if ($token->id === \T_DOUBLE_QUOTE) {
$token->text = '"';
}
continue;
}
// `b<<< "EOF"` -> `<<<EOF`
if ($lastString->id === \T_START_HEREDOC) {
$lastString->text = '<<<' . trim(ltrim($lastString->text, 'bB'), "< \t\"\n\r") . "\n";
}
array_pop($stack);
$lastString = null;
continue;
Expand Down Expand Up @@ -67,17 +74,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
40 changes: 29 additions & 11 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 @@ -39,6 +38,8 @@ public static function getPriority(string $method): ?int
public static function getTokens(AbstractTokenIndex $idx): array
{
return [
\T_DOUBLE_QUOTE => true,
\T_START_HEREDOC => true,
\T_CONSTANT_ENCAPSED_STRING => true,
\T_ENCAPSED_AND_WHITESPACE => true,
];
Expand All @@ -49,12 +50,15 @@ public static function getTokens(AbstractTokenIndex $idx): array
*/
public static function needsSortedTokens(): bool
{
return false;
return true;
}

/**
* Apply the rule to the given tokens
*
* Double quotes and leading whitespace are removed from heredoc and nowdoc
* labels. Binary prefixes are removed from all strings.
*
* Strings other than nowdocs are normalised as follows:
*
* - Single- and double-quoted strings are replaced with the most readable
Expand All @@ -72,15 +76,30 @@ public function processTokens(array $tokens): void
{
$string = '';
foreach ($tokens as $token) {
// `b"` -> `"`
if ($token->id === \T_DOUBLE_QUOTE) {
if ($token->text !== '"') {
$token->setText('"');
}
continue;
}

// `b<<< "EOF"` -> `<<<EOF`
if ($token->id === \T_START_HEREDOC) {
$text = '<<<' . trim(ltrim($token->text, 'bB'), "< \t\"\n\r") . "\n";
if ($token->text !== $text) {
$token->setText($text);
}
continue;
}

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 Expand Up @@ -132,10 +151,9 @@ public function processTokens(array $tokens): void
break;

case \T_START_HEREDOC:
$closedBy = $openedBy->Data[Data::END_STRING];
$start = trim($openedBy->text);
$start = rtrim($openedBy->text);
$end = trim($start, "<'");
$text = $token->text;
$end = trim($closedBy->text);
if ($next->id === \T_END_HEREDOC) {
$text = substr($text, 0, -1);
$suffix = "\n";
Expand Down
6 changes: 5 additions & 1 deletion src/Rule/StandardSpacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,11 @@ static function () use ($idx, $innerIndent, $next, $last) {
continue;
}

if ($token->id === \T_START_HEREDOC && $this->Formatter->Psr12) {
if (
$token->id === \T_START_HEREDOC
&& $this->Formatter->Psr12
&& $token !== $token->Statement
) {
$token->Whitespace |= Space::NO_BLANK_BEFORE | Space::NO_LINE_BEFORE | Space::SPACE_BEFORE;
}
}
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.

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.

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.

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.

Loading