Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Source\ConnectionInterface;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Source\ConnectionResolverInterface;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Source\SqlServerConnection;

final class SkipValidTypeFromReturnDoc
{
public function __invoke(ConnectionResolverInterface $resolver): void
{
$connection = $resolver->connection();

if (
$connection instanceof SqlServerConnection
|| ! method_exists($connection, 'getSchemaState')
) {
return;
}

$this->ensureCleanDatabase($connection);
}

private function ensureCleanDatabase(ConnectionInterface $connection): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

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

interface ConnectionInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

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

interface ConnectionResolverInterface
{
/**
* @param string|null $name
* @return ConnectionInterface
*/
public function connection($name = null);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

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

class SqlServerConnection extends ConnectionInterface
{
}
10 changes: 5 additions & 5 deletions rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ private function resolveStrictArgValueType(Arg $arg): Type
// "self" in another object is not correct, this make it independent
$argValueType = $this->correctSelfType($argValueType);

$type = $this->nodeTypeResolver->getType($arg->value);
if (! $type->equals($argValueType) && $this->typeComparator->isSubtype($type, $argValueType)) {
return $type;
}

if (! $argValueType instanceof ObjectType) {
return $argValueType;
}
Expand All @@ -69,11 +74,6 @@ private function resolveStrictArgValueType(Arg $arg): Type
return new MixedType();
}

$type = $this->nodeTypeResolver->getType($arg->value);
if (! $type->equals($argValueType) && $this->typeComparator->isSubtype($type, $argValueType)) {
return $type;
}

return $argValueType;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\NodeTypeResolver\NodeTypeCorrector;

use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;

final class AccessoryNonEmptyArrayTypeCorrector
{
public function correct(Type $mainType): Type
{
if (! $mainType instanceof IntersectionType) {
return $mainType;
}

if (! $mainType->isArray()->yes()) {
return $mainType;
}

foreach ($mainType->getTypes() as $type) {
if ($type instanceof NonEmptyArrayType) {
return new ArrayType(new MixedType(), new MixedType());
}
}

return $mainType;
}
}
19 changes: 13 additions & 6 deletions src/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use Rector\NodeTypeResolver\Contract\NodeTypeResolverAwareInterface;
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeCorrector\AccessoryNonEmptyArrayTypeCorrector;
use Rector\NodeTypeResolver\NodeTypeCorrector\AccessoryNonEmptyStringTypeCorrector;
use Rector\NodeTypeResolver\NodeTypeCorrector\GenericClassStringTypeCorrector;
use Rector\NodeTypeResolver\PHPStan\ObjectWithoutClassTypeWithParentTypes;
Expand Down Expand Up @@ -79,6 +80,7 @@ public function __construct(
private readonly GenericClassStringTypeCorrector $genericClassStringTypeCorrector,
private readonly ReflectionProvider $reflectionProvider,
private readonly AccessoryNonEmptyStringTypeCorrector $accessoryNonEmptyStringTypeCorrector,
private readonly AccessoryNonEmptyArrayTypeCorrector $accessoryNonEmptyArrayTypeCorrector,
private readonly RenamedClassesDataCollector $renamedClassesDataCollector,
private readonly NodeNameResolver $nodeNameResolver,
iterable $nodeTypeResolvers
Expand Down Expand Up @@ -195,8 +197,7 @@ public function getType(Node $node): Type
$type = $this->resolveByNodeTypeResolvers($node);

if ($type instanceof Type) {
$type = $this->accessoryNonEmptyStringTypeCorrector->correct($type);
$type = $this->genericClassStringTypeCorrector->correct($type);
$type = $this->correctType($type);

if ($type instanceof ObjectType) {
$scope = $node->getAttribute(AttributeKey::SCOPE);
Expand Down Expand Up @@ -231,9 +232,7 @@ public function getType(Node $node): Type
return new MixedType();
}

$type = $scope->getType($node);
$type = $this->accessoryNonEmptyStringTypeCorrector->correct($type);
$type = $this->genericClassStringTypeCorrector->correct($type);
$type = $this->correctType($scope->getType($node));

// hot fix for phpstan not resolving chain method calls
if (! $node instanceof MethodCall) {
Expand All @@ -256,6 +255,14 @@ public function isNullableType(Node $node): bool
return TypeCombinator::containsNull($nodeType);
}

private function correctType(Type $type): Type
{
$type = $this->accessoryNonEmptyStringTypeCorrector->correct($type);
$type = $this->genericClassStringTypeCorrector->correct($type);

return $this->accessoryNonEmptyArrayTypeCorrector->correct($type);
}

public function getNativeType(Expr $expr): Type
{
$scope = $expr->getAttribute(AttributeKey::SCOPE);
Expand All @@ -281,7 +288,7 @@ public function getNativeType(Expr $expr): Type
return new ObjectWithoutClassType();
}

return $this->accessoryNonEmptyStringTypeCorrector->correct($type);
return $this->correctType($type);
}

return $this->resolveNativeUnionType($type);
Expand Down
Loading