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
8 changes: 4 additions & 4 deletions src/Rules/RuleLevelHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private function transformAcceptedType(Type $acceptingType, Type $acceptedType):

return new CallableType(
$acceptedType->getParameters(),
$traverse($this->transformCommonType($acceptedType->getReturnType())),
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need another test for callable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

$traverse($acceptedType->getReturnType()),
$acceptedType->isVariadic(),
$acceptedType->getTemplateTypeMap(),
$acceptedType->getResolvedTemplateTypeMap(),
Expand All @@ -111,7 +111,7 @@ private function transformAcceptedType(Type $acceptingType, Type $acceptedType):

return new ClosureType(
$acceptedType->getParameters(),
$traverse($this->transformCommonType($acceptedType->getReturnType())),
$traverse($acceptedType->getReturnType()),
$acceptedType->isVariadic(),
$acceptedType->getTemplateTypeMap(),
$acceptedType->getResolvedTemplateTypeMap(),
Expand Down Expand Up @@ -142,10 +142,10 @@ private function transformAcceptedType(Type $acceptingType, Type $acceptedType):
}
}

return $traverse($this->transformCommonType($acceptedType));
return $traverse($acceptedType);
});

return [$acceptedType, $checkForUnion];
return [$this->transformCommonType($acceptedType), $checkForUnion];
}

/** @api */
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
64 changes: 64 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-13440.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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);
}
}

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

/**
* @template TNewReturn
* @param callable(Foo<TVal>): TNewReturn $cb
* @return self<TVal, TNewReturn>
*/
public function test($cb): self
{
return new self($this->val, $cb);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,14 @@ public function testBug12250(): void
$this->analyse([__DIR__ . '/data/bug-12250.php'], []);
}

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

public function testBug4525(): void
{
$this->analyse([__DIR__ . '/data/bug-4525.php'], []);
Expand Down
54 changes: 54 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-12688.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php // lint >= 8.1

namespace Bug12688;

/**
* @template T = mixed
*/
interface I {}

/**
* @implements I<mixed>
*/
enum E implements I
{
case E;
}

/**
* @template T
*/
final class TemplateWithoutDefaultWorks
{
/**
* @var I<T>
*/
public readonly I $i;

/**
* @param I<T> $i
*/
public function __construct(I $i = E::E)
{
$this->i = $i;
}
}

/**
* @template T = mixed
*/
final class TemplateWithDefaultDoesNotWork
{
/**
* @var I<T>
*/
public readonly I $i;

/**
* @param I<T> $i
*/
public function __construct(I $i = E::E)
{
$this->i = $i;
}
}
Loading