Skip to content

Commit

Permalink
Fix type without class name mapping without substracted type (#1104)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 29, 2021
1 parent 99f0681 commit a7d8b39
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use Rector\StaticTypeMapper\ValueObject\Type\ParentObjectWithoutClassType;
use Symfony\Contracts\Service\Attribute\Required;

/**
* @implements TypeMapperInterface<ObjectWithoutClassType>
*/
final class ObjectWithoutClassTypeMapper implements TypeMapperInterface
{
private PHPStanStaticTypeMapper $phpStanStaticTypeMapper;

public function __construct(
private PhpVersionProvider $phpVersionProvider
) {
Expand Down Expand Up @@ -62,21 +58,10 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): Type
*/
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node
{
$subtractedType = $type->getSubtractedType();
if ($subtractedType !== null) {
return $this->phpStanStaticTypeMapper->mapToPhpParserNode($subtractedType, $typeKind);
}

if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::OBJECT_TYPE)) {
return null;
}

return new Name('object');
}

#[Required]
public function autowireObjectWithoutClassTypeMapper(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
{
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;

use MyCLabs\Enum\Enum;

final class SkipInstanceOfEnum
{
public function run(object $object)
{
if ($object instanceof Enum) {
return;
}

return $this->processTheRest($object);
}

private function processTheRest($object)
{
return $object;
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;

use MyCLabs\Enum\Enum;

final class SkipInstanceOfEnum
{
public function run(object $object)
{
if ($object instanceof Enum) {
return;
}

return $this->processTheRest($object);
}

private function processTheRest(object $object)
{
return $object;
}
}

?>
2 changes: 2 additions & 0 deletions rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function resolveStrictTypesFromCalls(array $calls): array
}

$argValueType = $this->resolveStrictArgValueType($arg);

$staticTypesByArgumentPosition[$position][] = $argValueType;
}
}
Expand Down Expand Up @@ -82,6 +83,7 @@ private function unionToSingleType(array $staticTypesByArgumentPosition): array

// narrow parents to most child type
$unionedType = $this->narrowParentObjectTreeToSingleObjectChildType($unionedType);

$staticTypeByArgumentPosition[$position] = $unionedType;
}

Expand Down

0 comments on commit a7d8b39

Please sign in to comment.