Skip to content

Commit bed30a7

Browse files
committed
Fix issues about assigning typed properties
1 parent b6b8ebd commit bed30a7

37 files changed

+235
-17
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5556,15 +5556,10 @@ static function (): void {
55565556
$assignedExprType = $scope->getType($assignedExpr);
55575557
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
55585558
if ($propertyReflection->canChangeTypeAfterAssignment()) {
5559-
if ($propertyReflection->hasNativeType()) {
5559+
if ($propertyReflection->hasNativeType() && $scope->isDeclareStrictTypes()) {
55605560
$propertyNativeType = $propertyReflection->getNativeType();
5561-
if ($propertyNativeType->isSuperTypeOf($assignedExprType)->yes()) {
5562-
$assignedExprNativeType = $scope->getNativeType($assignedExpr);
5563-
if (!$propertyNativeType->isSuperTypeOf($assignedExprNativeType)->yes()) {
5564-
$assignedExprNativeType = $propertyNativeType;
5565-
}
5566-
$scope = $scope->assignExpression($var, $assignedExprType, $assignedExprNativeType);
5567-
}
5561+
5562+
$scope = $scope->assignExpression($var, TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType), TypeCombinator::intersect($scope->getNativeType($assignedExpr)->toCoercedArgumentType(true), $propertyNativeType));
55685563
} else {
55695564
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
55705565
}
@@ -5632,15 +5627,10 @@ static function (): void {
56325627
$assignedExprType = $scope->getType($assignedExpr);
56335628
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
56345629
if ($propertyReflection !== null && $propertyReflection->canChangeTypeAfterAssignment()) {
5635-
if ($propertyReflection->hasNativeType()) {
5630+
if ($propertyReflection->hasNativeType() && $scope->isDeclareStrictTypes()) {
56365631
$propertyNativeType = $propertyReflection->getNativeType();
5637-
if ($propertyNativeType->isSuperTypeOf($assignedExprType)->yes()) {
5638-
$assignedExprNativeType = $scope->getNativeType($assignedExpr);
5639-
if (!$propertyNativeType->isSuperTypeOf($assignedExprNativeType)->yes()) {
5640-
$assignedExprNativeType = $propertyNativeType;
5641-
}
5642-
$scope = $scope->assignExpression($var, $assignedExprType, $assignedExprNativeType);
5643-
}
5632+
5633+
$scope = $scope->assignExpression($var, TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType), TypeCombinator::intersect($scope->getNativeType($assignedExpr)->toCoercedArgumentType(true), $propertyNativeType));
56445634
} else {
56455635
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
56465636
}

src/Type/Accessory/AccessoryArrayListType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@ public function toArrayKey(): Type
469469
return new ErrorType();
470470
}
471471

472+
public function toCoercedArgumentType(bool $strictTypes): Type
473+
{
474+
return $this;
475+
}
476+
472477
public function traverse(callable $cb): Type
473478
{
474479
return $this;

src/Type/Accessory/AccessoryLiteralStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ public function toArrayKey(): Type
213213
return $this;
214214
}
215215

216+
public function toCoercedArgumentType(bool $strictTypes): Type
217+
{
218+
return $this;
219+
}
220+
216221
public function isNull(): TrinaryLogic
217222
{
218223
return TrinaryLogic::createNo();

src/Type/Accessory/AccessoryLowercaseStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ public function toArrayKey(): Type
210210
return $this;
211211
}
212212

213+
public function toCoercedArgumentType(bool $strictTypes): Type
214+
{
215+
return $this;
216+
}
217+
213218
public function isNull(): TrinaryLogic
214219
{
215220
return TrinaryLogic::createNo();

src/Type/Accessory/AccessoryNonEmptyStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ public function toArrayKey(): Type
211211
return $this;
212212
}
213213

214+
public function toCoercedArgumentType(bool $strictTypes): Type
215+
{
216+
return $this;
217+
}
218+
214219
public function isNull(): TrinaryLogic
215220
{
216221
return TrinaryLogic::createNo();

src/Type/Accessory/AccessoryNonFalsyStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ public function toArrayKey(): Type
212212
return $this;
213213
}
214214

215+
public function toCoercedArgumentType(bool $strictTypes): Type
216+
{
217+
return $this;
218+
}
219+
215220
public function isNull(): TrinaryLogic
216221
{
217222
return TrinaryLogic::createNo();

src/Type/Accessory/AccessoryNumericStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ public function toArrayKey(): Type
213213
return new IntegerType();
214214
}
215215

216+
public function toCoercedArgumentType(bool $strictTypes): Type
217+
{
218+
return $this;
219+
}
220+
216221
public function isNull(): TrinaryLogic
217222
{
218223
return TrinaryLogic::createNo();

src/Type/Accessory/AccessoryUppercaseStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ public function toArrayKey(): Type
210210
return $this;
211211
}
212212

213+
public function toCoercedArgumentType(bool $strictTypes): Type
214+
{
215+
return $this;
216+
}
217+
213218
public function isNull(): TrinaryLogic
214219
{
215220
return TrinaryLogic::createNo();

src/Type/Accessory/HasOffsetType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ public function toArrayKey(): Type
388388
return new ErrorType();
389389
}
390390

391+
public function toCoercedArgumentType(bool $strictTypes): Type
392+
{
393+
return $this;
394+
}
395+
391396
public function getEnumCases(): array
392397
{
393398
return [];

src/Type/Accessory/HasOffsetValueType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ public function toArrayKey(): Type
438438
return new ErrorType();
439439
}
440440

441+
public function toCoercedArgumentType(bool $strictTypes): Type
442+
{
443+
return $this;
444+
}
445+
441446
public function getEnumCases(): array
442447
{
443448
return [];

0 commit comments

Comments
 (0)