diff --git a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php index 618416f76e0..88fcbc9c017 100644 --- a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php +++ b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php @@ -4,8 +4,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; @@ -13,8 +13,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,6 +39,11 @@ final class TypedPropertyFromCreateMockAssignRector extends AbstractRector imple * @var \Rector\StaticTypeMapper\StaticTypeMapper */ private $staticTypeMapper; + /** + * @readonly + * @var \Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector + */ + private $constructorAssignDetector; /** * @var string */ @@ -47,11 +52,12 @@ final class TypedPropertyFromCreateMockAssignRector extends AbstractRector imple * @var string */ private const MOCK_OBJECT_CLASS = 'PHPUnit\\Framework\\MockObject\\MockObject'; - public function __construct(ReflectionResolver $reflectionResolver, AllAssignNodePropertyTypeInferer $allAssignNodePropertyTypeInferer, StaticTypeMapper $staticTypeMapper) + public function __construct(ReflectionResolver $reflectionResolver, AllAssignNodePropertyTypeInferer $allAssignNodePropertyTypeInferer, StaticTypeMapper $staticTypeMapper, ConstructorAssignDetector $constructorAssignDetector) { $this->reflectionResolver = $reflectionResolver; $this->allAssignNodePropertyTypeInferer = $allAssignNodePropertyTypeInferer; $this->staticTypeMapper = $staticTypeMapper; + $this->constructorAssignDetector = $constructorAssignDetector; } public function getRuleDefinition() : RuleDefinition { @@ -102,8 +108,7 @@ public function refactor(Node $node) : ?Node if ($property->type instanceof Node) { continue; } - $setUpClassMethod = $node->getMethod(MethodName::SET_UP); - if (!$setUpClassMethod instanceof ClassMethod) { + if (\count($property->props) !== 1) { continue; } if (!$classReflection instanceof ClassReflection) { @@ -124,6 +129,13 @@ public function refactor(Node $node) : ?Node if (!$this->isObjectType($propertyType, new ObjectType(self::MOCK_OBJECT_CLASS))) { 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; } diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 6b5719eb47c..dbae12ff686 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '1a58300b7d6ccaa1adbb1760b4a355263dc41408'; + public const PACKAGE_VERSION = 'eba615375fc6b795827e4ef3135d88c58479719d'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-07-24 04:50:32'; + public const RELEASE_DATE = '2024-07-24 06:00:39'; /** * @var int */