Skip to content

Commit

Permalink
[StaticTypeMapper] Clean up NameNodeMapper check Scalar and class exi…
Browse files Browse the repository at this point in the history
…sts (#5865)

* [StaticTypeMapper] Clean up NameNodeMapper check Scalar and class exists

* [ci-review] Rector Rectify

* fix phpstan

* add skip more detail type on AddMethodCallBasedStrictParamTypeRector

* Fix phpstan

* [ci-review] Rector Rectify

* remove FullyQualified check

* make ExprNodeMapper last

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed May 10, 2024
1 parent 558f44d commit 48fc55d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,14 @@ final class LazyContainerFactory
* @var array<class-string<PhpParserNodeMapperInterface>>
*/
private const PHP_PARSER_NODE_MAPPER_CLASSES = [
ExprNodeMapper::class,
FullyQualifiedNodeMapper::class,
IdentifierNodeMapper::class,
IntersectionTypeNodeMapper::class,
NameNodeMapper::class,
NullableTypeNodeMapper::class,
StringNodeMapper::class,
UnionTypeNodeMapper::class,
ExprNodeMapper::class,
];

/**
Expand Down
12 changes: 1 addition & 11 deletions src/StaticTypeMapper/Mapper/PhpParserNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
namespace Rector\StaticTypeMapper\Mapper;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\Type;
use Rector\Exception\NotImplementedYetException;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -33,15 +31,7 @@ public function mapToPHPStanType(Node $node): Type
continue;
}

// do not let Expr collect all the types
// note: can be solve later with priorities on mapper interface, making this last
if ($phpParserNodeMapper->getNodeType() !== Expr::class) {
return $phpParserNodeMapper->mapToPHPStan($nameOrExpr);
}

if (! $nameOrExpr instanceof String_) {
return $phpParserNodeMapper->mapToPHPStan($nameOrExpr);
}
return $phpParserNodeMapper->mapToPHPStan($nameOrExpr);
}

throw new NotImplementedYetException($nameOrExpr::class);
Expand Down
73 changes: 6 additions & 67 deletions src/StaticTypeMapper/PhpParser/NameNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,13 @@
use PhpParser\Node;
use PhpParser\Node\Name;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StringType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use Rector\Configuration\RenamedClassesDataCollector;
use Rector\Enum\ObjectReference;
use Rector\Reflection\ReflectionResolver;
use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\ParentObjectWithoutClassType;
use Rector\StaticTypeMapper\ValueObject\Type\ParentStaticType;
use Rector\StaticTypeMapper\ValueObject\Type\SelfStaticType;
Expand All @@ -34,8 +24,6 @@
final readonly class NameNodeMapper implements PhpParserNodeMapperInterface
{
public function __construct(
private RenamedClassesDataCollector $renamedClassesDataCollector,
private ReflectionProvider $reflectionProvider,
private ReflectionResolver $reflectionResolver
) {
}
Expand All @@ -51,27 +39,12 @@ public function getNodeType(): string
public function mapToPHPStan(Node $node): Type
{
$name = $node->toString();
if ($this->isExistingClass($name)) {
return new FullyQualifiedObjectType($name);
}

if (in_array($name, [ObjectReference::STATIC, ObjectReference::SELF, ObjectReference::PARENT], true)) {
if ($node->isSpecialClassName()) {
return $this->createClassReferenceType($node, $name);
}

return $this->createScalarType($name);
}

private function isExistingClass(string $name): bool
{
if ($this->reflectionProvider->hasClass($name)) {
return true;
}

// to be existing class names
$oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses();

return in_array($name, $oldToNewClasses, true);
return new MixedType();
}

private function createClassReferenceType(
Expand All @@ -91,45 +64,11 @@ private function createClassReferenceType(
return new SelfStaticType($classReflection);
}

if ($reference === ObjectReference::PARENT) {
$parentClassReflection = $classReflection->getParentClass();
if ($parentClassReflection instanceof ClassReflection) {
return new ParentStaticType($parentClassReflection);
}

return new ParentObjectWithoutClassType();
}

return new ThisType($classReflection);
}

private function createScalarType(
string $name
): ArrayType | IntegerType | FloatType | StringType | ConstantBooleanType | BooleanType | MixedType {
if ($name === 'array') {
return new ArrayType(new MixedType(), new MixedType());
}

if ($name === 'int') {
return new IntegerType();
}

if ($name === 'float') {
return new FloatType();
}

if ($name === 'string') {
return new StringType();
}

if ($name === 'false') {
return new ConstantBooleanType(false);
$parentClassReflection = $classReflection->getParentClass();
if ($parentClassReflection instanceof ClassReflection) {
return new ParentStaticType($parentClassReflection);
}

if ($name === 'bool') {
return new BooleanType();
}

return new MixedType();
return new ParentObjectWithoutClassType();
}
}

0 comments on commit 48fc55d

Please sign in to comment.