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
55 changes: 55 additions & 0 deletions src/Type/Php/ArrayReduceCallbackClosureTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\Native\NativeParameterReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Type\ClosureType;
use PHPStan\Type\FunctionParameterClosureTypeExtension;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;

#[AutowiredService]
final class ArrayReduceCallbackClosureTypeExtension implements FunctionParameterClosureTypeExtension
{

public function isFunctionSupported(FunctionReflection $functionReflection, ParameterReflection $parameter): bool
{
return $functionReflection->getName() === 'array_reduce' && $parameter->getName() === 'callback';
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, ParameterReflection $parameter, Scope $scope): ?Type
{
$args = $functionCall->getArgs();

if (!isset($args[0])) {
return null;
}

$arrayType = $scope->getType($args[0]->value);
$valueType = $arrayType->getIterableValueType();

if (isset($args[2])) {
$initialType = $scope->getType($args[2]->value);
} else {
$initialType = new NullType();
}

$carryType = $initialType->generalize(GeneralizePrecision::templateArgument());

return new ClosureType(
[
new NativeParameterReflection('carry', false, $carryType, $parameter->passedByReference(), false, null),
new NativeParameterReflection('value', false, $valueType, $parameter->passedByReference(), false, null),
],
new MixedType(),
);
}

}
59 changes: 59 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-7280.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug7280;

use function PHPStan\Testing\assertType;

// Test 1: Precise carry type from initial argument with constant array shape
$result1 = array_reduce(
['test1', 'test2'],
static function (array $carry, string $value): array {
assertType("array{starts: array{}, ends: array{}}", $carry);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this type is wrong. it only is correct for the first invocation when initial is used.

in this example, I think we need to aim for `assertType("array{starts: array, ends: array}", $carry);

$carry['starts'][] = $value;
$carry['ends'][] = $value;

return $carry;
},
initial: ['starts' => [], 'ends' => []],
);

// Test 2: Arrow function with precise carry type
$result2 = array_reduce(
[1, 2, 3],
static fn (int $carry, int $value): int => $carry + $value,
0,
);
assertType('int', $result2);

// Test 3: Carry type with no initial (defaults to null)
$result3 = array_reduce(
[1, 2, 3],
static function (?int $carry, int $value): int {
assertType('null', $carry);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assertType('null', $carry);
assertType('int|null', $carry);

return ($carry ?? 0) + $value;
},
);

// Test 4: Initial value type narrows carry - literal string initial
$result4 = array_reduce(
['a', 'b', 'c'],
static function (string $carry, string $value): string {
assertType('string', $carry);
return $carry . $value;
},
'',
);
assertType('string', $result4);

// Test 5: Carry type with literal int initial
$result5 = array_reduce(
[1, 2, 3],
static function (int $carry, int $value): int {
assertType('int', $carry);
return $carry + $value;
},
0,
);
assertType('int', $result5);
Loading