Skip to content

DocumentedArgumentType ​

warning native off by default

Group: 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, @var and @return tags 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 @param type. 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 as 0 or 'up'); otherwise @param positive-int accepts 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 @extends or @implements. They count when a declaration states them: a parameter the function does not write, a property of $this the function does not write, a function or method whose documented return mentions no template and is not static, 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 @param type of a variadic parameter, of a parameter of PHP's own functions, a @param type naming templates, and a @param type the declared type rejects (a stale tag).
  • The other value details of a @param type: 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 @param type 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

Released under the MIT License. Native rules and rules modeled on Php Inspections (EA Extended); independent clean-room implementation.