Skip to content

Commit

Permalink
One more wrong tip
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 14, 2023
1 parent 00adfaa commit 00a9d94
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 19 deletions.
12 changes: 1 addition & 11 deletions src/Type/Accessory/NonEmptyArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

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

$isArray = $type->isArray();
$isIterableAtLeastOnce = $type->isIterableAtLeastOnce();
$reasons = [];
if ($isArray->yes() && !$isIterableAtLeastOnce->yes()) {
$verbosity = VerbosityLevel::getRecommendedLevelByType($this, $type);
$reasons[] = sprintf(
'%s %s empty.',
$type->describe($verbosity),
$isIterableAtLeastOnce->no() ? 'is' : 'might be',
);
}

return new AcceptsResult($isArray->and($isIterableAtLeastOnce), $reasons);
return new AcceptsResult($isArray->and($isIterableAtLeastOnce), []);
}

public function isSuperTypeOf(Type $type): TrinaryLogic
Expand Down
28 changes: 20 additions & 8 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,27 @@ public function acceptsWithReason(Type $otherType, bool $strictTypes): AcceptsRe

if (!$result->yes()) {
$isList = $otherType->isList();
$reasons = [];
$verbosity = VerbosityLevel::getRecommendedLevelByType($this, $otherType);
if ($this->isList()->yes() && !$isList->yes()) {
$verbosity = VerbosityLevel::getRecommendedLevelByType($this, $otherType);
return new AcceptsResult($result->result, [
sprintf(
'%s %s a list.',
$otherType->describe($verbosity),
$isList->no() ? 'is not' : 'might not be',
),
]);
$reasons[] = sprintf(
'%s %s a list.',
$otherType->describe($verbosity),
$isList->no() ? 'is not' : 'might not be',
);
}

$isNonEmpty = $otherType->isIterableAtLeastOnce();
if ($this->isIterableAtLeastOnce()->yes() && !$isNonEmpty->yes()) {
$reasons[] = sprintf(
'%s %s empty.',
$otherType->describe($verbosity),
$isNonEmpty->no() ? 'is' : 'might be',
);
}

if (count($reasons) > 0) {
return new AcceptsResult($result->result, $reasons);
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,15 @@ public function testWrongListTip(): void
'Method WrongListTip\Test::doFoo() should return list<WrongListTip\Foo> but returns list<WrongListTip\Bar>.',
23,
],
[
'Method WrongListTip\Test2::doFoo() should return non-empty-array<WrongListTip\Foo> but returns non-empty-array<WrongListTip\Bar>.',
44,
],
[
'Method WrongListTip\Test3::doFoo() should return non-empty-list<WrongListTip\Foo> but returns array<WrongListTip\Bar>.',
67,
"• array<WrongListTip\Bar> might not be a list.\n• array<WrongListTip\Bar> might be empty.",
],
]);
}

Expand Down
44 changes: 44 additions & 0 deletions tests/PHPStan/Rules/Methods/data/wrong-list-tip.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,47 @@ public function listOfBars(): array
}

}

class Test2
{

/**
* @return non-empty-array<Foo>
*/
public function doFoo(): array
{
return $this->nonEmptyArrayOfBars();
}

/**
* @return non-empty-array<Bar>
*/
public function nonEmptyArrayOfBars(): array
{
/** @var Bar $b */
$b = doFoo();
return [$b];
}

}

class Test3
{

/**
* @return non-empty-list<Foo>
*/
public function doFoo(): array
{
return $this->nonEmptyArrayOfBars();
}

/**
* @return array<Bar>
*/
public function nonEmptyArrayOfBars(): array
{
return [];
}

}

0 comments on commit 00a9d94

Please sign in to comment.