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
46 changes: 46 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2716,7 +2716,16 @@
$this->callNodeCallback($nodeCallback, $expr->returnType, $scope, $storage);
}

$phpDocParameterTypes = $this->resolveClosurePhpDocParameterTypes($stmt, $scope, $expr);

$closureScope = $scope->enterAnonymousFunction($expr, $callableParameters);
foreach ($phpDocParameterTypes as $paramName => $paramType) {
if (!$closureScope->hasVariableType($paramName)->yes()) {

Check warning on line 2723 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $closureScope = $scope->enterAnonymousFunction($expr, $callableParameters); foreach ($phpDocParameterTypes as $paramName => $paramType) { - if (!$closureScope->hasVariableType($paramName)->yes()) { + if ($closureScope->hasVariableType($paramName)->no()) { continue; }

Check warning on line 2723 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $closureScope = $scope->enterAnonymousFunction($expr, $callableParameters); foreach ($phpDocParameterTypes as $paramName => $paramType) { - if (!$closureScope->hasVariableType($paramName)->yes()) { + if ($closureScope->hasVariableType($paramName)->no()) { continue; }
continue;
}

$closureScope = $closureScope->assignVariable($paramName, $paramType, $closureScope->getNativeType(new Variable($paramName)), TrinaryLogic::createYes());
}
$closureScope = $closureScope->processClosureScope($scope, null, $byRefUses);
$closureType = $closureScope->getAnonymousFunctionReflection();
if (!$closureType instanceof ClosureType) {
Expand Down Expand Up @@ -4175,6 +4184,43 @@
}
}

/**
* @return array<string, Type>
*/
private function resolveClosurePhpDocParameterTypes(Node\Stmt $stmt, MutatingScope $scope, Expr\Closure $closure): array
{
$phpDocParameterTypes = [];

foreach ($stmt->getComments() as $comment) {
if (!$comment instanceof Doc) {
continue;
}

$resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
$scope->getFile(),
$scope->isInClass() ? $scope->getClassReflection()->getName() : null,
$scope->isInTrait() ? $scope->getTraitReflection()->getName() : null,
$scope->getFunction() !== null ? $scope->getFunction()->getName() : null,
$comment->getText(),
);

foreach ($resolvedPhpDoc->getParamTags() as $paramName => $paramTag) {
foreach ($closure->params as $param) {
if (
$param->var instanceof Variable
&& is_string($param->var->name)
&& $param->var->name === $paramName
) {
$phpDocParameterTypes[$paramName] = $paramTag->getType();
break;
}
}
}
}

return $phpDocParameterTypes;
}

/**
* @return array{TemplateTypeMap, array<string, Type>, array<string, bool>, array<string, Type>, ?Type, ?Type, ?string, bool, bool, bool, bool|null, bool, bool, string|null, Assertions, ?Type, array<string, Type>, array<(string|int), VarTag>, bool, ?ResolvedPhpDocBlock}
*/
Expand Down
45 changes: 45 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14325.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace Bug14325;

use function PHPStan\Testing\assertType;

/**
* @param list<string> $array
*/
function func(array $array): void
{
assertType('list<string>', $array);
$array[] = 'bar';
assertType('non-empty-list<string>', $array);
}

/**
* @param list<string> $array
*/
$func = function(array $array): void
{
assertType('list<string>', $array);
$array[] = 'bar';
assertType('non-empty-list<string>', $array);
};

class Foo
{
/**
* @param list<string> $array
*/
public function method(array $array): void
{
assertType('list<string>', $array);

/**
* @param list<string> $inner
*/
$closure = function (array $inner): void {
assertType('list<string>', $inner);
$inner[] = 'baz';
assertType('non-empty-list<string>', $inner);
};
}
}
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/count-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ public function constantArrayWhichCanBecomeList(string $h): void
*/
function(\ArrayObject $obj): void {
if (count($obj) === 0) {
assertType('ArrayObject', $obj);
assertType('ArrayObject<int, mixed>', $obj);
return;
}

assertType('ArrayObject', $obj);
assertType('ArrayObject<int, mixed>', $obj);
};

function($mixed): void {
Expand Down
Loading