Skip to content

Commit 6a56c6f

Browse files
committed
fix bools
1 parent fd8aad2 commit 6a56c6f

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

src/Type/BooleanType.php

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

114114
public function toCoercedArgumentType(bool $strictTypes): Type
115115
{
116+
if (!$strictTypes) {
117+
return TypeCombinator::union(new IntegerType(), new FloatType(), new StringType(), new BooleanType());
118+
}
119+
116120
return $this;
117121
}
118122

src/Type/Constant/ConstantBooleanType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
use PHPStan\TrinaryLogic;
99
use PHPStan\Type\BooleanType;
1010
use PHPStan\Type\ConstantScalarType;
11+
use PHPStan\Type\FloatType;
1112
use PHPStan\Type\GeneralizePrecision;
13+
use PHPStan\Type\IntegerType;
1214
use PHPStan\Type\MixedType;
1315
use PHPStan\Type\NeverType;
1416
use PHPStan\Type\StaticTypeFactory;
17+
use PHPStan\Type\StringType;
1518
use PHPStan\Type\Traits\ConstantScalarTypeTrait;
1619
use PHPStan\Type\Type;
20+
use PHPStan\Type\TypeCombinator;
1721
use PHPStan\Type\VerbosityLevel;
1822

1923
/** @api */
@@ -109,6 +113,10 @@ public function toArrayKey(): Type
109113

110114
public function toCoercedArgumentType(bool $strictTypes): Type
111115
{
116+
if (!$strictTypes) {
117+
return TypeCombinator::union(new IntegerType(), new FloatType(), new StringType(), new BooleanType());
118+
}
119+
112120
return $this;
113121
}
114122

src/Type/Constant/ConstantIntegerType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
66
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
77
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
8+
use PHPStan\Type\BooleanType;
89
use PHPStan\Type\CompoundType;
910
use PHPStan\Type\ConstantScalarType;
1011
use PHPStan\Type\FloatType;
1112
use PHPStan\Type\GeneralizePrecision;
1213
use PHPStan\Type\IntegerRangeType;
1314
use PHPStan\Type\IntegerType;
1415
use PHPStan\Type\IsSuperTypeOfResult;
16+
use PHPStan\Type\StringType;
1517
use PHPStan\Type\Traits\ConstantNumericComparisonTypeTrait;
1618
use PHPStan\Type\Traits\ConstantScalarTypeTrait;
1719
use PHPStan\Type\Type;
@@ -97,8 +99,9 @@ public function toArrayKey(): Type
9799
public function toCoercedArgumentType(bool $strictTypes): Type
98100
{
99101
if (!$strictTypes) {
100-
return TypeCombinator::union($this, new FloatType());
102+
return TypeCombinator::union(new IntegerType(), new FloatType(), new StringType(), new BooleanType());
101103
}
104+
102105
return TypeCombinator::union($this, $this->toFloat());
103106
}
104107

src/Type/IntegerType.php

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

101101
public function toCoercedArgumentType(bool $strictTypes): Type
102102
{
103+
if (!$strictTypes) {
104+
return TypeCombinator::union(new IntegerType(), new FloatType(), new StringType(), new BooleanType());
105+
}
106+
103107
return TypeCombinator::union($this, $this->toFloat());
104108
}
105109

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,57 @@ public function doBar(): void
161161
assertType('int', $this->foo);
162162
}
163163
}
164+
165+
class FooBool
166+
{
167+
168+
public int $foo;
169+
170+
public function doFoo(bool $b): void
171+
{
172+
$this->foo = $b;
173+
assertType('int', $this->foo);
174+
}
175+
176+
public function doBar(): void
177+
{
178+
$this->foo = true;
179+
assertType('int', $this->foo);
180+
}
181+
}
182+
183+
class FooBoolString
184+
{
185+
186+
public string $foo;
187+
188+
public function doFoo(bool $b): void
189+
{
190+
$this->foo = $b;
191+
assertType('string', $this->foo);
192+
}
193+
194+
public function doBar(): void
195+
{
196+
$this->foo = true;
197+
assertType('string', $this->foo);
198+
}
199+
}
200+
201+
class FooIntString
202+
{
203+
204+
public string $foo;
205+
206+
public function doFoo(int $b): void
207+
{
208+
$this->foo = $b;
209+
assertType('string', $this->foo); // could be numeric-string
210+
}
211+
212+
public function doBar(): void
213+
{
214+
$this->foo = 1;
215+
assertType('string', $this->foo); // could be numeric-string
216+
}
217+
}

0 commit comments

Comments
 (0)