Skip to content

Commit

Permalink
Fix TemplateTypeArgumentStrategy::accepts()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 17, 2022
1 parent 5b2d7b6 commit 85ab8cf
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 11 deletions.
11 changes: 0 additions & 11 deletions src/Type/Generic/TemplateTypeArgumentStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPStan\TrinaryLogic;
use PHPStan\Type\CompoundType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\Type;

/**
Expand All @@ -15,16 +14,6 @@ class TemplateTypeArgumentStrategy implements TemplateTypeStrategy

public function accepts(TemplateType $left, Type $right, bool $strictTypes): TrinaryLogic
{
if ($right instanceof IntersectionType) {
foreach ($right->getTypes() as $type) {
if ($this->accepts($left, $type, $strictTypes)->yes()) {
return TrinaryLogic::createYes();
}
}

return TrinaryLogic::createNo();
}

if ($right instanceof CompoundType) {
$accepts = $right->isAcceptedBy($left, $strictTypes);
} else {
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,4 +700,10 @@ public function testBug7265(): void
$this->analyse([__DIR__ . '/data/bug-7265.php'], []);
}

public function testBug7460(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-7460.php'], []);
}

}
79 changes: 79 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-7460.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Bug7460;

interface PositionEntityInterface {
public function getPosition(): int;
}
interface TgEntityInterface {}

abstract class HelloWorld
{
/**
* @phpstan-template T of PositionEntityInterface&TgEntityInterface
*
* @param iterable<T> $tgs
*
* @return array<T>
*
* @throws \Exception
*/
public function computeForFrontByPosition($tgs)
{
/** @phpstan-var array<T> $res */
$res = [];

foreach ($tgs as $tgItem) {
$position = $tgItem->getPosition();

if (!isset($res[$position])) {
$res[$position] = $tgItem;
} else {
/** @phpstan-var T $tgItemToKeep */
$tgItemToKeep = $this->compare($tgItem, $res[$position]);
$res[$position] = $tgItemToKeep;
}
}
ksort($res);

return $res;
}

/**
* @phpstan-template T of PositionEntityInterface&TgEntityInterface
*
* @param iterable<T> $tgs
*
* @return array<T>
*
* @throws \Exception
*/
public function computeForFrontByPosition2($tgs)
{
/** @phpstan-var array<T> $res */
$res = [];

foreach ($tgs as $tgItem) {
$position = $tgItem->getPosition();

if (!isset($res[$position])) {
$res[$position] = $tgItem;
} else {
/** @phpstan-var T $tgItemToKeep */
$tgItemToKeep = $this->compare($tgItem, $res[$position]);
$res[$position] = $tgItemToKeep;
}
}
ksort($res);

return $res;
}

/**
* @phpstan-template S of TgEntityInterface
* @phpstan-param S $nextTg
* @phpstan-param S $currentTg
* @phpstan-return S
*/
abstract protected function compare(TgEntityInterface $nextTg, TgEntityInterface $currentTg): TgEntityInterface;
}

0 comments on commit 85ab8cf

Please sign in to comment.