From eba615375fc6b795827e4ef3135d88c58479719d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 24 Jul 2024 06:00:39 +0700 Subject: [PATCH] [TypeDeclaration] Allow defined in non-setUp() on TypedPropertyFromCreateMockAssignRector (#6180) * [TypeDeclaration] Skip defined in non-setUp() method on TypedPropertyFromCreateMockAssignRector * nullable if setUp() method exists, but not assign * allow non-setup * Fix * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action --- ...setup_method_exists_but_not_assign.php.inc | 45 +++++++++++++++++++ .../Fixture/with_non_setup_method.php.inc | 37 +++++++++++++++ ...ypedPropertyFromCreateMockAssignRector.php | 19 +++++--- 3 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/Fixture/setup_method_exists_but_not_assign.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/Fixture/with_non_setup_method.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/Fixture/setup_method_exists_but_not_assign.php.inc b/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/Fixture/setup_method_exists_but_not_assign.php.inc new file mode 100644 index 0000000000..ffa74b4f68 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/Fixture/setup_method_exists_but_not_assign.php.inc @@ -0,0 +1,45 @@ +someMock = $this->createMock(SomeMockedClass::class); + } +} + +?> +----- +someMock = $this->createMock(SomeMockedClass::class); + } +} + +?> diff --git a/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/Fixture/with_non_setup_method.php.inc b/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/Fixture/with_non_setup_method.php.inc new file mode 100644 index 0000000000..4574cd5c31 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector/Fixture/with_non_setup_method.php.inc @@ -0,0 +1,37 @@ +someMock = $this->createMock(SomeMockedClass::class); + } +} + +?> +----- +someMock = $this->createMock(SomeMockedClass::class); + } +} + +?> \ No newline at end of file diff --git a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php index 65d7013c7b..367ae06c7f 100644 --- a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php +++ b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php @@ -5,8 +5,8 @@ namespace Rector\TypeDeclaration\Rector\Class_; use PhpParser\Node; +use PhpParser\Node\NullableType; use PhpParser\Node\Stmt\Class_; -use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Reflection\ClassReflection; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; @@ -14,8 +14,8 @@ 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\ValueObject\MethodName; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -39,7 +39,8 @@ final class TypedPropertyFromCreateMockAssignRector extends AbstractRector imple public function __construct( private readonly ReflectionResolver $reflectionResolver, private readonly AllAssignNodePropertyTypeInferer $allAssignNodePropertyTypeInferer, - private readonly StaticTypeMapper $staticTypeMapper + private readonly StaticTypeMapper $staticTypeMapper, + private readonly ConstructorAssignDetector $constructorAssignDetector ) { } @@ -101,8 +102,7 @@ public function refactor(Node $node): ?Node continue; } - $setUpClassMethod = $node->getMethod(MethodName::SET_UP); - if (! $setUpClassMethod instanceof ClassMethod) { + if (count($property->props) !== 1) { continue; } @@ -134,6 +134,15 @@ public function refactor(Node $node): ?Node continue; } + $propertyName = (string) $this->getName($property); + if (! $this->constructorAssignDetector->isPropertyAssigned($node, $propertyName)) { + if (! $propertyType instanceof NullableType) { + continue; + } + + $property->props[0]->default = $this->nodeFactory->createNull(); + } + $property->type = $propertyType; $hasChanged = true; }