-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feature: binary_operator_spaces - Revert change about => alignment and use option instead #6724
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,16 +49,31 @@ final class BinaryOperatorSpacesFixer extends AbstractFixer implements Configura | |
*/ | ||
public const ALIGN = 'align'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public const ALIGN_BY_SCOPE = 'align_by_scope'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public const ALIGN_SINGLE_SPACE = 'align_single_space'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public const ALIGN_SINGLE_SPACE_BY_SCOPE = 'align_single_space_by_scope'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public const ALIGN_SINGLE_SPACE_MINIMAL = 'align_single_space_minimal'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public const ALIGN_SINGLE_SPACE_MINIMAL_BY_SCOPE = 'align_single_space_minimal_by_scope'; | ||
|
||
/** | ||
* @internal | ||
* | ||
|
@@ -133,8 +148,11 @@ final class BinaryOperatorSpacesFixer extends AbstractFixer implements Configura | |
*/ | ||
private static array $allowedValues = [ | ||
self::ALIGN, | ||
self::ALIGN_BY_SCOPE, | ||
self::ALIGN_SINGLE_SPACE, | ||
self::ALIGN_SINGLE_SPACE_MINIMAL, | ||
self::ALIGN_SINGLE_SPACE_BY_SCOPE, | ||
self::ALIGN_SINGLE_SPACE_MINIMAL_BY_SCOPE, | ||
self::SINGLE_SPACE, | ||
self::NO_SPACE, | ||
null, | ||
|
@@ -221,6 +239,8 @@ public function getDefinition(): FixerDefinitionInterface | |
$array = [ | ||
"foo" => 12, | ||
"baaaaaaaaaaar" => 13, | ||
|
||
"baz" => 1, | ||
]; | ||
', | ||
['operators' => ['=>' => 'align']] | ||
|
@@ -230,6 +250,19 @@ public function getDefinition(): FixerDefinitionInterface | |
$array = [ | ||
"foo" => 12, | ||
"baaaaaaaaaaar" => 13, | ||
|
||
"baz" => 1, | ||
]; | ||
', | ||
['operators' => ['=>' => 'align_by_scope']] | ||
), | ||
new CodeSample( | ||
'<?php | ||
$array = [ | ||
"foo" => 12, | ||
"baaaaaaaaaaar" => 13, | ||
|
||
"baz" => 1, | ||
]; | ||
', | ||
['operators' => ['=>' => 'align_single_space']] | ||
|
@@ -239,10 +272,34 @@ public function getDefinition(): FixerDefinitionInterface | |
$array = [ | ||
"foo" => 12, | ||
"baaaaaaaaaaar" => 13, | ||
|
||
"baz" => 1, | ||
]; | ||
', | ||
['operators' => ['=>' => 'align_single_space_by_scope']] | ||
), | ||
new CodeSample( | ||
'<?php | ||
$array = [ | ||
"foo" => 12, | ||
"baaaaaaaaaaar" => 13, | ||
|
||
"baz" => 1, | ||
]; | ||
', | ||
['operators' => ['=>' => 'align_single_space_minimal']] | ||
), | ||
new CodeSample( | ||
'<?php | ||
$array = [ | ||
"foo" => 12, | ||
"baaaaaaaaaaar" => 13, | ||
|
||
"baz" => 1, | ||
]; | ||
', | ||
['operators' => ['=>' => 'align_single_space_minimal_by_scope']] | ||
), | ||
] | ||
); | ||
} | ||
|
@@ -364,13 +421,19 @@ private function fixWhiteSpaceAroundOperator(Tokens $tokens, int $index): void | |
// schedule for alignment | ||
$this->alignOperatorTokens[$tokenContent] = $this->operators[$tokenContent]; | ||
|
||
if (self::ALIGN === $this->operators[$tokenContent]) { | ||
if ( | ||
self::ALIGN === $this->operators[$tokenContent] | ||
|| self::ALIGN_BY_SCOPE === $this->operators[$tokenContent] | ||
) { | ||
return; | ||
} | ||
|
||
// fix white space after operator | ||
if ($tokens[$index + 1]->isWhitespace()) { | ||
if (self::ALIGN_SINGLE_SPACE_MINIMAL === $this->operators[$tokenContent]) { | ||
if ( | ||
self::ALIGN_SINGLE_SPACE_MINIMAL === $this->operators[$tokenContent] | ||
|| self::ALIGN_SINGLE_SPACE_MINIMAL_BY_SCOPE === $this->operators[$tokenContent] | ||
) { | ||
$tokens[$index + 1] = new Token([T_WHITESPACE, ' ']); | ||
} | ||
|
||
|
@@ -491,7 +554,12 @@ private function fixAlignment(Tokens $tokens, array $toAlign): void | |
} | ||
|
||
// for all tokens that should be aligned but do not have anything to align with, fix spacing if needed | ||
if (self::ALIGN_SINGLE_SPACE === $alignStrategy || self::ALIGN_SINGLE_SPACE_MINIMAL === $alignStrategy) { | ||
if ( | ||
self::ALIGN_SINGLE_SPACE === $alignStrategy | ||
|| self::ALIGN_SINGLE_SPACE_MINIMAL === $alignStrategy | ||
|| self::ALIGN_SINGLE_SPACE_BY_SCOPE === $alignStrategy | ||
|| self::ALIGN_SINGLE_SPACE_MINIMAL_BY_SCOPE === $alignStrategy | ||
) { | ||
if ('=>' === $tokenContent) { | ||
for ($index = $tokens->count() - 2; $index > 0; --$index) { | ||
if ($tokens[$index]->isGivenKind(T_DOUBLE_ARROW)) { // always binary operator, never part of declare statement | ||
|
@@ -615,16 +683,23 @@ private function injectAlignmentPlaceholders(Tokens $tokens, int $from, int $unt | |
private function injectAlignmentPlaceholdersForArrow(Tokens $tokens, int $startAt, int $endAt): void | ||
{ | ||
$newLineFoundSinceLastPlaceholder = true; | ||
$yieldFoundSinceLastPlaceholder = false; | ||
|
||
for ($index = $startAt; $index < $endAt; ++$index) { | ||
/** @var Token $token */ | ||
$token = $tokens[$index]; | ||
$content = $token->getContent(); | ||
|
||
if (str_contains($content, "\n")) { | ||
$newLineFoundSinceLastPlaceholder = true; | ||
} | ||
|
||
if ($token->isGivenKind(T_YIELD)) { | ||
$yieldFoundSinceLastPlaceholder = true; | ||
} | ||
|
||
if ($token->isGivenKind(T_FN)) { | ||
$yieldFoundSinceLastPlaceholder = false; | ||
$from = $tokens->getNextMeaningfulToken($index); | ||
$until = $this->getLastTokenIndexOfFn($tokens, $index); | ||
$this->injectArrayAlignmentPlaceholders($tokens, $from + 1, $until - 1); | ||
|
@@ -634,6 +709,7 @@ private function injectAlignmentPlaceholdersForArrow(Tokens $tokens, int $startA | |
} | ||
|
||
if ($token->isGivenKind(T_ARRAY)) { // don't use "$tokens->isArray()" here, short arrays are handled in the next case | ||
$yieldFoundSinceLastPlaceholder = false; | ||
$from = $tokens->getNextMeaningfulToken($index); | ||
$until = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $from); | ||
$index = $until; | ||
|
@@ -644,6 +720,7 @@ private function injectAlignmentPlaceholdersForArrow(Tokens $tokens, int $startA | |
} | ||
|
||
if ($token->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) { | ||
$yieldFoundSinceLastPlaceholder = false; | ||
$from = $index; | ||
$until = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $from); | ||
$index = $until; | ||
|
@@ -656,6 +733,10 @@ private function injectAlignmentPlaceholdersForArrow(Tokens $tokens, int $startA | |
// no need to analyze for `isBinaryOperator` (always true), nor if part of declare statement (not valid PHP) | ||
// there is also no need to analyse the second arrow of a line | ||
if ($token->isGivenKind(T_DOUBLE_ARROW) && $newLineFoundSinceLastPlaceholder) { | ||
if ($yieldFoundSinceLastPlaceholder) { | ||
++$this->deepestLevel; | ||
++$this->currentLevel; | ||
} | ||
$tokenContent = sprintf(self::ALIGN_PLACEHOLDER, $this->currentLevel).$token->getContent(); | ||
|
||
$nextToken = $tokens[$index + 1]; | ||
|
@@ -667,6 +748,7 @@ private function injectAlignmentPlaceholdersForArrow(Tokens $tokens, int $startA | |
|
||
$tokens[$index] = new Token([T_DOUBLE_ARROW, $tokenContent]); | ||
$newLineFoundSinceLastPlaceholder = false; | ||
$yieldFoundSinceLastPlaceholder = false; | ||
|
||
continue; | ||
} | ||
|
@@ -742,7 +824,10 @@ private function fixWhiteSpaceBeforeOperator(Tokens $tokens, int $index, string | |
return; | ||
} | ||
|
||
if (self::ALIGN_SINGLE_SPACE_MINIMAL !== $alignStrategy || $tokens[$tokens->getPrevNonWhitespace($index - 1)]->isComment()) { | ||
if ( | ||
self::ALIGN_SINGLE_SPACE_MINIMAL !== $alignStrategy && self::ALIGN_SINGLE_SPACE_MINIMAL_BY_SCOPE !== $alignStrategy | ||
|| $tokens[$tokens->getPrevNonWhitespace($index - 1)]->isComment() | ||
) { | ||
return; | ||
} | ||
|
||
|
@@ -774,7 +859,11 @@ private function replacePlaceholders(Tokens $tokens, string $alignStrategy, stri | |
foreach ($lines as $index => $line) { | ||
if (substr_count($line, $placeholder) > 0) { | ||
$groups[$groupIndex][] = $index; | ||
} elseif ('=>' !== $tokenContent) { | ||
} elseif ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The specific behavior won't be hardcoded for |
||
self::ALIGN_BY_SCOPE !== $alignStrategy | ||
&& self::ALIGN_SINGLE_SPACE_BY_SCOPE !== $alignStrategy | ||
&& self::ALIGN_SINGLE_SPACE_MINIMAL_BY_SCOPE !== $alignStrategy | ||
) { | ||
++$groupIndex; | ||
$groups[$groupIndex] = []; | ||
} | ||
|
@@ -791,11 +880,17 @@ private function replacePlaceholders(Tokens $tokens, string $alignStrategy, stri | |
$currentPosition = strpos($lines[$index], $placeholder); | ||
$before = substr($lines[$index], 0, $currentPosition); | ||
|
||
if (self::ALIGN_SINGLE_SPACE === $alignStrategy) { | ||
if ( | ||
self::ALIGN_SINGLE_SPACE === $alignStrategy | ||
|| self::ALIGN_SINGLE_SPACE_BY_SCOPE === $alignStrategy | ||
) { | ||
if (!str_ends_with($before, ' ')) { // if last char of before-content is not ' '; add it | ||
$before .= ' '; | ||
} | ||
} elseif (self::ALIGN_SINGLE_SPACE_MINIMAL === $alignStrategy) { | ||
} elseif ( | ||
self::ALIGN_SINGLE_SPACE_MINIMAL === $alignStrategy | ||
|| self::ALIGN_SINGLE_SPACE_MINIMAL_BY_SCOPE === $alignStrategy | ||
) { | ||
if (1 !== Preg::match('/^\h+$/', $before)) { // if indent; do not move, leave to other fixer | ||
$before = rtrim($before).' '; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To handle #6716 (comment)
When a yield was previously use, it's another scope/level.