DocumentedArgumentType
warning native off by defaultGroup: Probable bugs
Report an argument that breaks a parameter's contract when PHPDoc is taken into account: the @param type of a project function rejects it, or the declared type rejects a value whose type only PHPDoc describes. PHP may run such a call, but the code and its documentation disagree. Mismatches the code alone proves are InvalidArgumentType's.
Example
php
<?php
declare(strict_types=1);
class Order {}
/** @param Order $order */
function archive($order) {}
archive('order-1');
function charge(int $cents) {}
/** @param string $amount */
function bill($amount) { charge($amount); }
/** @param positive-int $quantity */
function restock(int $quantity) {}
restock(0);Reported:
- line 7: Pass Order to $order of archive(), not string.
- line 11: Pass int to $cents of charge(), not string.
- line 15: Pass positive-int to $quantity of restock(), not 0.
Detection
- Calls, argument binding and callee resolution as InvalidArgumentType defines them.
- The type of the argument is the one PHPDoc-aware inference gives:
@param,@varand@returntags count. - Every declaration the call may bind to rejects every value of the argument's type, through the declared type of the parameter or, for a function of the project, its
@paramtype. A null default makes either type nullable. - The weak-mode conversions and the exceptions of InvalidArgumentType apply alike.
- D5. Integer ranges, non-empty strings, literal string values (
'asc'|'desc'), constants (Mode::FAST|Mode::SLOW,Mode::*) and the values a PhpStorm#[ExpectedValues]attribute lists count only against an argument whose possible values are all known (an integer or string literal such as0or'up'); otherwise@param positive-intaccepts any integer and@param 'asc'|'desc'any string. A constant counts only when it is declared with a literal value. - Generic arguments are compared by the variance of each template, also when the argument's class gives them to the documented class through
@extendsor@implements. They count when a declaration states them: a parameter the function does not write, a property of$thisthe function does not write, a function or method whose documented return mentions no template and is notstatic, or a local assigned only from one of those. - Array and object shapes, callable signatures, and arrays of int keys where only string keys are documented are compared.
Exceptions (no report)
- Arguments InvalidArgumentType reports: the declared types reject what the code alone shows.
- The
@paramtype of a variadic parameter, of a parameter of PHP's own functions, a@paramtype naming templates, and a@paramtype the declared type rejects (a stale tag). - The other value details of a
@paramtype: string lengths other than non-empty, literal strings with a backslash, a quote or a$, and every value detail against an argument whose values are not all known (see D5). - Every exception of InvalidArgumentType.
- Values read from a variadic parameter whose
@paramtype is an array or iterable (@param string[] ...$names): the tag may type the collected array or each value, so the values have no PHPDoc type.
Fix
None: either the call or the documentation is wrong.
PHP versions
All versions, as InvalidArgumentType.
Configure
In custos.json:
json
{
"rules": {
"DocumentedArgumentType": {
"enabled": true
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
php
// @custos-ignore DocumentedArgumentType