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 bracket placement issues #186

Merged
merged 5 commits into from
Jan 17, 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
11 changes: 8 additions & 3 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_order' => ['order' => [
'todo',
'method',
'property',
'property-read',
'property-write',
'method',
'api',
'internal',
'requires',
'dataProvider',
'backupGlobals',
'template',
'template-covariant',
'extends',
'implements',
'use',
Expand All @@ -83,18 +86,20 @@
'readonly',
'var',
'param',
'param-out',
'return',
'throws',
]],
// 'phpdoc_param_order' => true,
'phpdoc_separation' => ['groups' => [
['see', 'link'],
['property', 'phpstan-property', 'property-read', 'phpstan-property-read'],
['property', 'property-read', 'property-write', 'phpstan-property', 'phpstan-property-read', 'phpstan-property-write'],
['method', 'phpstan-method'],
['requires', 'dataProvider', 'backupGlobals'],
['template', 'template-covariant'],
['extends', 'implements', 'use'],
['phpstan-require-extends', 'phpstan-require-implements'],
['readonly', 'var', 'phpstan-var', 'param', 'param-out', 'phpstan-param', 'return', 'phpstan-return', 'throws', 'phpstan-assert*', 'phpstan-ignore*', 'disregard'],
['readonly', 'var', 'param', 'param-out', 'return', 'throws', 'phpstan-var', 'phpstan-param', 'phpstan-return', 'phpstan-assert*', 'phpstan-ignore*', 'disregard'],
['phpstan-*'],
]],
'phpdoc_tag_casing' => true,
Expand Down
132 changes: 66 additions & 66 deletions composer.lock

Large diffs are not rendered by default.

203 changes: 107 additions & 96 deletions docs/Rules.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion scripts/get-rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

/**
* @param array<int,array<array{rule:class-string<Rule>,is_mandatory:bool,is_default:bool,pass:int,method:string,priority:int,php_doc:PHPDoc|null,tokens:array<int,bool>|array{string}|null,declarations:array<int,bool>|array{'*'}|null}>> $array
* @param-out array<int,array<array{rule:class-string<Rule>,is_mandatory:bool,is_default:bool,pass:int,method:string,priority:int,php_doc:PHPDoc|null,tokens:array<int,bool>|array{string}|null,declarations:array<int,bool>|array{'*'}|null}>> $array
* @param class-string<Rule> $rule
* @param array<class-string<Rule>,array<string|null>>|null $callbackDocs
* @param-out array<int,array<array{rule:class-string<Rule>,is_mandatory:bool,is_default:bool,pass:int,method:string,priority:int,php_doc:PHPDoc|null,tokens:array<int,bool>|array{string}|null,declarations:array<int,bool>|array{'*'}|null}>> $array
*/
function maybeAddRule(
array &$array,
Expand Down
4 changes: 2 additions & 2 deletions src/App/PrettyPHPCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -1394,8 +1394,8 @@ private function getConfigFile(string $dir): ?string

/**
* @param array<string,string> $files
* @param-out array<string,string> $files
* @param array<string,string> $dirs
* @param-out array<string,string> $files
* @param-out array<string,string> $dirs
*/
private function addDir(string $dir, array &$files, array &$dirs): void
Expand Down Expand Up @@ -1434,8 +1434,8 @@ private function addDir(string $dir, array &$files, array &$dirs): void
/**
* @param SplFileInfo|string $file
* @param array<string,string> $files
* @param-out array<string,string> $files
* @param array<string,string> $dirs
* @param-out array<string,string> $files
* @param-out array<string,string> $dirs
*/
private function addFile($file, array &$files, array &$dirs): void
Expand Down
24 changes: 15 additions & 9 deletions src/Catalog/TokenData.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,50 +53,56 @@ interface TokenData
*/
public const PROPERTY_HOOKS = 7;

/**
* Collections of tokens associated with a T_FOR loop: expr1, expr2, expr3,
* semicolon delimiters, comma delimiters
*/
public const FOR_PARTS = 8;

/**
* The last token of the string opened by the token
*/
public const END_STRING = 8;
public const END_STRING = 9;

/**
* The first object operator in a chain of method calls
*/
public const CHAIN = 9;
public const CHAIN = 10;

/**
* The other T_QUESTION or T_COLON associated with a TERNARY
*/
public const OTHER_TERNARY = 10;
public const OTHER_TERNARY = 11;

/**
* The token ID of the delimiter associated with a LIST_PARENT token
*/
public const LIST_DELIMITER = 11;
public const LIST_DELIMITER = 12;

/**
* A collection of items associated with a LIST_PARENT token
*/
public const LIST_ITEMS = 12;
public const LIST_ITEMS = 13;

/**
* The number of items associated with a LIST_PARENT token
*/
public const LIST_ITEM_COUNT = 13;
public const LIST_ITEM_COUNT = 14;

/**
* The LIST_PARENT of the first token in a LIST_ITEM
*/
public const LIST_PARENT = 14;
public const LIST_PARENT = 15;

/**
* The content of a normalised DocBlock token (T_DOC_COMMENT or T_COMMENT)
* after delimiters and trailing whitespace are removed
*/
public const COMMENT_CONTENT = 15;
public const COMMENT_CONTENT = 16;

/**
* An array of closures that align other tokens with the token when its
* output column changes
*/
public const ALIGNMENT_CALLBACKS = 16;
public const ALIGNMENT_CALLBACKS = 17;
}
2 changes: 1 addition & 1 deletion src/Concern/RuleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private function preserveOneLine(
}

$start->collect($end)
->applyInnerWhitespace(Space::CRITICAL_NO_BLANK | Space::CRITICAL_NO_LINE);
->setInnerWhitespace(Space::CRITICAL_NO_BLANK | Space::CRITICAL_NO_LINE);

return true;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use Lkrms\PrettyPHP\Rule\NormaliseStrings;
use Lkrms\PrettyPHP\Rule\OperatorSpacing;
use Lkrms\PrettyPHP\Rule\PlaceBraces;
use Lkrms\PrettyPHP\Rule\PlaceBrackets;
use Lkrms\PrettyPHP\Rule\PlaceComments;
use Lkrms\PrettyPHP\Rule\PreserveNewlines;
use Lkrms\PrettyPHP\Rule\PreserveOneLineStatements;
Expand Down Expand Up @@ -152,6 +153,7 @@ final class Formatter implements Buildable, Immutable
PlaceComments::class,
PreserveNewlines::class,
VerticalSpacing::class,
PlaceBrackets::class,
DeclarationSpacing::class,
StandardIndentation::class,
SwitchIndentation::class,
Expand All @@ -168,9 +170,9 @@ final class Formatter implements Buildable, Immutable
PreserveNewlines::class,
PreserveOneLineStatements::class,
BlankBeforeReturn::class,
StrictLists::class,
StrictExpressions::class,
SemiStrictExpressions::class,
StrictLists::class,
AlignChains::class,
DeclarationSpacing::class,
AlignArrowFunctions::class,
Expand Down
19 changes: 13 additions & 6 deletions src/Internal/TokenCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,18 @@ public function toString(string $delimiter = ''): string
/**
* @return $this
*/
public function applyWhitespace(int $whitespace)
public function setTokenWhitespace(int $whitespace): self
{
foreach ($this->Items as $token) {
$token->Whitespace |= $whitespace;
}
return $this;
}

/**
* @return $this
*/
public function applyTokenWhitespace(int $whitespace): self
{
// Shift *_BEFORE and *_AFTER to their NO_* counterparts, then clear
// other bits
Expand All @@ -342,7 +353,7 @@ public function applyWhitespace(int $whitespace)
/**
* @return $this
*/
public function applyInnerWhitespace(int $whitespace)
public function setInnerWhitespace(int $whitespace): self
{
$this->assertCollected();

Expand All @@ -354,7 +365,6 @@ public function applyInnerWhitespace(int $whitespace)
if ($this->count() < 2) {
return $this;
}
$remove = $whitespace << 6 & 0b111111000000;
$ignore = true;
foreach ($this->Items as $token) {
if ($ignore) {
Expand All @@ -364,9 +374,6 @@ public function applyInnerWhitespace(int $whitespace)
|| $token->Data[Data::BOUND_TO]->index > $token->index
) {
$token->Whitespace |= $whitespace;
if ($remove) {
$token->doRemoveWhitespace($remove);
}
}
}
return $this;
Expand Down
44 changes: 39 additions & 5 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ private function linkTokens(array $tokens): void
* - `Data[Data::END_STRING]`
*
* @param non-empty-list<Token> $tokens
* @param-out non-empty-list<Token> $tokens
* @param array<int,array<int,Token>>|null $tokensById
* @param-out array<int,array<int,Token>> $tokensById
* @param Token[]|null $scopes
* @param-out non-empty-list<Token> $tokens
* @param-out array<int,array<int,Token>> $tokensById
* @param-out non-empty-array<Token> $scopes
*/
private function buildHierarchy(
Expand Down Expand Up @@ -719,20 +719,21 @@ private function parseStatements(array $scopes, ?array &$statements): void
}

/**
* Pass 4: identify declarations and parse (some) expressions
* Pass 4: parse expressions
*
* Token properties set:
*
* - `Data[Data::DECLARATION_PARTS]`
* - `Data[Data::DECLARATION_TYPE]`
* - `Data[Data::PROPERTY_HOOKS]`
* - `Data[Data::FOR_PARTS]`
* - `Data[Data::OTHER_TERNARY]`
* - `Data[Data::CHAIN]`
*
* @param Token[] $statements
* @param array<int,Token>|null $declarations
* @param-out array<int,Token> $declarations
* @param array<int,array<int,Token>>|null $declarationsByType
* @param-out array<int,Token> $declarations
* @param-out array<int,array<int,Token>> $declarationsByType
*/
private function parseExpressions(
Expand All @@ -750,7 +751,7 @@ private function parseExpressions(
$end = $statement->EndStatement;
$end = $end->OpenBracket ?? $end;

// Detect non-anonymous declarations
// Parse non-anonymous declarations and `for` expressions
if (
$idx->AttributeOrDeclaration[$statement->id]
&& ($first = $this->skipNextSiblingsFrom($statement, $idx->Attribute))
Expand Down Expand Up @@ -835,6 +836,39 @@ private function parseExpressions(
}
}
}
} elseif ($statement->id === \T_FOR) {
/** @var Token */
$open = $statement->NextCode;
/** @var Token */
$close = $open->CloseBracket;
/** @var Token */
$first = $open->Next;
/** @var Token */
$last = $close->Prev;

$children = $open->children();
$semicolons = $children->getAnyOf(\T_SEMICOLON);
$commas = $children->getAnyOf(\T_COMMA);
/** @var Token */
$semi1 = $semicolons->first();
/** @var Token */
$second = $semi1->Next;
/** @var Token */
$semi2 = $semicolons->last();
/** @var Token */
$third = $semi2->Next;

$expr1 = $first->collect($semi1);
$expr2 = $second->collect($semi2);
$expr3 = $third->collect($last);

$statement->Data[Data::FOR_PARTS] = [
$expr1,
$expr2,
$expr3,
$semicolons,
$commas,
];
}

$token = $statement;
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/BlankBeforeReturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class BlankBeforeReturn implements TokenRule
public static function getPriority(string $method): ?int
{
return [
self::PROCESS_TOKENS => 220,
self::PROCESS_TOKENS => 204,
][$method] ?? null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rule/DeclarationSpacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function processDeclarations(array $declarations): void
&& !$prev->isMultiLine()
&& !$decl->isMultiLine()
)) {
$prev->End->collect($token)->applyInnerWhitespace(Space::NO_BLANK);
$prev->End->collect($token)->setInnerWhitespace(Space::NO_BLANK);
$noBlankApplied = true;
} elseif (!$decl->isCollapsible()) {
// Apply "loose" spacing to multi-line declarations
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/OperatorSpacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function processTokens(array $tokens): void
$token->Whitespace |= Space::NONE_BEFORE | Space::NONE_AFTER;
if (!$inTypeContext) {
/** @var Token $parent */
$parent->outer()->applyInnerWhitespace(Space::NONE);
$parent->outer()->setInnerWhitespace(Space::NONE);
if (
$parent->PrevCode
&& $parent->PrevCode->id !== \T_OR
Expand Down
Loading