Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! fixing import rectors
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 11, 2019
1 parent 2933325 commit 6cc691e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,9 @@ public function getParamTypeByName(FunctionLike $functionLike, string $paramName
private function addTypeSpecificTag(Node $node, string $name, Type $type): void
{
$docStringType = $this->staticTypeMapper->mapPHPStanTypeToDocString($type);
if ($docStringType === '') {
return;
}

// there might be no phpdoc at all
if ($node->getDocComment() !== null) {
Expand Down
3 changes: 0 additions & 3 deletions packages/NodeTypeResolver/src/StaticTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ public function mapPHPStanTypeToPhpParserNode(Type $phpStanType, ?string $kind =
return null;
}


if ($phpStanType instanceof SelfObjectType) {
return new Identifier('self');
}
Expand Down Expand Up @@ -252,8 +251,6 @@ public function mapPHPStanTypeToPhpParserNode(Type $phpStanType, ?string $kind =
}

if ($phpStanType instanceof UnionType) {


// match array types
$arrayNode = $this->matchArrayTypes($phpStanType);
if ($arrayNode) {
Expand Down
39 changes: 38 additions & 1 deletion packages/TypeDeclaration/src/TypeInferer/ReturnTypeInferer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
namespace Rector\TypeDeclaration\TypeInferer;

use PhpParser\Node\FunctionLike;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\TypeDeclaration\Contract\TypeInferer\ReturnTypeInfererInterface;

final class ReturnTypeInferer extends AbstractPriorityAwareTypeInferer
Expand All @@ -15,12 +19,18 @@ final class ReturnTypeInferer extends AbstractPriorityAwareTypeInferer
*/
private $returnTypeInferers = [];

/**
* @var TypeFactory
*/
private $typeFactory;

/**
* @param ReturnTypeInfererInterface[] $returnTypeInferers
*/
public function __construct(array $returnTypeInferers)
public function __construct(TypeFactory $typeFactory, array $returnTypeInferers)
{
$this->returnTypeInferers = $this->sortTypeInferersByPriority($returnTypeInferers);
$this->typeFactory = $typeFactory;
}

public function inferFunctionLike(FunctionLike $functionLike): Type
Expand All @@ -39,6 +49,8 @@ public function inferFunctionLikeWithExcludedInferers(FunctionLike $functionLike
}

$type = $returnTypeInferer->inferFunctionLike($functionLike);
$type = $this->normalizeArrayTypeAndArrayNever($type);

if (! $type instanceof MixedType) {
return $type;
}
Expand Down Expand Up @@ -73,4 +85,29 @@ private function ensureIsTypeInferer(string $excludedInferer): void

throw new ShouldNotHappenException();
}

/**
* From "string[]|mixed[]" based on empty array to to "string[]"
*/
private function normalizeArrayTypeAndArrayNever(Type $type): Type
{
if (! $type instanceof UnionType) {
return $type;
}

$nonNeverTypes = [];
foreach ($type->getTypes() as $unionedType) {
if (! $unionedType instanceof ArrayType) {
return $type;
}

if ($unionedType->getItemType() instanceof NeverType) {
continue;
}

$nonNeverTypes[] = $unionedType;
}

return $this->typeFactory->createMixedPassedOrUnionType($nonNeverTypes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ final class AddArrayReturnDocTypeRectorTest extends AbstractRectorTestCase
public function test(): void
{
$this->doTestFiles([
// fix these 2
__DIR__ . '/Fixture/setter_based.php.inc',
__DIR__ . '/Fixture/simple_array.php.inc',
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/setter_based.php.inc',
__DIR__ . '/Fixture/fully_qualified_name.php.inc',
__DIR__ . '/Fixture/fully_qualified_name_nested_array.php.inc',
__DIR__ . '/Fixture/yield_strings.php.inc',
Expand Down

0 comments on commit 6cc691e

Please sign in to comment.