Skip to content

Commit fd8aad2

Browse files
committed
fix failling tests
1 parent 4f2ce34 commit fd8aad2

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5649,7 +5649,11 @@ static function (): void {
56495649
TypeCombinator::intersect($scope->getNativeType($assignedExpr)->toCoercedArgumentType(true), $propertyNativeType),
56505650
);
56515651
} else {
5652-
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
5652+
$scope = $scope->assignExpression(
5653+
$var,
5654+
TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(false), $propertyNativeType),
5655+
TypeCombinator::intersect($scope->getNativeType($assignedExpr)->toCoercedArgumentType(false), $propertyNativeType),
5656+
);
56535657
}
56545658
} else {
56555659
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
@@ -5740,7 +5744,11 @@ static function (): void {
57405744
TypeCombinator::intersect($scope->getNativeType($assignedExpr)->toCoercedArgumentType(true), $propertyNativeType),
57415745
);
57425746
} else {
5743-
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
5747+
$scope = $scope->assignExpression(
5748+
$var,
5749+
TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(false), $propertyNativeType),
5750+
TypeCombinator::intersect($scope->getNativeType($assignedExpr)->toCoercedArgumentType(false), $propertyNativeType),
5751+
);
57445752
}
57455753
} else {
57465754
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));

src/Type/Constant/ConstantIntegerType.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
88
use PHPStan\Type\CompoundType;
99
use PHPStan\Type\ConstantScalarType;
10+
use PHPStan\Type\FloatType;
1011
use PHPStan\Type\GeneralizePrecision;
1112
use PHPStan\Type\IntegerRangeType;
1213
use PHPStan\Type\IntegerType;
1314
use PHPStan\Type\IsSuperTypeOfResult;
1415
use PHPStan\Type\Traits\ConstantNumericComparisonTypeTrait;
1516
use PHPStan\Type\Traits\ConstantScalarTypeTrait;
1617
use PHPStan\Type\Type;
18+
use PHPStan\Type\TypeCombinator;
1719
use PHPStan\Type\VerbosityLevel;
1820
use function abs;
1921
use function sprintf;
@@ -92,6 +94,14 @@ public function toArrayKey(): Type
9294
return $this;
9395
}
9496

97+
public function toCoercedArgumentType(bool $strictTypes): Type
98+
{
99+
if (!$strictTypes) {
100+
return TypeCombinator::union($this, new FloatType());
101+
}
102+
return TypeCombinator::union($this, $this->toFloat());
103+
}
104+
95105
public function generalize(GeneralizePrecision $precision): Type
96106
{
97107
return new IntegerType();

src/Type/StringType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ public function toArrayKey(): Type
183183

184184
public function toCoercedArgumentType(bool $strictTypes): Type
185185
{
186+
if (!$strictTypes) {
187+
return TypeCombinator::union(new IntegerType(), new FloatType(), $this, new BooleanType());
188+
}
189+
186190
return $this;
187191
}
188192

tests/PHPStan/Analyser/nsrt/bug-12393b.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,21 @@ public function getMixed()
143143
}
144144

145145
}
146+
147+
class Foo
148+
{
149+
150+
public int $foo;
151+
152+
public function doFoo(string $s): void
153+
{
154+
$this->foo = $s;
155+
assertType('int', $this->foo);
156+
}
157+
158+
public function doBar(): void
159+
{
160+
$this->foo = 'foo';
161+
assertType('int', $this->foo);
162+
}
163+
}

0 commit comments

Comments
 (0)