Skip to content

Commit b0c1b76

Browse files
committed
specific tooWideBool error message
1 parent 7816a6b commit b0c1b76

File tree

5 files changed

+54
-23
lines changed

5 files changed

+54
-23
lines changed

src/Rules/TooWideTypehints/TooWideTypeCheck.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Node\MethodReturnStatementsNode;
1111
use PHPStan\Rules\IdentifierRuleError;
1212
use PHPStan\Rules\RuleErrorBuilder;
13+
use PHPStan\Type\Constant\ConstantBooleanType;
1314
use PHPStan\Type\Type;
1415
use PHPStan\Type\TypeCombinator;
1516
use PHPStan\Type\TypehintHelper;
@@ -54,6 +55,23 @@ public function checkProperty(
5455
continue;
5556
}
5657

58+
if ($propertyType->isBoolean()->yes()) {
59+
$suggestedType = $type->isTrue()->yes() ? new ConstantBooleanType(false) : new ConstantBooleanType(true);
60+
61+
$errors[] = RuleErrorBuilder::message(sprintf(
62+
'%s (%s) is never assigned %s so the property type can be changed to %s.',
63+
$propertyDescription,
64+
$propertyType->describe($verbosityLevel),
65+
$type->describe($verbosityLevel),
66+
$suggestedType->describe($verbosityLevel),
67+
))
68+
->identifier('property.tooWideBool')
69+
->line($property->getStartLine())
70+
->build();
71+
72+
continue;
73+
}
74+
5775
$errors[] = RuleErrorBuilder::message(sprintf(
5876
'%s (%s) is never assigned %s so it can be removed from the property type.',
5977
$propertyDescription,
@@ -144,6 +162,19 @@ public function checkFunction(
144162
}
145163
}
146164

165+
if ($functionReturnType->isBoolean()->yes()) {
166+
$suggestedType = $type->isTrue()->yes() ? new ConstantBooleanType(false) : new ConstantBooleanType(true);
167+
168+
$messages[] = RuleErrorBuilder::message(sprintf(
169+
'%s never returns %s so the return type can be changed to %s.',
170+
$functionDescription,
171+
$type->describe(VerbosityLevel::getRecommendedLevelByType($type)),
172+
$suggestedType->describe(VerbosityLevel::getRecommendedLevelByType($suggestedType)),
173+
))->identifier('return.tooWideBool')->build();
174+
175+
continue;
176+
}
177+
147178
$messages[] = RuleErrorBuilder::message(sprintf(
148179
'%s never returns %s so it can be removed from the return type.',
149180
$functionDescription,

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ public function testBug4734(): void
443443
$errors = $this->runAnalyse(__DIR__ . '/data/bug-4734.php');
444444
$this->assertCount(5, $errors); // could be 3
445445

446-
$this->assertSame('Static property Bug4734\Foo::$httpMethodParameterOverride (bool) is never assigned false so it can be removed from the property type.', $errors[0]->getMessage()); // should not error
447-
$this->assertSame('Property Bug4734\Foo::$httpMethodParameterOverride2 (bool) is never assigned false so it can be removed from the property type.', $errors[1]->getMessage()); // should not error
446+
$this->assertSame('Static property Bug4734\Foo::$httpMethodParameterOverride (bool) is never assigned false so the property type can be changed to true.', $errors[0]->getMessage()); // should not error
447+
$this->assertSame('Property Bug4734\Foo::$httpMethodParameterOverride2 (bool) is never assigned false so the property type can be changed to true.', $errors[1]->getMessage()); // should not error
448448
$this->assertSame('Unsafe access to private property Bug4734\Foo::$httpMethodParameterOverride through static::.', $errors[2]->getMessage());
449449
$this->assertSame('Access to an undefined static property static(Bug4734\Foo)::$httpMethodParameterOverride3.', $errors[3]->getMessage());
450450
$this->assertSame('Access to an undefined property Bug4734\Foo::$httpMethodParameterOverride4.', $errors[4]->getMessage());
@@ -1100,13 +1100,13 @@ public function testAssertDocblock(): void
11001100
{
11011101
$errors = $this->runAnalyse(__DIR__ . '/nsrt/assert-docblock.php');
11021102
$this->assertCount(8, $errors);
1103-
$this->assertSame('Function AssertDocblock\validateStringArrayIfTrue() never returns false so it can be removed from the return type.', $errors[0]->getMessage());
1103+
$this->assertSame('Function AssertDocblock\validateStringArrayIfTrue() never returns false so the return type can be changed to true.', $errors[0]->getMessage());
11041104
$this->assertSame(17, $errors[0]->getLine());
1105-
$this->assertSame('Function AssertDocblock\validateStringArrayIfFalse() never returns true so it can be removed from the return type.', $errors[1]->getMessage());
1105+
$this->assertSame('Function AssertDocblock\validateStringArrayIfFalse() never returns true so the return type can be changed to false.', $errors[1]->getMessage());
11061106
$this->assertSame(25, $errors[1]->getLine());
1107-
$this->assertSame('Function AssertDocblock\validateStringOrIntArray() never returns true so it can be removed from the return type.', $errors[2]->getMessage());
1107+
$this->assertSame('Function AssertDocblock\validateStringOrIntArray() never returns true so the return type can be changed to false.', $errors[2]->getMessage());
11081108
$this->assertSame(34, $errors[2]->getLine());
1109-
$this->assertSame('Function AssertDocblock\validateStringOrNonEmptyIntArray() never returns true so it can be removed from the return type.', $errors[3]->getMessage());
1109+
$this->assertSame('Function AssertDocblock\validateStringOrNonEmptyIntArray() never returns true so the return type can be changed to false.', $errors[3]->getMessage());
11101110
$this->assertSame(44, $errors[3]->getLine());
11111111
$this->assertSame('Call to method AssertDocblock\A::testInt() with string will always evaluate to false.', $errors[4]->getMessage());
11121112
$this->assertSame(218, $errors[4]->getLine());

tests/PHPStan/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRuleTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,19 @@ public function testBug13384cPhp82(): void
7575
$this->reportTooWideBool = true;
7676
$this->analyse([__DIR__ . '/data/bug-13384c.php'], [
7777
[
78-
'Function Bug13384c\doFoo() never returns true so it can be removed from the return type.',
78+
'Function Bug13384c\doFoo() never returns true so the return type can be changed to false.',
7979
5,
8080
],
8181
[
82-
'Function Bug13384c\doFoo2() never returns false so it can be removed from the return type.',
82+
'Function Bug13384c\doFoo2() never returns false so the return type can be changed to true.',
8383
9,
8484
],
8585
[
86-
'Function Bug13384c\doFooPhpdoc() never returns false so it can be removed from the return type.',
86+
'Function Bug13384c\doFooPhpdoc() never returns false so the return type can be changed to true.',
8787
93,
8888
],
8989
[
90-
'Function Bug13384c\doFooPhpdoc2() never returns true so it can be removed from the return type.',
90+
'Function Bug13384c\doFooPhpdoc2() never returns true so the return type can be changed to false.',
9191
100,
9292
],
9393
]);
@@ -99,11 +99,11 @@ public function testBug13384cPrePhp82(): void
9999
$this->reportTooWideBool = true;
100100
$this->analyse([__DIR__ . '/data/bug-13384c.php'], [
101101
[
102-
'Function Bug13384c\doFooPhpdoc() never returns false so it can be removed from the return type.',
102+
'Function Bug13384c\doFooPhpdoc() never returns false so the return type can be changed to true.',
103103
93,
104104
],
105105
[
106-
'Function Bug13384c\doFooPhpdoc2() never returns true so it can be removed from the return type.',
106+
'Function Bug13384c\doFooPhpdoc2() never returns true so the return type can be changed to false.',
107107
100,
108108
],
109109
]);

tests/PHPStan/Rules/TooWideTypehints/TooWideMethodReturnTypehintRuleTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,27 +226,27 @@ public function testBug13384c(): void
226226
$this->reportTooWideBool = true;
227227
$this->analyse([__DIR__ . '/data/bug-13384c.php'], [
228228
[
229-
'Method Bug13384c\Bug13384c::doBar() never returns true so it can be removed from the return type.',
229+
'Method Bug13384c\Bug13384c::doBar() never returns true so the return type can be changed to false.',
230230
33,
231231
],
232232
[
233-
'Method Bug13384c\Bug13384c::doBar2() never returns false so it can be removed from the return type.',
233+
'Method Bug13384c\Bug13384c::doBar2() never returns false so the return type can be changed to true.',
234234
37,
235235
],
236236
[
237-
'Method Bug13384c\Bug13384c::doBarPhpdoc() never returns false so it can be removed from the return type.',
237+
'Method Bug13384c\Bug13384c::doBarPhpdoc() never returns false so the return type can be changed to true.',
238238
55,
239239
],
240240
[
241-
'Method Bug13384c\Bug13384Static::doBar() never returns true so it can be removed from the return type.',
241+
'Method Bug13384c\Bug13384Static::doBar() never returns true so the return type can be changed to false.',
242242
62,
243243
],
244244
[
245-
'Method Bug13384c\Bug13384Static::doBar2() never returns false so it can be removed from the return type.',
245+
'Method Bug13384c\Bug13384Static::doBar2() never returns false so the return type can be changed to true.',
246246
66,
247247
],
248248
[
249-
'Method Bug13384c\Bug13384Static::doBarPhpdoc() never returns false so it can be removed from the return type.',
249+
'Method Bug13384c\Bug13384Static::doBarPhpdoc() never returns false so the return type can be changed to true.',
250250
84,
251251
],
252252
]);
@@ -258,11 +258,11 @@ public function testBug13384cPrePhp82(): void
258258
$this->reportTooWideBool = true;
259259
$this->analyse([__DIR__ . '/data/bug-13384c.php'], [
260260
[
261-
'Method Bug13384c\Bug13384c::doBarPhpdoc() never returns false so it can be removed from the return type.',
261+
'Method Bug13384c\Bug13384c::doBarPhpdoc() never returns false so the return type can be changed to true.',
262262
55,
263263
],
264264
[
265-
'Method Bug13384c\Bug13384Static::doBarPhpdoc() never returns false so it can be removed from the return type.',
265+
'Method Bug13384c\Bug13384Static::doBarPhpdoc() never returns false so the return type can be changed to true.',
266266
84,
267267
],
268268
]);

tests/PHPStan/Rules/TooWideTypehints/TooWidePropertyTypeRuleTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ public function testBug13384(): void
6767
$this->reportTooWideBool = true;
6868
$this->analyse([__DIR__ . '/data/bug-13384.php'], [
6969
[
70-
'Static property Bug13384\ShutdownHandlerFalseDefault::$registered (bool) is never assigned true so it can be removed from the property type.',
70+
'Static property Bug13384\ShutdownHandlerFalseDefault::$registered (bool) is never assigned true so the property type can be changed to false.',
7171
9,
7272
],
7373
[
74-
'Static property Bug13384\ShutdownHandlerTrueDefault::$registered (bool) is never assigned false so it can be removed from the property type.',
74+
'Static property Bug13384\ShutdownHandlerTrueDefault::$registered (bool) is never assigned false so the property type can be changed to true.',
7575
34,
7676
],
7777
]);
@@ -89,7 +89,7 @@ public function testBug13384Phpdoc(): void
8989
$this->reportTooWideBool = true;
9090
$this->analyse([__DIR__ . '/data/bug-13384-phpdoc.php'], [
9191
[
92-
'Static property Bug13384Phpdoc\ShutdownHandlerPhpdocTypes::$registered (bool) is never assigned true so it can be removed from the property type.',
92+
'Static property Bug13384Phpdoc\ShutdownHandlerPhpdocTypes::$registered (bool) is never assigned true so the property type can be changed to false.',
9393
12,
9494
],
9595
]);

0 commit comments

Comments
 (0)