* Dariusz Rumiński * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ReturnNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ReturnNotation\ReturnAssignmentFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ReturnNotation\ReturnAssignmentFixer> */ final class ReturnAssignmentFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixNestedFunctionsCases */ public function testFixNestedFunctions(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixNestedFunctionsCases(): iterable { yield [ 'doTest($expected, $input); } /** * @return iterable */ public static function provideFixCases(): iterable { yield [ ' [ ' [ '1){echo 1;} return (1 + 2 + $b); }', '1){echo 1;} $a= (1 + 2 + $b);return $a; }', ]; yield [ ' a(); } ', 'a(); return $a; } ', [ ' [ ' [ ' [ ' $x + $y; } ', ' $x + $y; return $fn1; } ', ]; yield 'try catch' => [ ' [ 'getMessage()); } if (isSomeCondition()) { return getSomeResult(); } try { $result = $a + $b; return $result; } catch (\Throwable $th) { var_dump($result ?? null); } } ', 'getMessage()); } if (isSomeCondition()) { $result = getSomeResult(); return $result; } try { $result = $a + $b; return $result; } catch (\Throwable $th) { var_dump($result ?? null); } } ', ]; yield 'try/catch/finally' => [ 'getMessage()); } finally { baz($result); } } ', 'getMessage()); } finally { baz($result); } } ', ]; yield 'multiple try/catch separated with conditional return, with finally block' => [ 'getMessage()); } if (isSomeCondition()) { return getSomeResult(); } try { $result = $a + $b; return $result; } catch (\Throwable $th) { throw $th; } finally { echo "result:", $result, \PHP_EOL; } } ', 'getMessage()); } if (isSomeCondition()) { $result = getSomeResult(); return $result; } try { $result = $a + $b; return $result; } catch (\Throwable $th) { throw $th; } finally { echo "result:", $result, \PHP_EOL; } } ', ]; } /** * @dataProvider provideDoNotFixCases */ public function testDoNotFix(string $expected): void { $this->doTest($expected); } /** * @return iterable */ public static function provideDoNotFixCases(): iterable { yield 'invalid reference stays invalid' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ 'doTest($expected); } /** * @return iterable */ public static function provideDoNotFix80Cases(): iterable { yield 'try with non-capturing catch block' => [ 'doTest($expected, $input); } /** * @return iterable */ public static function provideRepetitiveFixCases(): iterable { yield [ 'doTest($expected, $input); } /** * @return iterable */ public static function provideFix80Cases(): iterable { yield 'match' => [ ' "This food is an apple", "cake" => "This food is a cake", }; } ', ' "This food is an apple", "cake" => "This food is a cake", }; return $return_value; } ', ]; yield 'attribute before anonymous `class`' => [ 'doTest($expected, $input); } /** * @return iterable */ public static function provideFixPhp83Cases(): iterable { yield 'anonymous readonly class' => [ ' [ '