Skip to content

Commit a243baa

Browse files
committed
fix bools
1 parent fd8aad2 commit a243baa

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

src/Type/BooleanType.php

+4
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

+8
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

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public function toCoercedArgumentType(bool $strictTypes): Type
9999
if (!$strictTypes) {
100100
return TypeCombinator::union($this, new FloatType());
101101
}
102+
102103
return TypeCombinator::union($this, $this->toFloat());
103104
}
104105

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

+36
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,39 @@ 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+
}

0 commit comments

Comments
 (0)