-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix] Make UnionTypesRector and ReturnAnnotationIncorrectNullableRect…
…or skip chaotic methods (#2067) * [Fix] Make UnionTypesRector and ReturnAnnotationIncorrectNullableRector skip chaotic methods * [Fix] Fix UnionTypesRector Co-authored-by: Jiří Bok <jiri.bok@protonmail.com>
- Loading branch information
1 parent
dd96863
commit 0ba8579
Showing
10 changed files
with
117 additions
and
2 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
18 changes: 18 additions & 0 deletions
18
rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_chaotic_methods.php.inc
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,18 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
final class SkipAnnotationsOnChaoticMethods extends NodeVisitorAbstract | ||
{ | ||
/** | ||
* @return Node|null | ||
*/ | ||
public function enterNode(Node $node): ?Node | ||
{ | ||
return $node; | ||
} | ||
} | ||
|
14 changes: 14 additions & 0 deletions
14
...ests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_removing_mixed_array.php.inc
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,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture; | ||
|
||
class SkipRemovingMixedReturnType | ||
{ | ||
/** | ||
* @return mixed[] | ||
*/ | ||
private function x(): array | ||
{ | ||
return []; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_removing_mixed_array_or_null.php.inc
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,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture; | ||
|
||
class SkipRemovingNullableMixedReturnType | ||
{ | ||
/** | ||
* @return mixed[]|null | ||
*/ | ||
private function x(bool $returnNull): ?array | ||
{ | ||
return $returnNull ? null : []; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
.../ClassMethod/ReturnAnnotationIncorrectNullableRector/Fixture/skip-chaotic-methods.php.inc
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,18 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
final class SkipAnnotationsOnChaoticMethods extends NodeVisitorAbstract | ||
{ | ||
/** | ||
* @return Node | ||
*/ | ||
public function enterNode(Node $node): ?Node | ||
{ | ||
return $node; | ||
} | ||
} | ||
|
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
32 changes: 32 additions & 0 deletions
32
rules/DeadCode/TypeNodeAnalyzer/MixedArrayTypeNodeAnalyzer.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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DeadCode\TypeNodeAnalyzer; | ||
|
||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; | ||
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; | ||
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareArrayTypeNode; | ||
|
||
final class MixedArrayTypeNodeAnalyzer | ||
{ | ||
public function hasMixedArrayType(UnionTypeNode $unionTypeNode): bool | ||
{ | ||
$types = $unionTypeNode->types; | ||
|
||
foreach ($types as $type) { | ||
if ($type instanceof SpacingAwareArrayTypeNode) { | ||
$typeNode = $type->type; | ||
if (! $typeNode instanceof IdentifierTypeNode) { | ||
continue; | ||
} | ||
|
||
if ($typeNode->name === 'mixed') { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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