Skip to content

Commit

Permalink
Updated Rector to commit 52e3fdfb48a507be997bd0f47256ad14854be844
Browse files Browse the repository at this point in the history
rectorphp/rector-src@52e3fdf [TypeDeclaration] Fix deep nested array docblock with \ prefix on ReturnTypeFromStrictNewArrayRector (#6373)
  • Loading branch information
TomasVotruba committed Oct 11, 2024
1 parent 103280d commit 7595ee2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
8 changes: 4 additions & 4 deletions rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ public function refactor(Node $node) : ?Node
}
return null;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::MATCH_EXPRESSION;
}
private function castMatchCond(Match_ $match) : void
{
$type = $this->nodeTypeResolver->getNativeType($match->cond);
Expand Down Expand Up @@ -169,10 +173,6 @@ private function castMatchCond(Match_ $match) : void
$match->cond = $newMatchCond;
}
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::MATCH_EXPRESSION;
}
/**
* @param CondAndExpr[] $condAndExprs
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -192,7 +193,12 @@ private function changeReturnType($node, ArrayType $arrayType) : void
if ($arrayType instanceof ConstantArrayType && \count($arrayType->getValueTypes()) !== 1) {
return;
}
$narrowArrayType = new ArrayType(new MixedType(), $arrayType->getItemType());
$itemType = $arrayType->getItemType();
if ($itemType instanceof IntersectionType) {
$narrowArrayType = $arrayType;
} else {
$narrowArrayType = new ArrayType(new MixedType(), $itemType);
}
$this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $narrowArrayType);
}
/**
Expand Down
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 = '16f939995e159c934f0dcd1179a26a56fe64b65b';
public const PACKAGE_VERSION = '52e3fdfb48a507be997bd0f47256ad14854be844';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-10-11 13:52:42';
public const RELEASE_DATE = '2024-10-11 15:33:12';
/**
* @var int
*/
Expand Down
38 changes: 32 additions & 6 deletions src/PHPStanStaticTypeMapper/TypeMapper/IntersectionTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\PhpDocParser\Ast\Node as AstNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use Rector\Php\PhpVersionProvider;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\StaticTypeMapper\Mapper\ScalarStringToTypeMapper;
use Rector\ValueObject\PhpVersionFeature;
/**
* @implements TypeMapperInterface<IntersectionType>
Expand All @@ -36,11 +40,17 @@ final class IntersectionTypeMapper implements TypeMapperInterface
* @var \Rector\PHPStanStaticTypeMapper\TypeMapper\ObjectTypeMapper
*/
private $objectTypeMapper;
public function __construct(PhpVersionProvider $phpVersionProvider, \Rector\PHPStanStaticTypeMapper\TypeMapper\ObjectWithoutClassTypeMapper $objectWithoutClassTypeMapper, \Rector\PHPStanStaticTypeMapper\TypeMapper\ObjectTypeMapper $objectTypeMapper)
/**
* @readonly
* @var \Rector\StaticTypeMapper\Mapper\ScalarStringToTypeMapper
*/
private $scalarStringToTypeMapper;
public function __construct(PhpVersionProvider $phpVersionProvider, \Rector\PHPStanStaticTypeMapper\TypeMapper\ObjectWithoutClassTypeMapper $objectWithoutClassTypeMapper, \Rector\PHPStanStaticTypeMapper\TypeMapper\ObjectTypeMapper $objectTypeMapper, ScalarStringToTypeMapper $scalarStringToTypeMapper)
{
$this->phpVersionProvider = $phpVersionProvider;
$this->objectWithoutClassTypeMapper = $objectWithoutClassTypeMapper;
$this->objectTypeMapper = $objectTypeMapper;
$this->scalarStringToTypeMapper = $scalarStringToTypeMapper;
}
public function getNodeClass() : string
{
Expand All @@ -53,12 +63,28 @@ public function mapToPHPStanPhpDocTypeNode(Type $type) : TypeNode
{
$typeNode = $type->toPhpDocNode();
$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->traverseWithCallable($typeNode, '', static function (AstNode $astNode) : ?IdentifierTypeNode {
if ($astNode instanceof IdentifierTypeNode) {
$astNode->name = '\\' . \ltrim($astNode->name, '\\');
return $astNode;
$phpDocNodeTraverser->traverseWithCallable($typeNode, '', function (AstNode $astNode) {
if ($astNode instanceof UnionTypeNode) {
return PhpDocNodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
return null;
if ($astNode instanceof ArrayShapeItemNode) {
return PhpDocNodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if (!$astNode instanceof IdentifierTypeNode) {
return PhpDocNodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
$type = $this->scalarStringToTypeMapper->mapScalarStringToType($astNode->name);
if ($type->isScalar()->yes()) {
return PhpDocNodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if ($type->isArray()->yes()) {
return PhpDocNodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if ($type instanceof MixedType && $type->isExplicitMixed()) {
return PhpDocNodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
$astNode->name = '\\' . \ltrim($astNode->name, '\\');
return $astNode;
});
return $typeNode;
}
Expand Down
3 changes: 3 additions & 0 deletions src/StaticTypeMapper/Mapper/ScalarStringToTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public function mapScalarStringToType(string $scalarName) : Type
if ($loweredScalarName === 'array') {
return new ArrayType(new MixedType(), new MixedType());
}
if ($loweredScalarName === 'non-empty-array') {
return new ArrayType(new MixedType(), new MixedType());
}
if ($loweredScalarName === 'iterable') {
return new IterableType(new MixedType(), new MixedType());
}
Expand Down

0 comments on commit 7595ee2

Please sign in to comment.