Skip to content

Commit

Permalink
feature: NoUselessConcatOperatorFixer - Introduction (PHP-CS-Fixer#6447)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum authored Sep 2, 2022
1 parent 8ce0207 commit aafad72
Show file tree
Hide file tree
Showing 26 changed files with 760 additions and 22 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'header_comment' => ['header' => $header],
'heredoc_indentation' => false, // TODO switch on when # of PR's is lower
'modernize_strpos' => true, // needs PHP 8+ or polyfill
'no_useless_concat_operator' => false, // TODO switch back on when the `src/Console/Application.php` no longer needs the concat
'use_arrow_functions' => false, // TODO switch on when # of PR's is lower
])
->setFinder($finder)
Expand Down
15 changes: 15 additions & 0 deletions doc/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,21 @@ List of Available Rules
Part of rule sets `@PhpCsFixer <./ruleSets/PhpCsFixer.rst>`_ `@Symfony <./ruleSets/Symfony.rst>`_

`Source PhpCsFixer\\Fixer\\Import\\NoUnusedImportsFixer <./../src/Fixer/Import/NoUnusedImportsFixer.php>`_
- `no_useless_concat_operator <./rules/operator/no_useless_concat_operator.rst>`_

There should not be useless concat operations.

Configuration options:

- | ``juggle_simple_strings``
| Allow for simple string quote juggling if it results in more concat-operations merges.
| Allowed types: ``bool``
| Default value: ``false``

Part of rule sets `@PhpCsFixer <./ruleSets/PhpCsFixer.rst>`_ `@Symfony <./ruleSets/Symfony.rst>`_

`Source PhpCsFixer\\Fixer\\Operator\\NoUselessConcatOperatorFixer <./../src/Fixer/Operator/NoUselessConcatOperatorFixer.php>`_
- `no_useless_else <./rules/control_structure/no_useless_else.rst>`_

There should not be useless ``else`` cases.
Expand Down
1 change: 1 addition & 0 deletions doc/ruleSets/Symfony.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Rules
- `no_unneeded_import_alias <./../rules/import/no_unneeded_import_alias.rst>`_
- `no_unset_cast <./../rules/cast_notation/no_unset_cast.rst>`_
- `no_unused_imports <./../rules/import/no_unused_imports.rst>`_
- `no_useless_concat_operator <./../rules/operator/no_useless_concat_operator.rst>`_
- `no_useless_nullsafe_operator <./../rules/operator/no_useless_nullsafe_operator.rst>`_
- `no_whitespace_before_comma_in_array <./../rules/array_notation/no_whitespace_before_comma_in_array.rst>`_
- `normalize_index_brace <./../rules/array_notation/normalize_index_brace.rst>`_
Expand Down
3 changes: 3 additions & 0 deletions doc/rules/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ Operator
- `no_space_around_double_colon <./operator/no_space_around_double_colon.rst>`_

There must be no space around double colons (also called Scope Resolution Operator or Paamayim Nekudotayim).
- `no_useless_concat_operator <./operator/no_useless_concat_operator.rst>`_

There should not be useless concat operations.
- `no_useless_nullsafe_operator <./operator/no_useless_nullsafe_operator.rst>`_

There should not be useless ``null-safe-operators`` ``?->`` used.
Expand Down
58 changes: 58 additions & 0 deletions doc/rules/operator/no_useless_concat_operator.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
===================================
Rule ``no_useless_concat_operator``
===================================

There should not be useless concat operations.

Configuration
-------------

``juggle_simple_strings``
~~~~~~~~~~~~~~~~~~~~~~~~~

Allow for simple string quote juggling if it results in more concat-operations
merges.

Allowed types: ``bool``

Default value: ``false``

Examples
--------

Example #1
~~~~~~~~~~

*Default* configuration.

.. code-block:: diff
--- Original
+++ New
<?php
-$a = 'a'.'b';
+$a = 'ab';
Example #2
~~~~~~~~~~

With configuration: ``['juggle_simple_strings' => true]``.

.. code-block:: diff
--- Original
+++ New
<?php
-$a = 'a'."b";
+$a = "ab";
Rule sets
---------

The rule is part of the following rule sets:

@PhpCsFixer
Using the `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ rule set will enable the ``no_useless_concat_operator`` rule with the default config.

@Symfony
Using the `@Symfony <./../../ruleSets/Symfony.rst>`_ rule set will enable the ``no_useless_concat_operator`` rule with the default config.
2 changes: 1 addition & 1 deletion src/Fixer/Alias/BacktickToShellExecFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getDefinition(): FixerDefinitionInterface
*/
public function getPriority(): int
{
return 2;
return 17;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Fixer/Alias/EregToPregFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public function getDefinition(): FixerDefinitionInterface
);
}

/**
* {@inheritdoc}
*
* Must run after NoUselessConcatOperatorFixer.
*/
public function getPriority(): int
{
return 0;
}

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Alias/SetTypeToCastFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getDefinition(): FixerDefinitionInterface
/**
* {@inheritdoc}
*
* Must run after NoBinaryStringFixer.
* Must run after NoBinaryStringFixer, NoUselessConcatOperatorFixer.
*/
public function getPriority(): int
{
Expand Down
10 changes: 10 additions & 0 deletions src/Fixer/FunctionNotation/DateTimeCreateFromFormatCallFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public function getDefinition(): FixerDefinitionInterface
);
}

/**
* {@inheritdoc}
*
* Must run after NoUselessConcatOperatorFixer.
*/
public function getPriority(): int
{
return 0;
}

public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_DOUBLE_COLON);
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/FunctionNotation/RegularCallableCallFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getDefinition(): FixerDefinitionInterface
* {@inheritdoc}
*
* Must run before NativeFunctionInvocationFixer.
* Must run after NoBinaryStringFixer.
* Must run after NoBinaryStringFixer, NoUselessConcatOperatorFixer.
*/
public function getPriority(): int
{
Expand Down
Loading

0 comments on commit aafad72

Please sign in to comment.