Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static function dataAsserts(): iterable
yield from self::gatherAssertTypes(__DIR__ . '/data/dynamic-method-return-compound-types.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-7344.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-7391b.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-7385.php');
}

/**
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/data/TestDynamicReturnTypeExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\Dummy\ChangedTypeMethodReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
Expand All @@ -18,6 +22,7 @@
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MethodTypeSpecifyingExtension;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
Expand Down Expand Up @@ -299,3 +304,34 @@ public function getTypeFromStaticMethodCall(
return $scope->getType(new New_($methodCall->class));
}
}

class Bug7385MethodTypeSpecifyingExtension implements TypeSpecifierAwareExtension, MethodTypeSpecifyingExtension
{
public function getClass(): string
{
return \Bug7385\Model::class;
}

public function isMethodSupported(MethodReflection $methodReflection, ?MethodCall $methodCall = null, ?TypeSpecifierContext $context = null): bool
{
return $methodReflection->getName() === 'assertHasIface';
}

/** @var TypeSpecifier */
protected $typeSpecifier;

public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}

public function specifyTypes(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
$type = TypeCombinator::intersect(
$scope->getType($methodCall->var),
new ObjectType(\Bug7385\Iface::class)
);

return $this->typeSpecifier->create($methodCall->var, $type, TypeSpecifierContext::createNull());
}
}
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7385.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace Bug7385;

use function PHPStan\Testing\assertType;

class Model
{
public function assertHasIface(): void
{
throw new \Error('For static analysis only, $this type is narrowed by MethodTypeSpecifyingExtension');
}
}

interface Iface
{
}

function () {
$m = random_int(0, 1) === 0 ? new Model() : new class() extends Model implements Iface {};
assertType('Bug7385\Model', $m);
$m->assertHasIface();
assertType('Bug7385\Iface&Bug7385\Model', $m);
};
4 changes: 4 additions & 0 deletions tests/PHPStan/Analyser/dynamic-return-type.neon
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ services:
class: PHPStan\Tests\Bug7391BDynamicStaticMethodReturnTypeExtension
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
-
class: PHPStan\Tests\Bug7385MethodTypeSpecifyingExtension
tags:
- phpstan.typeSpecifier.methodTypeSpecifyingExtension
Loading