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
1 change: 1 addition & 0 deletions resources/constantToFunctionParameterMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@
'constants' => [
'ARRAY_FILTER_USE_KEY',
'ARRAY_FILTER_USE_BOTH',
'ARRAY_FILTER_USE_VALUE',
],
],
],
Expand Down
5 changes: 4 additions & 1 deletion src/Type/Php/ArrayFilterFunctionReturnTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,10 @@ private function determineMode(?Expr $flagArg, Scope $scope): ?int
return null;
}

if ($flagValues[0] === $this->getConstant('ARRAY_FILTER_USE_KEY')) {
// ARRAY_FILTER_USE_VALUE (PHP 8.6+) is 0, the default mode
if ($flagValues[0] === 0) {
return self::USE_ITEM;
} elseif ($flagValues[0] === $this->getConstant('ARRAY_FILTER_USE_KEY')) {
return self::USE_KEY;
} elseif ($flagValues[0] === $this->getConstant('ARRAY_FILTER_USE_BOTH')) {
return self::USE_BOTH;
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15372.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php // lint >= 8.6

declare(strict_types = 1);

namespace Bug15372;

use function PHPStan\Testing\assertType;

/**
* @param list<int|null> $list
*/
function doFoo(array $list): void
{
assertType('array<int<0, max>, int>', array_filter($list, static fn ($item): bool => $item !== null, ARRAY_FILTER_USE_VALUE));
assertType('array<int<0, max>, int>', array_filter($list, static fn ($item): bool => $item !== null, mode: ARRAY_FILTER_USE_VALUE));
assertType('array<int<0, max>, int>', array_filter($list, static fn ($item): bool => $item !== null, 0));
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
parameters:
phpVersion: 80500
phpVersion: 80600

@staabm staabm Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why do we need to adjust pre-existing tests? we shouldn't do that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The map test resolves every constant of constantToFunctionParameterMap.php through reflection for this phpVersion, and ARRAY_FILTER_USE_VALUE only exists as of PHP 8.6, so with 80500 it fails with Constant ARRAY_FILTER_USE_VALUE (used in array_filter($mode)) not found in reflection. With 80600 every existing entry is still checked and passes.

If you'd rather keep this file untouched, I could make the test skip constants that don't exist for the configured version instead. Which do you prefer?

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Functions;

use PHPStan\Rules\FunctionCallParametersCheck;
use PHPStan\Rules\NullsafeCheck;
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
use PHPStan\Rules\Properties\PropertyReflectionFinder;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<CallToFunctionParametersRule>
*/
class CallToFunctionParametersRulePhp86Test extends RuleTestCase
{

protected function getRule(): Rule
{
$broker = self::createReflectionProvider();
return new CallToFunctionParametersRule(
$broker,
new FunctionCallParametersCheck(
new RuleLevelHelper(
$broker,
checkNullables: true,
checkThisOnly: false,
checkUnionTypes: true,
checkExplicitMixed: true,
checkImplicitMixed: true,
checkBenevolentUnionTypes: false,
discoveringSymbolsTip: true,
),
new NullsafeCheck(),
new UnresolvableTypeHelper(),
new PropertyReflectionFinder(),
$broker,
checkArgumentTypes: true,
checkArgumentsPassedByReference: true,
checkExtraArguments: true,
checkMissingTypehints: true,
),
);
}

public function testBug15372(): void
{
$this->analyse([__DIR__ . '/data/bug-15372.php'], [
[
'Constant SORT_REGULAR is not allowed for parameter #3 $mode of function array_filter.',
12,
],
]);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/data/call-to-function-php86.neon',
];
}

}
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-15372.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug15372;

function (array $a): void {
array_filter($a, fn ($v) => $v > 1, ARRAY_FILTER_USE_VALUE);
array_filter($a, fn ($v) => $v > 1, mode: ARRAY_FILTER_USE_VALUE);
array_filter($a, fn ($k) => $k > 1, ARRAY_FILTER_USE_KEY);
array_filter($a, fn ($v, $k) => $v > $k, ARRAY_FILTER_USE_BOTH);
array_filter($a, fn ($v) => $v > 1, SORT_REGULAR);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
phpVersion: 80600
Loading