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
2 changes: 1 addition & 1 deletion src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ public static function intersect(Type ...$types): Type
for ($i = 0; $i < $typesCount; $i++) {
$type = $types[$i];

if ($type instanceof IntersectionType) {
if ($type instanceof IntersectionType && !$type instanceof TemplateType) {
// transform A & (B & C) to A & B & C
array_splice($types, $i--, 1, $type->getTypes());
$typesCount = count($types);
Expand Down
35 changes: 35 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13577.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);

namespace Bug13577;

use function PHPStan\Testing\assertType;

class A {}
interface B {}

/**
* @template T of A&B
*/
class Foo
{
/**
* @param T $foo
*/
public function a($foo): void
{
assertType('T of Bug13577\A&Bug13577\B (class Bug13577\Foo, argument)', $foo);

if (!$foo instanceof B) {
throw new \Exception();
}

assertType('T of Bug13577\A&Bug13577\B (class Bug13577\Foo, argument)', $foo);

$this->b($foo);
}

/**
* @param T $foo
*/
public function b($foo): void {}
}
23 changes: 23 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14348.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types = 1);

namespace Bug14348;

use function PHPStan\Testing\assertType;

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

abstract class HelloWorld
{
/**
* @phpstan-template T of PositionEntityInterface&TgEntityInterface
*
* @param non-empty-array<T> $tgs
*/
public function computeForFrontByPosition(array $tgs): void
{
assertType('T of Bug14348\PositionEntityInterface&Bug14348\TgEntityInterface (method Bug14348\HelloWorld::computeForFrontByPosition(), argument)', $tgs[0]);
}
}
35 changes: 35 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9961.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);

namespace Bug9961;

use function PHPStan\Testing\assertType;

interface Ia {}
interface Ib {}
interface Ic {}
interface Id {}

class A implements Ia, Ib {}

class HelloWorld
{
/**
* @template T of (Ia&Ib)|(Ic&Id)
* @param T $a
* @return T
*/
public function sayHello(Ia|Ic $a): mixed
{
if ($a instanceof Ic && $a instanceof Id) {
assertType('T of Bug9961\Ic&Bug9961\Id (method Bug9961\HelloWorld::sayHello(), argument)', $a);
} elseif ($a instanceof A) {
assertType('Bug9961\A&T of T of Bug9961\Ia&Bug9961\Ib (method Bug9961\HelloWorld::sayHello(), argument) (method Bug9961\HelloWorld::sayHello(), argument)', $a);
} else {
throw new \Exception;
}

assertType('(Bug9961\A&T of T of Bug9961\Ia&Bug9961\Ib (method Bug9961\HelloWorld::sayHello(), argument) (method Bug9961\HelloWorld::sayHello(), argument))|T of Bug9961\Ic&Bug9961\Id (method Bug9961\HelloWorld::sayHello(), argument)', $a);

return $a;
}
}
16 changes: 16 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\GenericStaticType;
use PHPStan\Type\Generic\TemplateBenevolentUnionType;
use PHPStan\Type\Generic\TemplateIntersectionType;
use PHPStan\Type\Generic\TemplateMixedType;
use PHPStan\Type\Generic\TemplateObjectType;
use PHPStan\Type\Generic\TemplateObjectWithoutClassType;
Expand Down Expand Up @@ -4917,6 +4918,21 @@ public static function dataIntersect(): iterable
ObjectType::class,
$nonFinalClass->getDisplayName() . '=final',
];

// https://github.com/phpstan/phpstan/issues/14348
yield [
[
TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('a'),
'T',
new IntersectionType([new ObjectType('Iterator'), new ObjectType('Countable')]),
TemplateTypeVariance::createInvariant(),
),
new MixedType(),
],
TemplateIntersectionType::class,
'T of Countable&Iterator (function a(), parameter)',
];
}

/**
Expand Down
Loading