Skip to content

Commit 8e3457f

Browse files
committed
test methods
1 parent 123e218 commit 8e3457f

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
if (
103103
$returnType->isConstantScalarValue()->yes()
@@ -115,7 +115,8 @@ public function checkFunction(
115115
}
116116

117117
$messages = [];
118-
foreach ($functionReturnType->getTypes() as $type) {
118+
$functionReturnTypes = $functionReturnType instanceof UnionType ? $functionReturnType->getTypes() : $functionReturnType->getFiniteTypes();
119+
foreach ($functionReturnTypes as $type) {
119120
if (!$type->isSuperTypeOf($returnType)->no()) {
120121
continue;
121122
}

tests/PHPStan/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRuleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,18 @@ public function testBug10312a(): void
6666
$this->analyse([__DIR__ . '/data/bug-10312a.php'], []);
6767
}
6868

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

tests/PHPStan/Rules/TooWideTypehints/TooWideMethodReturnTypehintRuleTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,26 @@ public function testBug10312d(): void
218218
$this->analyse([__DIR__ . '/data/bug-10312d.php'], []);
219219
}
220220

221+
public function testBug13384c(): void
222+
{
223+
$this->analyse([__DIR__ . '/data/bug-13384c.php'], [
224+
[
225+
'Method Bug13384c\Bug13384c::doBar() never returns true so it can be removed from the return type.',
226+
33,
227+
],
228+
[
229+
'Method Bug13384c\Bug13384c::doBar2() never returns false so it can be removed from the return type.',
230+
37,
231+
],
232+
[
233+
'Method Bug13384c\Bug13384Static::doBar() never returns true so it can be removed from the return type.',
234+
50,
235+
],
236+
[
237+
'Method Bug13384c\Bug13384Static::doBar2() never returns false so it can be removed from the return type.',
238+
54,
239+
],
240+
]);
241+
}
242+
221243
}
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)