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

feature: binary_operator_spaces - Revert change about => alignment and use option instead #6724

Merged
merged 1 commit into from
Jan 29, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ List of Available Rules

- | ``default``
| Default fix strategy.
| Allowed values: ``'align'``, ``'align_single_space'``, ``'align_single_space_minimal'``, ``'no_space'``, ``'single_space'``, ``null``
| Allowed values: ``'align'``, ``'align_by_scope'``, ``'align_single_space'``, ``'align_single_space_by_scope'``, ``'align_single_space_minimal'``, ``'align_single_space_minimal_by_scope'``, ``'no_space'``, ``'single_space'``, ``null``
| Default value: ``'single_space'``
- | ``operators``
| Dictionary of `binary operator` => `fix strategy` values that differ from the default strategy. Supported are: `=`, `*`, `/`, `%`, `<`, `>`, `|`, `^`, `+`, `-`, `&`, `&=`, `&&`, `||`, `.=`, `/=`, `=>`, `==`, `>=`, `===`, `!=`, `<>`, `!==`, `<=`, `and`, `or`, `xor`, `-=`, `%=`, `*=`, `|=`, `+=`, `<<`, `<<=`, `>>`, `>>=`, `^=`, `**`, `**=`, `<=>`, `??`, `??=`
Expand Down
68 changes: 66 additions & 2 deletions doc/rules/operator/binary_operator_spaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Configuration

Default fix strategy.

Allowed values: ``'align'``, ``'align_single_space'``, ``'align_single_space_minimal'``, ``'no_space'``, ``'single_space'``, ``null``
Allowed values: ``'align'``, ``'align_by_scope'``, ``'align_single_space'``, ``'align_single_space_by_scope'``, ``'align_single_space_minimal'``, ``'align_single_space_minimal_by_scope'``, ``'no_space'``, ``'single_space'``, ``null``

Default value: ``'single_space'``

Expand Down Expand Up @@ -145,12 +145,14 @@ With configuration: ``['operators' => ['=>' => 'align']]``.
- "foo" => 12,
+ "foo" => 12,
"baaaaaaaaaaar" => 13,

"baz" => 1,
];

Example #8
~~~~~~~~~~

With configuration: ``['operators' => ['=>' => 'align_single_space']]``.
With configuration: ``['operators' => ['=>' => 'align_by_scope']]``.

.. code-block:: diff

Expand All @@ -161,11 +163,51 @@ With configuration: ``['operators' => ['=>' => 'align_single_space']]``.
- "foo" => 12,
+ "foo" => 12,
"baaaaaaaaaaar" => 13,

- "baz" => 1,
+ "baz" => 1,
];

Example #9
~~~~~~~~~~

With configuration: ``['operators' => ['=>' => 'align_single_space']]``.

.. code-block:: diff

--- Original
+++ New
<?php
$array = [
- "foo" => 12,
+ "foo" => 12,
"baaaaaaaaaaar" => 13,

"baz" => 1,
];

Example #10
~~~~~~~~~~~

With configuration: ``['operators' => ['=>' => 'align_single_space_by_scope']]``.

.. code-block:: diff

--- Original
+++ New
<?php
$array = [
- "foo" => 12,
+ "foo" => 12,
"baaaaaaaaaaar" => 13,

- "baz" => 1,
+ "baz" => 1,
];

Example #11
~~~~~~~~~~~

With configuration: ``['operators' => ['=>' => 'align_single_space_minimal']]``.

.. code-block:: diff
Expand All @@ -178,6 +220,28 @@ With configuration: ``['operators' => ['=>' => 'align_single_space_minimal']]``.
- "baaaaaaaaaaar" => 13,
+ "foo" => 12,
+ "baaaaaaaaaaar" => 13,

"baz" => 1,
];

Example #12
~~~~~~~~~~~

With configuration: ``['operators' => ['=>' => 'align_single_space_minimal_by_scope']]``.

.. code-block:: diff

--- Original
+++ New
<?php
$array = [
- "foo" => 12,
- "baaaaaaaaaaar" => 13,
+ "foo" => 12,
+ "baaaaaaaaaaar" => 13,

- "baz" => 1,
+ "baz" => 1,
];

Rule sets
Expand Down
109 changes: 102 additions & 7 deletions src/Fixer/Operator/BinaryOperatorSpacesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -221,6 +239,8 @@ public function getDefinition(): FixerDefinitionInterface
$array = [
"foo" => 12,
"baaaaaaaaaaar" => 13,

"baz" => 1,
];
',
['operators' => ['=>' => 'align']]
Expand All @@ -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']]
Expand All @@ -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']]
),
]
);
}
Expand Down Expand Up @@ -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, ' ']);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Copy link
Contributor Author

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.

++$this->deepestLevel;
++$this->currentLevel;
}
$tokenContent = sprintf(self::ALIGN_PLACEHOLDER, $this->currentLevel).$token->getContent();

$nextToken = $tokens[$index + 1];
Expand All @@ -667,6 +748,7 @@ private function injectAlignmentPlaceholdersForArrow(Tokens $tokens, int $startA

$tokens[$index] = new Token([T_DOUBLE_ARROW, $tokenContent]);
$newLineFoundSinceLastPlaceholder = false;
$yieldFoundSinceLastPlaceholder = false;

continue;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The specific behavior won't be hardcoded for => but instead will be an option BY_SCOPE for alignment.

self::ALIGN_BY_SCOPE !== $alignStrategy
&& self::ALIGN_SINGLE_SPACE_BY_SCOPE !== $alignStrategy
&& self::ALIGN_SINGLE_SPACE_MINIMAL_BY_SCOPE !== $alignStrategy
) {
++$groupIndex;
$groups[$groupIndex] = [];
}
Expand All @@ -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).' ';
}
Expand Down