forked from PHP-CS-Fixer/PHP-CS-Fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Introduce
ReturnToYieldFromFixer
(PHP-CS-Fixer#7168)
- Loading branch information
1 parent
94ad3e0
commit 319d15d
Showing
13 changed files
with
351 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
============================= | ||
Rule ``return_to_yield_from`` | ||
============================= | ||
|
||
If the function explicitly returns an array, and has the return type | ||
``iterable``, then ``yield from`` must be used instead of ``return``. | ||
|
||
Examples | ||
-------- | ||
|
||
Example #1 | ||
~~~~~~~~~~ | ||
|
||
.. code-block:: diff | ||
--- Original | ||
+++ New | ||
<?php function giveMeData(): iterable { | ||
- return [1, 2, 3]; | ||
+ yield from [1, 2, 3]; | ||
} |
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,105 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* Dariusz Rumiński <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\Fixer\ArrayNotation; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; | ||
use PhpCsFixer\Tokenizer\CT; | ||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
/** | ||
* @author Kuba Werłos <[email protected]> | ||
*/ | ||
final class ReturnToYieldFromFixer extends AbstractFixer | ||
{ | ||
public function getDefinition(): FixerDefinitionInterface | ||
{ | ||
return new FixerDefinition( | ||
'If the function explicitly returns an array, and has the return type `iterable`, then `yield from` must be used instead of `return`.', | ||
[new CodeSample('<?php function giveMeData(): iterable { | ||
return [1, 2, 3]; | ||
} | ||
')], | ||
); | ||
} | ||
|
||
public function isCandidate(Tokens $tokens): bool | ||
{ | ||
return $tokens->isAllTokenKindsFound([T_FUNCTION, T_RETURN]) && $tokens->isAnyTokenKindsFound([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* Must run before YieldFromArrayToYieldsFixer. | ||
* Must run after PhpUnitDataProviderReturnTypeFixer, PhpdocToReturnTypeFixer. | ||
*/ | ||
public function getPriority(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void | ||
{ | ||
foreach ($tokens->findGivenKind(T_RETURN) as $index => $token) { | ||
if (!$this->shouldBeFixed($tokens, $index)) { | ||
continue; | ||
} | ||
|
||
$tokens[$index] = new Token([T_YIELD_FROM, 'yield from']); | ||
} | ||
} | ||
|
||
private function shouldBeFixed(Tokens $tokens, int $returnIndex): bool | ||
{ | ||
$arrayStartIndex = $tokens->getNextMeaningfulToken($returnIndex); | ||
if (!$tokens[$arrayStartIndex]->isGivenKind([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN])) { | ||
return false; | ||
} | ||
|
||
if ($tokens[$arrayStartIndex]->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) { | ||
$arrayEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $arrayStartIndex); | ||
} else { | ||
$arrayOpenParenthesisIndex = $tokens->getNextTokenOfKind($arrayStartIndex, ['(']); | ||
$arrayEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $arrayOpenParenthesisIndex); | ||
} | ||
|
||
$functionEndIndex = $arrayEndIndex; | ||
do { | ||
$functionEndIndex = $tokens->getNextMeaningfulToken($functionEndIndex); | ||
} while ($tokens[$functionEndIndex]->equals(';')); | ||
if (!$tokens[$functionEndIndex]->equals('}')) { | ||
return false; | ||
} | ||
|
||
$functionStartIndex = $tokens->findBlockStart(Tokens::BLOCK_TYPE_CURLY_BRACE, $functionEndIndex); | ||
|
||
$returnTypeIndex = $tokens->getPrevMeaningfulToken($functionStartIndex); | ||
if (!$tokens[$returnTypeIndex]->isGivenKind(T_STRING)) { | ||
return false; | ||
} | ||
|
||
if ('iterable' !== strtolower($tokens[$returnTypeIndex]->getContent())) { | ||
return false; | ||
} | ||
|
||
$beforeReturnTypeIndex = $tokens->getPrevMeaningfulToken($returnTypeIndex); | ||
|
||
return $tokens[$beforeReturnTypeIndex]->isGivenKind(CT::T_TYPE_COLON); | ||
} | ||
} |
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
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
138 changes: 138 additions & 0 deletions
138
tests/Fixer/ArrayNotation/ReturnToYieldFromFixerTest.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,138 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* Dariusz Rumiński <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\Tests\Fixer\ArrayNotation; | ||
|
||
use PhpCsFixer\Tests\Test\AbstractFixerTestCase; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \PhpCsFixer\Fixer\ArrayNotation\ReturnToYieldFromFixer | ||
*/ | ||
final class ReturnToYieldFromFixerTest extends AbstractFixerTestCase | ||
{ | ||
/** | ||
* @dataProvider provideFixCases | ||
*/ | ||
public function testFix(string $expected, ?string $input = null): void | ||
{ | ||
$this->doTest($expected, $input); | ||
} | ||
|
||
/** | ||
* @return iterable<array{0: string, 1?: string}> | ||
*/ | ||
public static function provideFixCases(): iterable | ||
{ | ||
yield ['<?php function foo() { return [1, 2, 3]; }']; | ||
|
||
yield ['<?php function foo(): MyAwesomeIterableType { return [1, 2, 3]; }']; | ||
|
||
yield ['<?php function foo(): iterable { if (true) { return [1]; } else { return [2]; } }']; | ||
|
||
yield ['<?php function foo(): ?iterable { return [1, 2, 3]; }']; | ||
|
||
yield ['<?php | ||
abstract class Foo { | ||
abstract public function bar(): iterable; | ||
public function baz(): array { return []; } | ||
} | ||
']; | ||
|
||
yield [ | ||
'<?php class Foo { | ||
function bar(): iterable { yield from [1, 2, 3]; } | ||
}', | ||
'<?php class Foo { | ||
function bar(): iterable { return [1, 2, 3]; } | ||
}', | ||
]; | ||
|
||
yield [ | ||
'<?php function foo(): iterable { yield from [1, 2, 3];;;;;;;; }', | ||
'<?php function foo(): iterable { return [1, 2, 3];;;;;;;; }', | ||
]; | ||
|
||
yield [ | ||
'<?php function foo(): iterable { yield from array(1, 2, 3); }', | ||
'<?php function foo(): iterable { return array(1, 2, 3); }', | ||
]; | ||
|
||
yield [ | ||
'<?php function foo(): iterable { $x = 0; yield from [1, 2, 3]; }', | ||
'<?php function foo(): iterable { $x = 0; return [1, 2, 3]; }', | ||
]; | ||
|
||
yield [ | ||
'<?php function foo(): iterable { $x = 0; yield from array(1, 2, 3); }', | ||
'<?php function foo(): iterable { $x = 0; return array(1, 2, 3); }', | ||
]; | ||
|
||
yield [ | ||
'<?php function foo(): ITERABLE { yield from [1, 2, 3]; }', | ||
'<?php function foo(): ITERABLE { return [1, 2, 3]; }', | ||
]; | ||
|
||
yield [ | ||
'<?php $f = function(): iterable { yield from [1, 2, 3]; };', | ||
'<?php $f = function(): iterable { return [1, 2, 3]; };', | ||
]; | ||
|
||
yield [ | ||
'<?php | ||
function foo(): array { return [3, 4]; } | ||
function bar(): iterable { yield from [1, 2]; } | ||
function baz(): int { return 5; } | ||
', | ||
'<?php | ||
function foo(): array { return [3, 4]; } | ||
function bar(): iterable { return [1, 2]; } | ||
function baz(): int { return 5; } | ||
', | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideFix80Cases | ||
* | ||
* @requires PHP 8.0 | ||
*/ | ||
public function testFix80(string $expected, ?string $input = null): void | ||
{ | ||
$this->doTest($expected, $input); | ||
} | ||
|
||
/** | ||
* @return iterable<array{0: string, 1?: string}> | ||
*/ | ||
public static function provideFix80Cases(): iterable | ||
{ | ||
yield [ | ||
'<?php function foo(): null|iterable { return [1, 2, 3]; }', | ||
]; | ||
|
||
yield [ | ||
'<?php function foo(): iterable|null { return [1, 2, 3]; }', | ||
]; | ||
|
||
yield [ | ||
'<?php function foo(): ITERABLE|null { return [1, 2, 3]; }', | ||
]; | ||
|
||
yield [ | ||
'<?php function foo(): Bar|iterable { return [1, 2, 3]; }', | ||
]; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ixtures/Integration/priority/php_unit_data_provider_return_type,return_to_yield_from.test
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,27 @@ | ||
--TEST-- | ||
Integration of fixers: php_unit_data_provider_return_type,return_to_yield_from. | ||
--RULESET-- | ||
{"php_unit_data_provider_return_type": true, "return_to_yield_from": true} | ||
--EXPECT-- | ||
<?php | ||
class FooTest extends TestCase { | ||
/** | ||
* @dataProvider provideFooCases | ||
*/ | ||
function testFoo() {} | ||
function provideFooCases(): iterable { | ||
yield from [[1], [2], [3]]; | ||
} | ||
} | ||
|
||
--INPUT-- | ||
<?php | ||
class FooTest extends TestCase { | ||
/** | ||
* @dataProvider provideFooCases | ||
*/ | ||
function testFoo() {} | ||
function provideFooCases() { | ||
return [[1], [2], [3]]; | ||
} | ||
} |
Oops, something went wrong.