-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: NoUselessNullsafeOperatorFixer - Introduction (#6425)
NoUselessNullsafeOperatorFixer - Introduction
- Loading branch information
1 parent
ccab1db
commit 83071d9
Showing
7 changed files
with
194 additions
and
0 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
===================================== | ||
Rule ``no_useless_nullsafe_operator`` | ||
===================================== | ||
|
||
There should not be useless ``null-safe-operators`` ``?->`` used. | ||
|
||
Examples | ||
-------- | ||
|
||
Example #1 | ||
~~~~~~~~~~ | ||
|
||
.. code-block:: diff | ||
--- Original | ||
+++ New | ||
<?php | ||
class Foo extends Bar | ||
{ | ||
public function test() { | ||
- echo $this?->parentMethod(); | ||
+ echo $this->parentMethod(); | ||
} | ||
} | ||
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_nullsafe_operator`` rule. | ||
|
||
@Symfony | ||
Using the `@Symfony <./../../ruleSets/Symfony.rst>`_ rule set will enable the ``no_useless_nullsafe_operator`` rule. |
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\Fixer\Operator; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; | ||
use PhpCsFixer\FixerDefinition\VersionSpecification; | ||
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample; | ||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
final class NoUselessNullsafeOperatorFixer extends AbstractFixer | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefinition(): FixerDefinitionInterface | ||
{ | ||
return new FixerDefinition( | ||
'There should not be useless `null-safe-operators` `?->` used.', | ||
[ | ||
new VersionSpecificCodeSample( | ||
'<?php | ||
class Foo extends Bar | ||
{ | ||
public function test() { | ||
echo $this?->parentMethod(); | ||
} | ||
} | ||
', | ||
new VersionSpecification(80000) | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isCandidate(Tokens $tokens): bool | ||
{ | ||
return \PHP_VERSION_ID >= 80000 && $tokens->isAllTokenKindsFound([T_VARIABLE, T_NULLSAFE_OBJECT_OPERATOR]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void | ||
{ | ||
for ($index = $tokens->count() - 1; $index >= 0; --$index) { | ||
if (!$tokens[$index]->isGivenKind(T_NULLSAFE_OBJECT_OPERATOR)) { | ||
continue; | ||
} | ||
|
||
$nullsafeObjectOperatorIndex = $index; | ||
$index = $tokens->getPrevMeaningfulToken($index); | ||
|
||
if (!$tokens[$index]->isGivenKind(T_VARIABLE)) { | ||
continue; | ||
} | ||
|
||
if ('$this' !== strtolower($tokens[$index]->getContent())) { | ||
continue; | ||
} | ||
|
||
$tokens[$nullsafeObjectOperatorIndex] = new Token([T_OBJECT_OPERATOR, '->']); | ||
} | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
tests/Fixer/Operator/NoUselessNullsafeOperatorFixerTest.php
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\Tests\Fixer\Operator; | ||
|
||
use PhpCsFixer\Tests\Test\AbstractFixerTestCase; | ||
|
||
/** | ||
* @internal | ||
* @requires PHP 8.0 | ||
* | ||
* @covers \PhpCsFixer\Fixer\Operator\NoUselessNullsafeOperatorFixer | ||
*/ | ||
final class NoUselessNullsafeOperatorFixerTest extends AbstractFixerTestCase | ||
{ | ||
/** | ||
* @dataProvider provideFixCases | ||
*/ | ||
public function testFix(string $expected, ?string $input = null): void | ||
{ | ||
$this->doTest($expected, $input); | ||
} | ||
|
||
public function provideFixCases(): iterable | ||
{ | ||
yield 'simple case + comment' => [ | ||
'<?php $a = new class extends foo { | ||
public function bar() { | ||
$this->g(); | ||
// $this?->g(); | ||
} | ||
};', | ||
'<?php $a = new class extends foo { | ||
public function bar() { | ||
$this?->g(); | ||
// $this?->g(); | ||
} | ||
};', | ||
]; | ||
|
||
yield 'multiple casing cases + comment + no candidate' => [ | ||
'<?php $a = new class extends foo { | ||
public function bar() { | ||
return $THIS /*1*/ -> g().$THis->g().$this->do()?->o(); | ||
} | ||
};', | ||
'<?php $a = new class extends foo { | ||
public function bar() { | ||
return $THIS /*1*/ ?-> g().$THis?->g().$this->do()?->o(); | ||
} | ||
};', | ||
]; | ||
} | ||
} |