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
30 changes: 28 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2321,13 +2321,39 @@
$arrowFunctionScope = $arrowFunctionScope->invalidateExpression(new Variable('this'));
}

$filteredExpressionTypes = $this->invalidateStaticExpressions($arrowFunctionScope->expressionTypes);
$filteredNativeExpressionTypes = $arrowFunctionScope->nativeExpressionTypes;

if (!$arrowFunction->static && $this->hasVariableType('this')->yes()) {

Check warning on line 2327 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $filteredExpressionTypes = $this->invalidateStaticExpressions($arrowFunctionScope->expressionTypes); $filteredNativeExpressionTypes = $arrowFunctionScope->nativeExpressionTypes; - if (!$arrowFunction->static && $this->hasVariableType('this')->yes()) { + if (!$arrowFunction->static && !$this->hasVariableType('this')->no()) { foreach ($filteredExpressionTypes as $exprString => $typeHolder) { $expr = $typeHolder->getExpr(); if (!$expr instanceof PropertyFetch) {

Check warning on line 2327 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $filteredExpressionTypes = $this->invalidateStaticExpressions($arrowFunctionScope->expressionTypes); $filteredNativeExpressionTypes = $arrowFunctionScope->nativeExpressionTypes; - if (!$arrowFunction->static && $this->hasVariableType('this')->yes()) { + if (!$arrowFunction->static && !$this->hasVariableType('this')->no()) { foreach ($filteredExpressionTypes as $exprString => $typeHolder) { $expr = $typeHolder->getExpr(); if (!$expr instanceof PropertyFetch) {
foreach ($filteredExpressionTypes as $exprString => $typeHolder) {
$expr = $typeHolder->getExpr();
if (!$expr instanceof PropertyFetch) {
continue;
}
if ($this->isReadonlyPropertyFetch($expr, true)) {
continue;
}
unset($filteredExpressionTypes[$exprString]);
}
foreach ($filteredNativeExpressionTypes as $exprString => $typeHolder) {
$expr = $typeHolder->getExpr();
if (!$expr instanceof PropertyFetch) {
continue;
}
if ($this->isReadonlyPropertyFetch($expr, true)) {
continue;
}
unset($filteredNativeExpressionTypes[$exprString]);
}
}

return $this->scopeFactory->create(
$arrowFunctionScope->context,
$this->isDeclareStrictTypes(),
$arrowFunctionScope->getFunction(),
$arrowFunctionScope->getNamespace(),
$this->invalidateStaticExpressions($arrowFunctionScope->expressionTypes),
$arrowFunctionScope->nativeExpressionTypes,
$filteredExpressionTypes,
$filteredNativeExpressionTypes,
$arrowFunctionScope->conditionalExpressions,
$arrowFunctionScope->inClosureBindScopeClasses,
new ClosureType(),
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Generic/TemplateTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public function describe(VerbosityLevel $level): string
}
$defaultDescription = '';
if ($this->default !== null) {
$recursionGuard = RecursionGuard::runOnObjectIdentity($this->default, fn () => $this->default->describe($level));
$default = $this->default;
$recursionGuard = RecursionGuard::runOnObjectIdentity($default, static fn () => $default->describe($level));
if (!$recursionGuard instanceof ErrorType) {
$defaultDescription .= sprintf(' = %s', $recursionGuard);
}
Expand Down
55 changes: 55 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13563.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types = 1);

namespace Bug13563;

use DateTime;
use function PHPStan\Testing\assertType;

class Invoker
{
/**
* @var array<string, \Closure>
*/
private array $callbacks = [];

public function willReturnCallback(string $method, callable $callback): void
{
$this->callbacks[$method] = \Closure::fromCallable($callback);
}
}

class MyTest
{
/**
* @var array<int, DateTime>
*/
private array $dates = [];

/**
* @var array<int, DateTime>
*/
private array $propNotCleared = [];

public function setUp(): void
{
$invoker = new Invoker();
$this->dates = [];

assertType('array{}', $this->dates);

// Arrow function should see the declared property type, not the narrowed array{} type
$invoker->willReturnCallback('get', fn (int $id) => assertType('array<int, DateTime>', $this->dates));

// Closure correctly sees the declared property type
$invoker->willReturnCallback('get', function (int $id) {
assertType('array<int, DateTime>', $this->dates);
});

// Property not cleared - both should see the declared type
assertType('array<int, DateTime>', $this->propNotCleared);
$invoker->willReturnCallback('get', fn (int $id) => assertType('array<int, DateTime>', $this->propNotCleared));
$invoker->willReturnCallback('get', function (int $id) {
assertType('array<int, DateTime>', $this->propNotCleared);
});
}
}
Loading