Skip to content

Ignore @no-named-arguments in the native type of variadic parameters - #6824

Open
GromNaN wants to merge 1 commit into
phpstan:2.3.xfrom
GromNaN:native-type-ignores-no-named-arguments
Open

GromNaN wants to merge 1 commit into
phpstan:2.3.xfrom
GromNaN:native-type-ignores-no-named-arguments

Conversation

@GromNaN

@GromNaN GromNaN commented Oct 24, 2026

Copy link
Copy Markdown

getNativeType() is documented as returning only what PHP's native type system knows, ignoring PHPDoc. The native type of a variadic parameter does not follow that rule: it uses acceptsNamedArguments(), which comes from the @no-named-arguments PHPDoc tag.

As a result, with treatPhpDocTypesAsCertain: false, PHPStan still reports an @phpstan-assert on such a parameter as redundant, even though the report relies on PHPDoc:

/**
 * @template TItem
 * @param TItem[] $parameter
 * @phpstan-assert list<TItem> $parameter
 */
private static function validateVariadicParameter(array $parameter): void {}

/**
 * @no-named-arguments
 */
public function groupBy(string ...$groupBy): void
{
    self::validateVariadicParameter($groupBy); // reported even with treatPhpDocTypesAsCertain: false
}

The native parameter type now keeps string keys whenever the PHP version supports named arguments, and only the PHPDoc-aware type uses acceptsNamedArguments(). With treatPhpDocTypesAsCertain: false, $groupBy is array<int<0, max>|string, string> for the analyser, the assertion narrows, and nothing is reported. With the default true, the PHPDoc-aware type is still list<string> and the call is reported as before.

The native mirror in turbo-ext/src/MutatingScope.cpp applies the same change. tests/PHPStan/Analyser/nsrt/no-named-arguments.php now expects array<int<0, max>|string, float> from assertNativeType, and a rule test covers the treatPhpDocTypesAsCertain: false case.

The other half of the @phpstan-assert behaviour, method templates resolved to their bound, is in #6823.

@GromNaN
GromNaN force-pushed the native-type-ignores-no-named-arguments branch from 8afa421 to db5b675 Compare October 24, 2026 20:05
The native type of a variadic parameter used acceptsNamedArguments(),
which comes from the @no-named-arguments PHPDoc tag. That contradicts
getNativeType(), which is documented as ignoring PHPDoc, and makes an
@phpstan-assert on such a parameter report as redundant under
treatPhpDocTypesAsCertain: false.

Only the PHPDoc-aware type now uses acceptsNamedArguments(). The native
type keeps string keys whenever the PHP version supports named arguments.
@GromNaN
GromNaN force-pushed the native-type-ignores-no-named-arguments branch from db5b675 to 4bc93c4 Compare October 24, 2026 20:08
@GromNaN
GromNaN marked this pull request as ready for review October 24, 2026 20:11
@phpstan-bot

Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants