Skip to content

Commit

Permalink
[Php80] Handle crash after transform with trait-string on UnionTypesR…
Browse files Browse the repository at this point in the history
…ector (#2985)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Oct 14, 2022
1 parent 1bf1547 commit 2427c5a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareIntersectionTypeNode;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
Expand Down Expand Up @@ -90,7 +89,7 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
$resolvedTypeName = self::STRING;
$resolvedType = new Name(self::STRING);
} elseif (! $resolvedType instanceof Name) {
throw new ShouldNotHappenException();
return null;
} else {
$resolvedTypeName = (string) $resolvedType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return new FullyQualified($className);
}

if ($type instanceof NonExistingObjectType) {
return null;
}

if (! $type instanceof GenericObjectType) {
// fallback
return new FullyQualified($type->getClassName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureUnionIntersectionTypes;

use MockObject;
use ObjectRepository;
use PHPUnit\Framework\MockObject\MockObject;
use Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\Doctrine\ObjectRepository;

final class DemoFile
{
Expand All @@ -19,15 +19,15 @@ final class DemoFile

namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureUnionIntersectionTypes;

use MockObject;
use ObjectRepository;
use PHPUnit\Framework\MockObject\MockObject;
use Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\Doctrine\ObjectRepository;

final class DemoFile
{
/**
* @var MockObject&ObjectRepository<ItemCountry>
*/
private \MockObject&\ObjectRepository $itemCountryRepo;
private \PHPUnit\Framework\MockObject\MockObject&\Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\Doctrine\ObjectRepository $itemCountryRepo;
}

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

namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureUnionIntersectionTypes;

final class SkipObjectTypeNotExists
{
/**
* @var NotExistClass1&NotExistClass2<ItemCountry>
*/
private $itemCountryRepo;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;

class SkipUnionTraitString
{
/**
* @param string|trait-string $fromId
*/
public function withFromId(string $fromId): static
{
return $this;
}
}

0 comments on commit 2427c5a

Please sign in to comment.