Skip to content

Commit

Permalink
Merge branch 'review'
Browse files Browse the repository at this point in the history
  • Loading branch information
lkrms committed Dec 26, 2024
2 parents 1c32355 + 57ed4de commit 49542d2
Show file tree
Hide file tree
Showing 16 changed files with 803 additions and 461 deletions.
148 changes: 88 additions & 60 deletions docs/Rules.md

Large diffs are not rendered by default.

104 changes: 95 additions & 9 deletions src/Concern/FilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,33 @@ private function getPrevSibling(int $i, int $offset = 1, ?int &$key = null): ?Ge
return null;
}

/**
* Get the given token's next sibling that is in an index
*
* @param array<int,bool> $index
* @param-out int $key
*/
private function getNextSiblingFrom(int $i, array $index, int $to, ?int &$key = null): ?GenericToken
{
while ($token = $this->getNextSibling($i, $to + 1, 1, $j)) {
if ($index[$token->id]) {
$key = $j;
return $token;
}
$i = $j;
}
$key = -1;
return null;
}

/**
* Get the given token's next sibling with the given token ID
*
* @param-out int $key
*/
private function getNextSiblingOf(int $i, int $count, int $id, ?int &$key = null): ?GenericToken
private function getNextSiblingOf(int $i, int $id, int $to, ?int &$key = null): ?GenericToken
{
while ($token = $this->getNextSibling($i, $count, 1, $j)) {
while ($token = $this->getNextSibling($i, $to + 1, 1, $j)) {
if ($token->id === $id) {
$key = $j;
return $token;
Expand Down Expand Up @@ -201,15 +220,82 @@ private function isColonAltSyntaxDelimiter(int $i): bool
private function isColonSwitchCaseDelimiter(int $i, ?int $parentIndex = null): bool
{
$parent = $parentIndex === null
// @codeCoverageIgnoreStart
? $this->getParent($i, $parentIndex)
// @codeCoverageIgnoreEnd
: $this->Tokens[$parentIndex];
return $parent
&& ($parentPrev = $this->getPrevSibling($parentIndex, 2))
&& $parentPrev->id === \T_SWITCH
&& ($prev = $this->getPrevSiblingFrom($i, $this->Idx->SwitchCaseOrDelimiter, $parentIndex + 1))
&& ($prev->id === \T_CASE || $prev->id === \T_DEFAULT);

if (
!$parent
|| !($parentPrev = $this->getPrevSibling($parentIndex, 2))
|| $parentPrev->id !== \T_SWITCH
) {
return false;
}

$t = $this->getPrevSiblingFrom($i, $this->Idx->CaseOrDefault, $parentIndex + 1, $j);
if (!$t) {
return false;
}

$ternaryCount = 0;
do {
$t = $this->getNextSiblingFrom($j, $this->Idx->SwitchCaseDelimiterOrTernary, $i, $j);
if (!$t) {
return false;
} elseif ($t->id === \T_QUESTION) {
/** @var GenericToken */
$prev = $this->getPrevCode($j, $prevIndex);
if (
$prev->id !== \T_COLON
|| !$this->isColonReturnTypeDelimiter($prevIndex)
) {
$ternaryCount++;
}
continue;
} elseif ($t->id === \T_COLON) {
if (
$this->isColonReturnTypeDelimiter($j)
|| $ternaryCount--
) {
continue;
}
}
break;
} while (true);

return $t === $this->Tokens[$i];
}

/**
* Check if the given T_COLON is a return type delimiter
*/
private function isColonReturnTypeDelimiter(int $i, ?int $prevCodeIndex = null): bool
{
/** @var GenericToken */
$prevCode = $prevCodeIndex === null
? $this->getPrevCode($i, $prevCodeIndex)
: $this->Tokens[$prevCodeIndex];

if ($prevCode->id === \T_CLOSE_PARENTHESIS) {
$prev = $this->getPrevSibling($prevCodeIndex, 1, $prevIndex);
if (
$prev
&& $prev->id === \T_USE
&& ($prevCode2 = $this->getPrevCode($prevIndex, $prevCode2Index))
&& $prevCode2->id === \T_CLOSE_PARENTHESIS
) {
$prev = $this->getPrevSibling($prevCode2Index, 1, $prevIndex);
}

while ($prev && $this->Idx->FunctionIdentifier[$prev->id]) {
$prev = $this->getPrevSibling($prevIndex, 1, $prevIndex);
}

if ($prev && $this->Idx->FunctionOrFn[$prev->id]) {
return true;
}
}

return false;
}

/**
Expand Down
39 changes: 12 additions & 27 deletions src/Filter/MoveComments.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function filterTokens(array $tokens): array
if ($this->NeedsFnDoubleArrow) {
foreach ($tokens as $i => $token) {
if ($token->id === \T_FN) {
if (!$this->getNextSiblingOf($i, $this->Count, \T_DOUBLE_ARROW, $j)) {
if (!$this->getNextSiblingOf($i, \T_DOUBLE_ARROW, $this->Count - 1, $j)) {
// @codeCoverageIgnoreStart
throw new ShouldNotHappenException('Invalid arrow function');
// @codeCoverageIgnoreEnd
Expand Down Expand Up @@ -206,21 +206,17 @@ private function checkToken(int $i, bool $isLast = false): bool
$token = $this->Tokens[$i];

if ($token->id === \T_COLON) {
// The following code replicates most of `Token::getColonType()`,
// which can't be used in this context
// The following code replicates `Token::getColonSubId()`, which
// can't be used in this context
if ($this->isColonAltSyntaxDelimiter($i)) {
// Allow comments AFTER alternative syntax delimiters
return $isLast;
}

$parent = $this->getParent($i, $parentIndex);
if ($parent && $this->isColonSwitchCaseDelimiter($i, $parentIndex)) {
// Allow comments AFTER switch case delimiters
return $isLast;
}

/** @var GenericToken */
$prevCode = $this->getPrevCode($i, $prevCodeIndex);
$parent = $this->getParent($i, $parentIndex);

if (
$parent
&& $parent->id === \T_OPEN_PARENTHESIS
Expand All @@ -241,25 +237,14 @@ private function checkToken(int $i, bool $isLast = false): bool
return $isLast;
}

if ($prevCode->id === \T_CLOSE_PARENTHESIS) {
$prev = $this->getPrevSibling($prevCodeIndex, 1, $prevIndex);
if (
$prev
&& $prev->id === \T_USE
&& ($prevCode2 = $this->getPrevCode($prevIndex, $prevCode2Index))
&& $prevCode2->id === \T_CLOSE_PARENTHESIS
) {
$prev = $this->getPrevSibling($prevCode2Index, 1, $prevIndex);
}

while ($prev && $this->Idx->FunctionIdentifier[$prev->id]) {
$prev = $this->getPrevSibling($prevIndex, 1, $prevIndex);
}
if ($this->isColonReturnTypeDelimiter($i, $prevCodeIndex)) {
// Allow comments AFTER return type delimiters
return $isLast;
}

if ($prev && $this->Idx->FunctionOrFn[$prev->id]) {
// Allow comments AFTER return type delimiters
return $isLast;
}
if ($parent && $this->isColonSwitchCaseDelimiter($i, $parentIndex)) {
// Allow comments AFTER switch case delimiters
return $isLast;
}

while ($prevCode->id === \T_STRING && (
Expand Down
9 changes: 5 additions & 4 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
use Lkrms\PrettyPHP\Rule\BlankBeforeReturn;
use Lkrms\PrettyPHP\Rule\ControlStructureSpacing;
use Lkrms\PrettyPHP\Rule\DeclarationSpacing;
use Lkrms\PrettyPHP\Rule\EssentialWhitespace;
use Lkrms\PrettyPHP\Rule\EssentialSpacing;
use Lkrms\PrettyPHP\Rule\HangingIndentation;
use Lkrms\PrettyPHP\Rule\HeredocIndentation;
use Lkrms\PrettyPHP\Rule\IndexSpacing;
Expand All @@ -70,7 +70,7 @@
use Lkrms\PrettyPHP\Rule\StrictExpressions;
use Lkrms\PrettyPHP\Rule\StrictLists;
use Lkrms\PrettyPHP\Rule\SwitchIndentation;
use Lkrms\PrettyPHP\Rule\VerticalWhitespace;
use Lkrms\PrettyPHP\Rule\VerticalSpacing;
use Salient\Contract\Core\Buildable;
use Salient\Contract\Core\Immutable;
use Salient\Core\Concern\HasBuilder;
Expand Down Expand Up @@ -258,14 +258,14 @@ final class Formatter implements Buildable, Immutable
PlaceComments::class,
PlaceBraces::class,
PreserveNewlines::class,
VerticalWhitespace::class,
VerticalSpacing::class,
ListSpacing::class,
StandardIndentation::class,
SwitchIndentation::class,
DeclarationSpacing::class,
HangingIndentation::class,
HeredocIndentation::class,
EssentialWhitespace::class,
EssentialSpacing::class,
];

/**
Expand Down Expand Up @@ -552,6 +552,7 @@ private function apply(): self
$this->PreserveEol = false;
$this->HeredocIndent = HeredocIndent::HANGING;
$this->OneTrueBraceStyle = false;
$this->ExpandHeaders = true;
$this->NewlineBeforeFnDoubleArrow = true;

$enable = array_merge(
Expand Down
16 changes: 3 additions & 13 deletions src/Rule/BlankBeforeReturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,10 @@ public function processTokens(array $tokens): void
continue;
}

// Ignore empty statements
$prev = $token;
while (
$prev->PrevCode
&& $prev->PrevCode->id === \T_SEMICOLON
&& $prev->PrevCode->Statement === $prev->PrevCode
) {
$prev = $prev->PrevCode;
}

if (
$prev->PrevSibling
&& $prev->PrevSibling->Statement
&& $this->Idx->Return[$prev->PrevSibling->Statement->id]
($prev = $token->skipPrevEmptyStatements()->PrevSibling)
&& $prev->Statement
&& $this->Idx->Return[$prev->Statement->id]
) {
continue;
}
Expand Down
Loading

0 comments on commit 49542d2

Please sign in to comment.