Skip to content
Merged
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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

just had the idea that we only need todo this work when the bound maybe matches

#5122

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
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,38 @@ 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,
],
]);
}

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

$this->analyse([__DIR__ . '/data/bug-12558.php'], [
[
'Parameter #1 $object of static method Bug12558\Foo::assertStatic() expects Bug12558\Foo, Bug12558\Foo|null given.',
45,
],
[
'Parameter #1 $object of static method Bug12558\Foo::assertStatic() expects Bug12558\Foo, bool|Bug12558\Foo given.',
46,
],
]);
}

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

namespace Bug12558;

class Foo
{
/**
* @template T of object
*
* @param T $object
*
* @return (T is static ? T : static)
*/
public static function assertStatic(object $object)
{
if (!$object instanceof static) {
throw new \Error('Object is not an instance of static class');
}

return $object;
}

protected function createFoo(): self
{
return new Foo();
}

protected function createFooNullable(): ?self
{
return new Foo();
}

protected function createFooUnionedWithBool(): self|bool
{
return new Foo();
}

protected function foo(): void
{
}

public function testAssertInstanceOf(): void
{
(static::class)::assertStatic($this->createFoo())->foo();
(static::class)::assertStatic($this->createFooNullable())->foo();
(static::class)::assertStatic($this->createFooUnionedWithBool())->foo();
}
}
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