Skip to content

Commit

Permalink
[TypeDeclaration] Clean up get ClassReflection on loop on TypedProper…
Browse files Browse the repository at this point in the history
…tyFromCreateMockAssignRector (#6181)

* [TypeDeclaration] Clean up get ClassReflection on loop on rules

* clean up unnecessary ClassReflection

* clean up

* unrelated change

* clean up only on TypedPropertyFromCreateMockAssignRector

* add test for assign with non-mock object type
  • Loading branch information
samsonasik authored Jul 24, 2024
1 parent fa84ab5 commit 1aef755
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector\Fixture;

use PHPUnit\Framework\TestCase;

class SkipNonMockObjectType extends TestCase
{
public $someProperty;

protected function setUp(): void
{
$this->someProperty = 'some property value';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
use PhpParser\Node;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\AllAssignNodePropertyTypeInferer;
use Rector\TypeDeclaration\TypeInferer\AssignToPropertyTypeInferer;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -37,8 +35,7 @@ final class TypedPropertyFromCreateMockAssignRector extends AbstractRector imple
private const MOCK_OBJECT_CLASS = 'PHPUnit\Framework\MockObject\MockObject';

public function __construct(
private readonly ReflectionResolver $reflectionResolver,
private readonly AllAssignNodePropertyTypeInferer $allAssignNodePropertyTypeInferer,
private readonly AssignToPropertyTypeInferer $assignToPropertyTypeInferer,
private readonly StaticTypeMapper $staticTypeMapper,
private readonly ConstructorAssignDetector $constructorAssignDetector
) {
Expand Down Expand Up @@ -93,7 +90,6 @@ public function refactor(Node $node): ?Node
return null;
}

$classReflection = null;
$hasChanged = false;

foreach ($node->getProperties() as $property) {
Expand All @@ -106,19 +102,11 @@ public function refactor(Node $node): ?Node
continue;
}

if (! $classReflection instanceof ClassReflection) {
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
}

// ClassReflection not detected, early skip
if (! $classReflection instanceof ClassReflection) {
return null;
}

$type = $this->allAssignNodePropertyTypeInferer->inferProperty(
$propertyName = (string) $this->getName($property);
$type = $this->assignToPropertyTypeInferer->inferPropertyInClassLike(
$property,
$classReflection,
$this->file
$propertyName,
$node
);

if (! $type instanceof Type) {
Expand All @@ -134,7 +122,6 @@ public function refactor(Node $node): ?Node
continue;
}

$propertyName = (string) $this->getName($property);
if (! $this->constructorAssignDetector->isPropertyAssigned($node, $propertyName)) {
if (! $propertyType instanceof NullableType) {
continue;
Expand Down

0 comments on commit 1aef755

Please sign in to comment.