Skip to content

Commit c48c398

Browse files
committed
test methods
1 parent acfb3c0 commit c48c398

File tree

4 files changed

+108
-7
lines changed

4 files changed

+108
-7
lines changed

src/Rules/TooWideTypehints/TooWideTypeCheck.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function checkFunction(
6969
): array
7070
{
7171
$functionReturnType = TypeUtils::resolveLateResolvableTypes($functionReturnType);
72-
if (!$functionReturnType instanceof UnionType) {
72+
if (!$functionReturnType instanceof UnionType && !$functionReturnType->isBoolean()->yes()) {
7373
return [];
7474
}
7575
$statementResult = $node->getStatementResult();
@@ -82,22 +82,22 @@ public function checkFunction(
8282
return [];
8383
}
8484

85-
$returnTypes = [];
85+
$functionReturnTypes = [];
8686
foreach ($returnStatements as $returnStatement) {
8787
$returnNode = $returnStatement->getReturnNode();
8888
if ($returnNode->expr === null) {
89-
$returnTypes[] = new VoidType();
89+
$functionReturnTypes[] = new VoidType();
9090
continue;
9191
}
9292

93-
$returnTypes[] = $returnStatement->getScope()->getType($returnNode->expr);
93+
$functionReturnTypes[] = $returnStatement->getScope()->getType($returnNode->expr);
9494
}
9595

9696
if (!$statementResult->isAlwaysTerminating()) {
97-
$returnTypes[] = new VoidType();
97+
$functionReturnTypes[] = new VoidType();
9898
}
9999

100-
$returnType = TypeCombinator::union(...$returnTypes);
100+
$returnType = TypeCombinator::union(...$functionReturnTypes);
101101

102102
// Do not require to have @return null/true/false in descendant classes
103103
if (
@@ -108,7 +108,8 @@ public function checkFunction(
108108
}
109109

110110
$messages = [];
111-
foreach ($functionReturnType->getTypes() as $type) {
111+
$functionReturnTypes = $functionReturnType instanceof UnionType ? $functionReturnType->getTypes() : $functionReturnType->getFiniteTypes();
112+
foreach ($functionReturnTypes as $type) {
112113
if (!$type->isSuperTypeOf($returnType)->no()) {
113114
continue;
114115
}

tests/PHPStan/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRuleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,18 @@ public function testBug11980(): void
6161
]);
6262
}
6363

64+
public function testBug13384c(): void
65+
{
66+
$this->analyse([__DIR__ . '/data/bug-13384c.php'], [
67+
[
68+
'Function Bug13384c\doFoo() never returns true so it can be removed from the return type.',
69+
5,
70+
],
71+
[
72+
'Function Bug13384c\doFoo2() never returns false so it can be removed from the return type.',
73+
9,
74+
],
75+
]);
76+
}
77+
6478
}

tests/PHPStan/Rules/TooWideTypehints/TooWideMethodReturnTypehintRuleTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,26 @@ public function testBug11980(): void
199199
]);
200200
}
201201

202+
public function testBug13384c(): void
203+
{
204+
$this->analyse([__DIR__ . '/data/bug-13384c.php'], [
205+
[
206+
'Method Bug13384c\Bug13384c::doBar() never returns true so it can be removed from the return type.',
207+
33,
208+
],
209+
[
210+
'Method Bug13384c\Bug13384c::doBar2() never returns false so it can be removed from the return type.',
211+
37,
212+
],
213+
[
214+
'Method Bug13384c\Bug13384Static::doBar() never returns true so it can be removed from the return type.',
215+
50,
216+
],
217+
[
218+
'Method Bug13384c\Bug13384Static::doBar2() never returns false so it can be removed from the return type.',
219+
54,
220+
],
221+
]);
222+
}
223+
202224
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Bug13384c;
4+
5+
function doFoo(): bool {
6+
return false;
7+
}
8+
9+
function doFoo2(): bool {
10+
return true;
11+
}
12+
13+
function doFoo3(): bool {
14+
if (rand(0, 1)) {
15+
return true;
16+
}
17+
return false;
18+
}
19+
20+
21+
class Bug13384c {
22+
public function doBarPublic(): bool {
23+
return false;
24+
}
25+
26+
/**
27+
* @return false
28+
*/
29+
private function doBarPhpdocReturn(): bool {
30+
return false;
31+
}
32+
33+
private function doBar(): bool {
34+
return false;
35+
}
36+
37+
private function doBar2(): bool {
38+
return true;
39+
}
40+
41+
private function doBar3(): bool {
42+
if (rand(0, 1)) {
43+
return true;
44+
}
45+
return false;
46+
}
47+
}
48+
49+
class Bug13384Static {
50+
private static function doBar(): bool {
51+
return false;
52+
}
53+
54+
private static function doBar2(): bool {
55+
return true;
56+
}
57+
58+
private static function doBar3(): bool {
59+
if (rand(0, 1)) {
60+
return true;
61+
}
62+
return false;
63+
}
64+
}

0 commit comments

Comments
 (0)