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
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,12 @@ parameters:
count: 2
path: src/Type/TypeCombinator.php

-
rawMessage: Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
count: 2
path: src/Type/TypeCombinator.php

-
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
Expand Down
38 changes: 36 additions & 2 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateArrayType;
use PHPStan\Type\Generic\TemplateBenevolentUnionType;
use PHPStan\Type\Generic\TemplateMixedType;
Expand Down Expand Up @@ -1503,9 +1504,42 @@ public static function intersect(Type ...$types): Type
continue 2;
}

if ($isSuperTypeA->no()) {
return new NeverType();
if (!$isSuperTypeA->no()) {
continue;
}

if (
$types[$i] instanceof GenericObjectType
&& $types[$j] instanceof GenericObjectType
&& $types[$i]->getClassName() === $types[$j]->getClassName()
&& count($types[$i]->getTypes()) === count($types[$j]->getTypes())
) {
$mergedParams = [];
foreach ($types[$i]->getTypes() as $k => $paramTypeI) {
$paramTypeJ = $types[$j]->getTypes()[$k];
$merged = self::intersect($paramTypeI, $paramTypeJ);
if ($merged instanceof NeverType) {
return $merged;
}
$mergedParams[] = $merged;
}
$subtractedType = $types[$i]->getSubtractedType();
if ($subtractedType !== null && $types[$j]->getSubtractedType() !== null) {
$subtractedType = self::union($subtractedType, $types[$j]->getSubtractedType());
} elseif ($types[$j]->getSubtractedType() !== null) {
$subtractedType = $types[$j]->getSubtractedType();
}
$types[$j] = new GenericObjectType(
$types[$j]->getClassName(),
$mergedParams,
$subtractedType,
);
array_splice($types, $i--, 1);
$typesCount--;
continue 2;
}

return new NeverType();
}
}

Expand Down
103 changes: 103 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-5357.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php declare(strict_types = 1);

namespace Bug5357;

use function PHPStan\Testing\assertType;

/**
* @phpstan-template T of object
*/
interface AdminInterface {}

/**
* @phpstan-template T of ProxyQueryInterface
*/
interface PagerInterface {}

/**
* @phpstan-template T of ProxyQueryInterface
* @phpstan-implements PagerInterface<T>
*/
class Pager implements PagerInterface
{}

/**
* @phpstan-template T of ProxyQueryInterface
*/
interface DatagridInterface
{
/**
* @phpstan-return PagerInterface<T>
*/
public function getPager(): PagerInterface;
}

/**
* @phpstan-template T of ProxyQueryInterface
* @phpstan-implements DatagridInterface<T>
*/
class Datagrid implements DatagridInterface
{
/**
* @phpstan-return PagerInterface<T>
*/
public function getPager(): PagerInterface
{
throw new \Exception();
}
}

interface ProxyQueryInterface {}

class Proxy implements ProxyQueryInterface {}

class MockObject {}

/**
* @phpstan-template T of ProxyQueryInterface
*/
interface DatagridBuilderInterface
{
/**
* @param AdminInterface<object> $admin
* @param array<string, mixed> $values
*
* @phpstan-return DatagridInterface<T>
*/
public function getBaseDatagrid(AdminInterface $admin, array $values = []): DatagridInterface;
}

/**
* @phpstan-implements DatagridBuilderInterface<Proxy>
*/
class DatagridBuilder implements DatagridBuilderInterface
{
/**
* @param AdminInterface<object> $admin
* @param array<string, mixed> $values
*
* @phpstan-return DatagridInterface<Proxy>
*/
public function getBaseDatagrid(AdminInterface $admin, array $values = []): DatagridInterface
{
throw new \Exception();
}
}

class HelloWorld
{
/**
* @var MockObject&AdminInterface<object>
*/
public $admin;

public DatagridBuilder $datagridBuilder;

public function sayHello(): void
{
$datagrid = $this->datagridBuilder->getBaseDatagrid($this->admin);
assert($datagrid instanceof Datagrid);
assertType('Bug5357\Datagrid&Bug5357\DatagridInterface<Bug5357\Proxy>', $datagrid);
assertType('Bug5357\PagerInterface<Bug5357\Proxy>', $datagrid->getPager());
}
}
Loading