Skip to content

Commit 00a9d94

Browse files
committed
One more wrong tip
1 parent 00adfaa commit 00a9d94

File tree

4 files changed

+74
-19
lines changed

4 files changed

+74
-19
lines changed

src/Type/Accessory/NonEmptyArrayType.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use PHPStan\Type\Type;
2626
use PHPStan\Type\UnionType;
2727
use PHPStan\Type\VerbosityLevel;
28-
use function sprintf;
2928

3029
class NonEmptyArrayType implements CompoundType, AccessoryType
3130
{
@@ -86,17 +85,8 @@ public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
8685

8786
$isArray = $type->isArray();
8887
$isIterableAtLeastOnce = $type->isIterableAtLeastOnce();
89-
$reasons = [];
90-
if ($isArray->yes() && !$isIterableAtLeastOnce->yes()) {
91-
$verbosity = VerbosityLevel::getRecommendedLevelByType($this, $type);
92-
$reasons[] = sprintf(
93-
'%s %s empty.',
94-
$type->describe($verbosity),
95-
$isIterableAtLeastOnce->no() ? 'is' : 'might be',
96-
);
97-
}
9888

99-
return new AcceptsResult($isArray->and($isIterableAtLeastOnce), $reasons);
89+
return new AcceptsResult($isArray->and($isIterableAtLeastOnce), []);
10090
}
10191

10292
public function isSuperTypeOf(Type $type): TrinaryLogic

src/Type/IntersectionType.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,27 @@ public function acceptsWithReason(Type $otherType, bool $strictTypes): AcceptsRe
191191

192192
if (!$result->yes()) {
193193
$isList = $otherType->isList();
194+
$reasons = [];
195+
$verbosity = VerbosityLevel::getRecommendedLevelByType($this, $otherType);
194196
if ($this->isList()->yes() && !$isList->yes()) {
195-
$verbosity = VerbosityLevel::getRecommendedLevelByType($this, $otherType);
196-
return new AcceptsResult($result->result, [
197-
sprintf(
198-
'%s %s a list.',
199-
$otherType->describe($verbosity),
200-
$isList->no() ? 'is not' : 'might not be',
201-
),
202-
]);
197+
$reasons[] = sprintf(
198+
'%s %s a list.',
199+
$otherType->describe($verbosity),
200+
$isList->no() ? 'is not' : 'might not be',
201+
);
202+
}
203+
204+
$isNonEmpty = $otherType->isIterableAtLeastOnce();
205+
if ($this->isIterableAtLeastOnce()->yes() && !$isNonEmpty->yes()) {
206+
$reasons[] = sprintf(
207+
'%s %s empty.',
208+
$otherType->describe($verbosity),
209+
$isNonEmpty->no() ? 'is' : 'might be',
210+
);
211+
}
212+
213+
if (count($reasons) > 0) {
214+
return new AcceptsResult($result->result, $reasons);
203215
}
204216
}
205217

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,15 @@ public function testWrongListTip(): void
962962
'Method WrongListTip\Test::doFoo() should return list<WrongListTip\Foo> but returns list<WrongListTip\Bar>.',
963963
23,
964964
],
965+
[
966+
'Method WrongListTip\Test2::doFoo() should return non-empty-array<WrongListTip\Foo> but returns non-empty-array<WrongListTip\Bar>.',
967+
44,
968+
],
969+
[
970+
'Method WrongListTip\Test3::doFoo() should return non-empty-list<WrongListTip\Foo> but returns array<WrongListTip\Bar>.',
971+
67,
972+
"• array<WrongListTip\Bar> might not be a list.\n• array<WrongListTip\Bar> might be empty.",
973+
],
965974
]);
966975
}
967976

tests/PHPStan/Rules/Methods/data/wrong-list-tip.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,47 @@ public function listOfBars(): array
3232
}
3333

3434
}
35+
36+
class Test2
37+
{
38+
39+
/**
40+
* @return non-empty-array<Foo>
41+
*/
42+
public function doFoo(): array
43+
{
44+
return $this->nonEmptyArrayOfBars();
45+
}
46+
47+
/**
48+
* @return non-empty-array<Bar>
49+
*/
50+
public function nonEmptyArrayOfBars(): array
51+
{
52+
/** @var Bar $b */
53+
$b = doFoo();
54+
return [$b];
55+
}
56+
57+
}
58+
59+
class Test3
60+
{
61+
62+
/**
63+
* @return non-empty-list<Foo>
64+
*/
65+
public function doFoo(): array
66+
{
67+
return $this->nonEmptyArrayOfBars();
68+
}
69+
70+
/**
71+
* @return array<Bar>
72+
*/
73+
public function nonEmptyArrayOfBars(): array
74+
{
75+
return [];
76+
}
77+
78+
}

0 commit comments

Comments
 (0)