Skip to content

Commit

Permalink
fix: SimplifiedNullReturnFixer - support array return typehint (PHP-C…
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus authored Jan 14, 2024
1 parent 9922e5b commit 04e3513
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ parameters:
-
message: '#^Method PhpCsFixer\\Tests\\.+::provide.+Cases\(\) return type has no value type specified in iterable type iterable\.$#'
path: tests
count: 1034
count: 1025

-
message: '#Call to static method .+ with .+ will always evaluate to true.$#'
Expand Down
3 changes: 2 additions & 1 deletion src/Fixer/ReturnNotation/SimplifiedNullReturnFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ private function isStrictOrNullableReturnTypeFunction(Tokens $tokens, int $retur
} while ($closingCurlyBraceIndex < $returnIndex);

$possibleVoidIndex = $tokens->getPrevMeaningfulToken($openingCurlyBraceIndex);
$isStrictReturnType = $tokens[$possibleVoidIndex]->isGivenKind(T_STRING) && 'void' !== $tokens[$possibleVoidIndex]->getContent();
$isStrictReturnType = $tokens[$possibleVoidIndex]->isGivenKind([T_STRING, CT::T_ARRAY_TYPEHINT])
&& 'void' !== $tokens[$possibleVoidIndex]->getContent();

$nullableTypeIndex = $tokens->getNextTokenOfKind($functionIndex, [[CT::T_NULLABLE_TYPE]]);
$isNullableReturnType = null !== $nullableTypeIndex && $nullableTypeIndex < $openingCurlyBraceIndex;
Expand Down
3 changes: 3 additions & 0 deletions tests/Fixer/ReturnNotation/NoUselessReturnFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public function testFix(string $expected, ?string $input = null): void
$this->doTest($expected, $input);
}

/**
* @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
*/
public static function provideFixCases(): iterable
{
yield [
Expand Down
21 changes: 21 additions & 0 deletions tests/Fixer/ReturnNotation/ReturnAssignmentFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public function testFixNestedFunctions(string $expected, string $input): void
$this->doTest($expected, $input);
}

/**
* @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
*/
public static function provideFixNestedFunctionsCases(): iterable
{
yield [
Expand Down Expand Up @@ -189,6 +192,9 @@ public function testFix(string $expected, string $input): void
$this->doTest($expected, $input);
}

/**
* @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
*/
public static function provideFixCases(): iterable
{
yield [
Expand Down Expand Up @@ -819,6 +825,9 @@ public function testDoNotFix(string $expected): void
$this->doTest($expected);
}

/**
* @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
*/
public static function provideDoNotFixCases(): iterable
{
yield 'invalid reference stays invalid' => [
Expand Down Expand Up @@ -1261,6 +1270,9 @@ public function testDoNotFix80(string $expected): void
$this->doTest($expected);
}

/**
* @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
*/
public static function provideDoNotFix80Cases(): iterable
{
yield 'try with non-capturing catch block' => [
Expand Down Expand Up @@ -1291,6 +1303,9 @@ public function testRepetitiveFix(string $expected, ?string $input = null): void
$this->doTest($expected, $input);
}

/**
* @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
*/
public static function provideRepetitiveFixCases(): iterable
{
yield [
Expand Down Expand Up @@ -1344,6 +1359,9 @@ public function testFix80(string $expected, ?string $input = null): void
$this->doTest($expected, $input);
}

/**
* @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'match' => [
Expand Down Expand Up @@ -1394,6 +1412,9 @@ public function testFixPhp83(string $expected, ?string $input = null): void
$this->doTest($expected, $input);
}

/**
* @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
*/
public static function provideFixPhp83Cases(): iterable
{
yield 'anonymous readonly class' => [
Expand Down
46 changes: 46 additions & 0 deletions tests/Fixer/ReturnNotation/SimplifiedNullReturnFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public function testFix(string $expected, ?string $input = null): void
$this->doTest($expected, $input);
}

/**
* @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
*/
public static function provideFixCases(): iterable
{
// check correct statements aren't changed
Expand Down Expand Up @@ -90,4 +93,47 @@ public static function provideFixCases(): iterable
'<?php function foo(): void { return; }',
];
}

/**
* @requires PHP 8.0
*
* @dataProvider provideFix80Cases
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}

/**
* @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php
function test(): null|int
{
if (true) { return null; }
return 42;
}',
];

yield [
'<?php
function test(): null|array
{
if (true) { return null; }
return [];
}',
];

yield [
'<?php
function test(): array|null
{
if (true) { return null; }
return [];
}',
];
}
}

0 comments on commit 04e3513

Please sign in to comment.