Skip to content

Commit

Permalink
[CodeQuality] Handle return is_array with conditional parameter insid…
Browse files Browse the repository at this point in the history
…e on ReturnTypeFromStrictScalarReturnExprRector (#4132)

* [CodeQuality] Handle return is_array with conditional parameter inside on ReturnTypeFromStrictScalarReturnExprRector

* Fixed 🎉

* [ci-review] Rector Rectify

* increase kernel cache key

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jun 9, 2023
1 parent 390730a commit 9889dd4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\PHPStanStaticTypeMapper\TypeMapper;

use PHPStan\Type\TypeCombinator;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\ConditionalTypeForParameter;
Expand Down Expand Up @@ -40,7 +41,8 @@ public function getNodeClass(): string
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNode
{
return $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($type->getTarget(), $typeKind);
$type = TypeCombinator::union($type->getIf(), $type->getElse());
return $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($type, $typeKind);
}

/**
Expand All @@ -49,6 +51,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNo
*/
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
return $this->phpStanStaticTypeMapper->mapToPhpParserNode($type->getTarget(), $typeKind);
$type = TypeCombinator::union($type->getIf(), $type->getElse());
return $this->phpStanStaticTypeMapper->mapToPhpParserNode($type, $typeKind);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture;

final class ConditionalTypeForParameter
{
public function run()
{
$username = 'xxxxxx';
$password = 'yyyyyyy';
return base64_encode(implode(':', [$username, $password]));
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture;

final class ConditionalTypeForParameter
{
public function run(): string
{
$username = 'xxxxxx';
$password = 'yyyyyyy';
return base64_encode(implode(':', [$username, $password]));
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture;

final class ReturnIsArray
{
public function run($value)
{
return is_array(ftp_nlist($value, '.'));
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture;

final class ReturnIsArray
{
public function run($value): bool
{
return is_array(ftp_nlist($value, '.'));
}
}

?>

This file was deleted.

2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v79';
private const CACHE_KEY = 'v80';

private ContainerInterface|null $container = null;

Expand Down

0 comments on commit 9889dd4

Please sign in to comment.