Missing Param Type
ID |
php.missing_param_type |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Php |
Tags |
code_smell, type-safety |
Description
Reports parameters of a named function or method that have no type declaration. Since PHP 7.0
every parameter can declare a scalar, class, nullable (?int) or union (int|string) type, and
an undeclared parameter leaves the function’s contract weaker than it needs to be.
Rationale
A parameter without a declared type forces every reader and every caller to infer what the function expects, and it disables the engine’s own argument-type checks: a mismatched value is only discovered when it triggers a fault deep inside the body. Declaring the type documents the contract, lets the runtime reject bad calls at the boundary, and unlocks accurate static analysis.
Variadic (…$rest) and by-reference (&$value) parameters are flagged too, because they carry
values that benefit from a declared type just like ordinary parameters. Closures and arrow
functions are not inspected, and the magic methods whose signatures PHP fixes
(get, set, call, callStatic, isset, unset) are exempt.
<?php
function discount($price, float $rate) // FLAW — $price has no type declaration
{
return $price * (1 - $rate);
}
function total(float $price, float $rate): float // OK — every parameter is typed
{
return $price * (1 + $rate);
}
Remediation
Add the most specific type the parameter accepts. Use a scalar (int, float, string,
bool), a class or interface name, a nullable type (?Type) when null is allowed, or a union
(int|string) when several types are valid.