Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Nov 26, 2024
1 parent 561423d commit 3f0c076
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Rector\StaticTypeMapper\Mapper\ScalarStringToTypeMapper;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Rector\TypeDeclaration\TypeInferer\AssignToPropertyTypeInferer;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\AllAssignNodePropertyTypeInferer;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
Expand All @@ -36,11 +37,6 @@
*/
final class TypedPropertyFromJMSSerializerAttributeTypeRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @var string
*/
private const JMS_TYPE = 'JMS\Serializer\Annotation\Type';

public function __construct(
private readonly AllAssignNodePropertyTypeInferer $allAssignNodePropertyTypeInferer,
private readonly MakePropertyTypedGuard $makePropertyTypedGuard,
Expand Down Expand Up @@ -107,7 +103,7 @@ public function refactor(Node $node): ?Node
continue;
}

if (! $this->phpAttributeAnalyzer->hasPhpAttribute($property, self::JMS_TYPE)) {
if (! $this->phpAttributeAnalyzer->hasPhpAttribute($property, AssignToPropertyTypeInferer::JMS_TYPE)) {
continue;
}

Expand Down Expand Up @@ -140,7 +136,7 @@ public function refactor(Node $node): ?Node
$typeValue = null;
foreach ($property->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() === self::JMS_TYPE) {
if ($attr->name->toString() === AssignToPropertyTypeInferer::JMS_TYPE) {
$typeValue = $this->valueResolver->getValue($attr->args[0]->value);
break;
}
Expand Down
18 changes: 15 additions & 3 deletions rules/TypeDeclaration/TypeInferer/AssignToPropertyTypeInferer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
Expand All @@ -34,6 +35,11 @@
*/
final readonly class AssignToPropertyTypeInferer
{
/**
* @var string
*/
public const JMS_TYPE = 'JMS\Serializer\Annotation\Type';

public function __construct(
private ConstructorAssignDetector $constructorAssignDetector,
private PropertyAssignMatcher $propertyAssignMatcher,
Expand All @@ -45,6 +51,7 @@ public function __construct(
private ExprAnalyzer $exprAnalyzer,
private ValueResolver $valueResolver,
private PropertyFetchAnalyzer $propertyFetchAnalyzer,
private PhpAttributeAnalyzer $phpAttributeAnalyzer
) {
}

Expand All @@ -54,7 +61,7 @@ public function inferPropertyInClassLike(Property $property, string $propertyNam
return null;
}

$assignedExprTypes = $this->getAssignedExprTypes($classLike, $propertyName);
$assignedExprTypes = $this->getAssignedExprTypes($classLike, $property, $propertyName);

if ($this->shouldAddNullType($classLike, $propertyName, $assignedExprTypes)) {
$assignedExprTypes[] = new NullType();
Expand Down Expand Up @@ -180,13 +187,15 @@ private function hasAssignDynamicPropertyValue(ClassLike $classLike, string $pro
/**
* @return array<Type>
*/
private function getAssignedExprTypes(ClassLike $classLike, string $propertyName): array
private function getAssignedExprTypes(ClassLike $classLike, Property $property, string $propertyName): array
{
$assignedExprTypes = [];

$hasJmsType = $this->phpAttributeAnalyzer->hasPhpAttribute($property, self::JMS_TYPE);
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($classLike->stmts, function (Node $node) use (
$propertyName,
&$assignedExprTypes,
$hasJmsType
): ?int {
if (! $node instanceof Assign) {
return null;
Expand All @@ -198,7 +207,10 @@ private function getAssignedExprTypes(ClassLike $classLike, string $propertyName
}

if ($this->exprAnalyzer->isNonTypedFromParam($node->expr)) {
$assignedExprTypes[] = new MixedType();
$assignedExprTypes = $hasJmsType
? []
: [new MixedType()];

return NodeVisitor::STOP_TRAVERSAL;
}

Expand Down

0 comments on commit 3f0c076

Please sign in to comment.