Skip to content
Closed
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
9 changes: 9 additions & 0 deletions src/Type/Generic/TemplateTypeVariance.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ public function isValidVariance(TemplateType $templateType, Type $a, Type $b): I

if ($this->invariant()) {
$result = $a->equals($b);
if (
!$result
&& $a instanceof TemplateType
&& $b instanceof TemplateType
&& $a->getScope()->equals($b->getScope())
&& $a->getName() === $b->getName()
) {
$result = true;
}
$reasons = [];
if (!$result) {
if (
Expand Down
11 changes: 10 additions & 1 deletion tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
class InstantiationRuleTest extends RuleTestCase
{

private bool $checkExplicitMixed = false;

protected function getRule(): Rule
{
$reflectionProvider = self::createReflectionProvider();
$container = self::getContainer();
return new InstantiationRule(
$container,
$reflectionProvider,
new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false, true), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, true, true, true, true),
new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, $this->checkExplicitMixed, false, false, true), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, true, true, true, true),
new ClassNameCheck(
new ClassCaseSensitivityCheck($reflectionProvider, true),
new ClassForbiddenNameCheck($container),
Expand Down Expand Up @@ -570,6 +572,13 @@ public function testBug14097(): void
$this->analyse([__DIR__ . '/data/bug-14097.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug13440(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-13440.php'], []);
}

public function testNewStaticWithConsistentConstructor(): void
{
$this->analyse([__DIR__ . '/data/instantiation-new-static-consistent-constructor.php'], [
Expand Down
37 changes: 37 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-13440.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug13440;

use Closure;

/** @template T */
interface Foo {}

/**
* @template TVal
* @template TReturn
*/
class Box
{
/**
* @param TVal $val
* @param Closure(Foo<TVal>): TReturn $cb
*/
public function __construct(
private mixed $val,
private Closure $cb,
) {
}

/**
* @template TNewReturn
* @param Closure(Foo<TVal>): TNewReturn $cb
* @return self<TVal, TNewReturn>
*/
public function test(Closure $cb): self
{
return new self($this->val, $cb);
}
}
Loading