Skip to content

Commit

Permalink
Fix supertype checks between late resolvable types
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard van Velzen authored and ondrejmirtes committed Jul 19, 2022
1 parent 2c49320 commit 958a053
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Type/Traits/LateResolvableTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Type\BooleanType;
use PHPStan\Type\CompoundType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\LateResolvableType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;

Expand All @@ -36,6 +37,10 @@ private function isSuperTypeOfDefault(Type $type): TrinaryLogic
return TrinaryLogic::createYes();
}

if ($type instanceof LateResolvableType) {
$type = $type->resolve();
}

$isSuperType = $this->resolve()->isSuperTypeOf($type);

if (!$this->isResolvable()) {
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,16 @@ public function testOverridenMethodWithConditionalReturnType(): void
]);
}

public function testBug7652(): void
{
$this->reportMaybes = true;
$this->reportStatic = true;
$this->analyse([__DIR__ . '/data/bug-7652.php'], [
[
'Parameter #1 $offset (TOffset of key-of<TArray of array>) of method Bug7652\Options::offsetSet() should be contravariant with parameter $offset (key-of<array>|null) of method ArrayAccess<key-of<TArray of array>,value-of<TArray of array>>::offsetSet()',
30,
],
]);
}

}
38 changes: 38 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-7652.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace Bug7652;

// @link https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/OptionsResolver/Options.php

/**
* @template TArray of array
* @extends \ArrayAccess<key-of<TArray>, value-of<TArray>>
*/
interface Options extends \ArrayAccess {

/**
* @param key-of<TArray> $offset
*/
public function offsetExists(mixed $offset): bool;

/**
* @template TOffset of key-of<TArray>
* @param TOffset $offset
* @return TArray[TOffset]
*/
public function offsetGet(mixed $offset);

/**
* @template TOffset of key-of<TArray>
* @param TOffset $offset
* @param TArray[TOffset] $value
*/
public function offsetSet(mixed $offset, mixed $value): void;

/**
* @template TOffset of key-of<TArray>
* @param TOffset $offset
*/
public function offsetUnset(mixed $offset): void;

}

0 comments on commit 958a053

Please sign in to comment.