No Extra Argument
July 2, 2015 ยท View on GitHub
PHP does not check at compile time that a function or method calls has too many arguments. One may call any function with any number of arguments, and the extra arguments will be dropped at execution time.
This is true for PHP native functions too.
<?php
function x($a, $b, $c = 2) { return $a + $b; }
x(1, 2, 3, 4, 5); // two arguments too many
?>
It is recommended to provide only the needed arguments when calling a method or a function.
Rule Details
This rule spots functions and methods calls with arguments than needed.
When the called method makes use of variable number of argument, using func_get_args, func_get_arg or func_num_args, or even the ... operator, the number of acceptable argument is.
The following codes are considered a warning:
<?php
function x($a, $b, $c = 2) { return $a + $b; }
function z($a, ...$b) { return array_sum($b) + $a; }
x(1, 2, 3, 4); // extra argument 4
?>
The following pattern are considered legit:
<?php
function x($a, $b, $c = 2) { return $a + $b; }
function y($a, $b, $c = 2) { return array_sum(func_get_args()); }
function z($a, ...$b) { return array_sum($b) + $a; }
x('a', 'b', 'c', 'd');
y();
z(1, 2, 3, 4);
?>
Further Reading
- func_get_arg
- [func_get_args] (http://php.net/aliases)
- func_num_args
- Variable-length argument lists