Skip to content

Commit bc09230

Browse files
committed
Report bool properties as too wide when only used with true/false
1 parent 5a50985 commit bc09230

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

src/Php/PhpVersions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@ public function supportsNamedArgumentAfterUnpackedArgument(): TrinaryLogic
4343
return IntegerRangeType::fromInterval(80100, null)->isSuperTypeOf($this->phpVersions)->result;
4444
}
4545

46+
public function supportsTrueAndFalseStandaloneType(): TrinaryLogic
47+
{
48+
return IntegerRangeType::fromInterval(80200, null)->isSuperTypeOf($this->phpVersions)->result;
49+
}
50+
4651
}

tests/PHPStan/Rules/Comparison/IfConstantConditionRuleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,15 @@ public function testBug8926(): void
182182
$this->analyse([__DIR__ . '/data/bug-8926.php'], []);
183183
}
184184

185+
public function testBug13384b(): void
186+
{
187+
$this->treatPhpDocTypesAsCertain = true;
188+
$this->analyse([__DIR__ . '/../TooWideTypehints/data/bug-13384b.php'], [
189+
[
190+
'If condition is always false.',
191+
23,
192+
],
193+
]);
194+
}
195+
185196
}

tests/PHPStan/Rules/TooWideTypehints/TooWidePropertyTypeRuleTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,30 @@ public function testBug11667(): void
5959
$this->analyse([__DIR__ . '/data/bug-11667.php'], []);
6060
}
6161

62+
#[RequiresPhp('>= 8.2')]
63+
public function testBug13384(): void
64+
{
65+
$this->analyse([__DIR__ . '/data/bug-13384.php'], [
66+
[
67+
'Static property Bug13384\ShutdownHandlerFalseDefault::$registered (bool) is never assigned true so it can be removed from the property type.',
68+
9,
69+
],
70+
[
71+
'Static property Bug13384\ShutdownHandlerTrueDefault::$registered (bool) is never assigned false so it can be removed from the property type.',
72+
34,
73+
],
74+
]);
75+
}
76+
77+
#[RequiresPhp('< 8.2')]
78+
public function testBug13384NoStandaloneTrueFalse(): void
79+
{
80+
$this->analyse([__DIR__ . '/data/bug-13384.php'], []);
81+
}
82+
83+
public function testBug13384b(): void
84+
{
85+
$this->analyse([__DIR__ . '/data/bug-13384b.php'], []);
86+
}
87+
6288
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug13384;
4+
5+
use function register_shutdown_function;
6+
7+
final class ShutdownHandlerFalseDefault
8+
{
9+
private static bool $registered = false;
10+
private static string $message = '';
11+
12+
public static function setMessage(string $message): void
13+
{
14+
self::register();
15+
16+
self::$message = $message;
17+
}
18+
19+
private static function register(): void
20+
{
21+
if (self::$registered) {
22+
return;
23+
}
24+
25+
register_shutdown_function(static function (): void
26+
{
27+
print self::$message;
28+
});
29+
}
30+
}
31+
32+
final class ShutdownHandlerTrueDefault
33+
{
34+
private static bool $registered = true;
35+
private static string $message = '';
36+
37+
public static function setMessage(string $message): void
38+
{
39+
self::register();
40+
41+
self::$message = $message;
42+
}
43+
44+
private static function register(): void
45+
{
46+
if (self::$registered) {
47+
return;
48+
}
49+
50+
register_shutdown_function(static function (): void
51+
{
52+
print self::$message;
53+
});
54+
}
55+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php // lint >= 8.2
2+
3+
declare(strict_types=1);
4+
5+
namespace Bug13384b;
6+
7+
use function register_shutdown_function;
8+
9+
final class ShutdownHandlerFooBar
10+
{
11+
private static false $registered = false;
12+
private static string $message = '';
13+
14+
public static function setMessage(string $message): void
15+
{
16+
self::register();
17+
18+
self::$message = $message;
19+
}
20+
21+
private static function register(): void
22+
{
23+
if (self::$registered) {
24+
return;
25+
}
26+
27+
register_shutdown_function(static function (): void
28+
{
29+
print self::$message;
30+
});
31+
}
32+
}

0 commit comments

Comments
 (0)