ArgumentCountMismatch
error native on by defaultGroup: Probable bugs
Report a call that leaves a required parameter without an argument, and a call to a PHP function or method that passes more arguments than it declares. Since PHP 7.1 a missing argument throws an ArgumentCountError for functions of the project, and PHP's own functions reject both missing and extra arguments.
Example
php
<?php
function greet($name, $greeting) {}
greet('Ada');
strlen('a', 'b');Reported:
- line 3: Pass the required argument $greeting.
- line 4: Remove the extra argument: strlen() accepts at most 1.
Detection
- A function call, method call, static call or object construction whose callee resolves, as listed in D3, to known declarations.
- Arguments bind as PHP binds them: positional arguments fill the parameters in order, named arguments fill the parameter of that name. A
newwritten without parentheses passes no argument. - D3. The declarations a call may bind to:
- a function call: the single declaration of the function the call reaches (the namespaced function, else the global fallback);
- a construction: the constructor the class runs, inherited or its own;
- an instance method call: the method as each class of the receiver's type resolves it, plus the method as every indexed subtype of those classes resolves it, since the object may be any of them;
- a
static::call: the method of the enclosing class and of its indexed subtypes; - a call through a class name,
selforparent: the method that class resolves. Subtypes are not consulted when the method is private or final, or its class is final.
- Too few: every declaration of D3 leaves a parameter that has no default value and is not variadic without an argument. The message names the parameters the nominal declaration leaves unbound.
- Too many: every declaration of D3 is declared by PHP itself (a builtin function, or a method or constructor of a builtin class) or, with the
strictUserArgsoption, by the project in a body that cannot read extra arguments; none has a variadic parameter, and the call passes more positional arguments than the declaration with the most parameters accepts. - A builtin's parameters are those it declares at the configured PHP version.
Exceptions (no report)
- A call that unpacks arguments (
...$args), a first-class callable (f(...)), or a named argument that matches no parameter or is collected by a variadic parameter. - Callees that cannot be resolved: a dynamic name, a receiver of unknown or mixed type, a union receiver whose classes disagree on the parameter list, a method not found (served by
__callor__callStatic), a method declared only by a@methodtag, a method the caller cannot see while the class declares__callor__callStatic, a class whose ancestors are not all indexed, a trait method called through$this,selforstatic. - Uncertain declarations: a function the project declares more than once (conditionally loaded variants), a project function that shares the name of a function PHP declares in some version (a polyfill), a class declared more than once, a constructor in an abstract class, interface or enum.
- An override in a subtype that accepts the call (fewer required parameters, a variadic parameter) makes the call valid.
- Extra arguments to a function or method of the project: PHP allows them, and the callee may read them with
func_get_args(). With thestrictUserArgsoption, still silent when a declaration has no body, or its body callsfunc_get_args(),func_get_arg()orfunc_num_args(), includes another file or evaluates code. - Builtins below PHP 8.0, whose parameter counts are not declared in a form the stubs describe exactly; builtins of extensions not bundled with PHP; and builtins whose stub signature is known to differ from the one PHP enforces (overloaded forms written as one signature, such as
array_keys,strtr,implode,mt_randorstream_filter_append).
Fix
None: the missing value or the intended call cannot be guessed.
PHP versions
All versions. Builtin callees are checked from PHP 8.0, against the signature they declare at the configured version.
Options
| Option | Type | Default | Effect |
|---|---|---|---|
| strictUserArgs | bool | false | Also report extra arguments passed to functions and methods of the project that cannot read them. |
Configure
In custos.json:
json
{
"rules": {
"ArgumentCountMismatch": {
"enabled": false,
"options": {
"strictUserArgs": false
}
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
php
// @custos-ignore ArgumentCountMismatch