Skip to content

Commit

Permalink
Updated Rector to commit 48fc55d89350f24521a197c04b5512452a3b50fc
Browse files Browse the repository at this point in the history
rectorphp/rector-src@48fc55d [StaticTypeMapper] Clean up NameNodeMapper check Scalar and class exists (#5865)
  • Loading branch information
TomasVotruba committed May 10, 2024
1 parent fe89136 commit 322f3ad
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 82 deletions.
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '558f44d5939be4314d76f4850a34be3590e65bfd';
public const PACKAGE_VERSION = '48fc55d89350f24521a197c04b5512452a3b50fc';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-05-10 10:10:29';
public const RELEASE_DATE = '2024-05-10 10:25:34';
/**
* @var int
*/
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ 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];
private const PHP_PARSER_NODE_MAPPER_CLASSES = [FullyQualifiedNodeMapper::class, IdentifierNodeMapper::class, IntersectionTypeNodeMapper::class, NameNodeMapper::class, NullableTypeNodeMapper::class, StringNodeMapper::class, UnionTypeNodeMapper::class, ExprNodeMapper::class];
/**
* @var array<class-string<SkipVoterInterface>>
*/
Expand Down
11 changes: 1 addition & 10 deletions src/StaticTypeMapper/Mapper/PhpParserNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,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,14 +31,7 @@ public function mapToPHPStanType(Node $node) : Type
if (!\is_a($nameOrExpr, $phpParserNodeMapper->getNodeType())) {
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(\get_class($nameOrExpr));
}
Expand Down
76 changes: 7 additions & 69 deletions src/StaticTypeMapper/PhpParser/NameNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,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 @@ -31,25 +21,13 @@
*/
final class NameNodeMapper implements PhpParserNodeMapperInterface
{
/**
* @readonly
* @var \Rector\Configuration\RenamedClassesDataCollector
*/
private $renamedClassesDataCollector;
/**
* @readonly
* @var \PHPStan\Reflection\ReflectionProvider
*/
private $reflectionProvider;
/**
* @readonly
* @var \Rector\Reflection\ReflectionResolver
*/
private $reflectionResolver;
public function __construct(RenamedClassesDataCollector $renamedClassesDataCollector, ReflectionProvider $reflectionProvider, ReflectionResolver $reflectionResolver)
public function __construct(ReflectionResolver $reflectionResolver)
{
$this->renamedClassesDataCollector = $renamedClassesDataCollector;
$this->reflectionProvider = $reflectionProvider;
$this->reflectionResolver = $reflectionResolver;
}
public function getNodeType() : string
Expand All @@ -62,22 +40,10 @@ 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();
}
/**
* @return \PHPStan\Type\MixedType|\PHPStan\Type\StaticType|\Rector\StaticTypeMapper\ValueObject\Type\SelfStaticType|\PHPStan\Type\ObjectWithoutClassType
Expand All @@ -94,38 +60,10 @@ private function createClassReferenceType(Name $name, string $reference)
if ($reference === ObjectReference::SELF) {
return new SelfStaticType($classReflection);
}
if ($reference === ObjectReference::PARENT) {
$parentClassReflection = $classReflection->getParentClass();
if ($parentClassReflection instanceof ClassReflection) {
return new ParentStaticType($parentClassReflection);
}
return new ParentObjectWithoutClassType();
$parentClassReflection = $classReflection->getParentClass();
if ($parentClassReflection instanceof ClassReflection) {
return new ParentStaticType($parentClassReflection);
}
return new ThisType($classReflection);
}
/**
* @return \PHPStan\Type\ArrayType|\PHPStan\Type\IntegerType|\PHPStan\Type\FloatType|\PHPStan\Type\StringType|\PHPStan\Type\Constant\ConstantBooleanType|\PHPStan\Type\BooleanType|\PHPStan\Type\MixedType
*/
private function createScalarType(string $name)
{
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);
}
if ($name === 'bool') {
return new BooleanType();
}
return new MixedType();
return new ParentObjectWithoutClassType();
}
}

0 comments on commit 322f3ad

Please sign in to comment.