Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Type/Generic/TemplateTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function count;
use function sprintf;

/**
Expand Down Expand Up @@ -287,6 +288,23 @@ public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
]))->union($map);
}

if ($receivedType instanceof UnionType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we drop the previous if on line 285 than? seems like its redundant now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not redundant, the previous if will works

  • For any non UnionType
  • For BenevolentUnionType
  • For UnionType in which the whole type is superTypeOf

$matchingTypes = [];
foreach ($receivedType->getTypes() as $innerType) {
if (!$resolvedBound->isSuperTypeOf($innerType)->yes()) {
continue;
}

$matchingTypes[] = $innerType;
}
if (count($matchingTypes) > 0) {
$filteredType = TypeCombinator::union(...$matchingTypes);
return (new TemplateTypeMap([
$this->name => $filteredType,
]))->union($map);
}
}

return $map;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,20 @@ public function testBug13556(): void
$this->analyse([__DIR__ . '/data/bug-13556.php'], []);
}

public function testBug9732(): void
{
$this->checkThisOnly = false;
$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;

$this->analyse([__DIR__ . '/data/bug-9732.php'], [
[
'Parameter #1 $array of static method Bug9732\HelloWorld::stringifyKeys() expects array<string, mixed>, array<mixed> given.',
21,
],
]);
}

#[RequiresPhp('>= 8.5')]
public function testPipeOperator(): void
{
Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-9732.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types = 1);

namespace Bug9732;

class HelloWorld
{
/**
* @phpstan-template TKeyType of string
* @phpstan-template TValueType
* @phpstan-param array<TKeyType, TValueType> $array
* @phpstan-return \Generator<TKeyType, TValueType, void, void>
*/
public static function stringifyKeys(array $array) : \Generator{
foreach($array as $key => $value){
yield (string) $key => $value;
}
}

public function sayHello(): void
{
self::stringifyKeys($GLOBALS);
}
}
Loading