Skip to content

CallableParameterUseCaseInTypeContext ​

warning on by default

Group: Architecture · PhpStorm name: CallableParameterUseCaseInTypeContextInspection

A parameter's declared/documented type is a contract. Testing it with an is_*() function that can never succeed (or never fail) for that type is dead logic, and re-assigning the parameter a value of an unrelated type breaks the contract for every later reader of the variable.

Example ​

php
<?php
/** @param int[] $ids */
function pick(array $rows, iterable $feed, $ids, callable $cb, float ...$nums) {
    $ok = is_array($rows) && is_array($feed) && is_callable($cb) && is_object($cb);
    $bad = is_string($rows)
        || is_bool($feed)
        || !is_float($ids)
        || is_float($nums);
    return [$ok, $bad];
}

/**
 * @param mixed  $any
 * @param number $qty
 */
function loose($any, $qty, string $code) {
    return [is_int($any), is_float($qty), is_numeric($code), is_long($code)];
}

interface Shape {}
class Circle implements Shape {}
class Ring extends Circle {}
class Label {}

function redraw(Shape $s, Ring $r = null, int $n = 0, string $txt = '', array $list = []) {
    $s = $s ?? new Circle();
    $r = $r ?? new Circle();
    $r = new Label();
    $n = 2 * $n + 1;
    $n = $n / 2.5;
    $txt = str_replace('a', 'b', $txt);
    $txt = substr($txt, 1);
    $list = explode(',', $txt);
    $list = 'none';
    $s = null;
    $txt .= 5;
}

function untyped($flag = null) {
    $flag = 'on';
}

Reported:

  • line 5: This check is always false for the declared parameter type; is the parameter being reused?
  • line 6: This check is always false for the declared parameter type; is the parameter being reused?
  • line 7: This check is always true for the declared parameter type; is the parameter being reused?
  • line 8: This check is always false for the declared parameter type; is the parameter being reused?
  • line 28: Assigning a value of type \Label does not match the parameter's declared type.
  • line 30: Assigning a value of type float does not match the parameter's declared type.
  • line 34: Assigning a value of type string does not match the parameter's declared type.
  • line 35: Assigning a value of type null does not match the parameter's declared type.

Configure ​

In custos.json:

json
{
  "rules": {
    "CallableParameterUseCaseInTypeContext": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore CallableParameterUseCaseInTypeContext

/**
 * @noinspection CallableParameterUseCaseInTypeContextInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.