Having this code:

<?php declare(strict_types = 1);

function FA(string $s) : void
{
    echo($s);
}

FA('Test', '2');

I'm calling the function FA with two arguments but it only expects one.

PHP Tools does not detect this as an error, PHPStan does:

Function FA invoked with 2 parameters, 1 required.

Suggestion

Please also detect wrong number or arguments and notify me about it.

a month later

Thanks!

We're already reporting if there are missing arguments.

We'll add diagnostic for more arguments as well.

7 days later

thank you @UweKeim - we've implemented the diagnostic; will be available in the next prerelease/release.

9 days later

JakubMisek Hi guys!
I just update to the new v 1.17.10641 and in the change log we have
Too many arguments provided to a function call. (#645)
Im not a expert on PHP, but as far as i know for php ITS NOT A ERROR/BUG/PROBLEM to have too many args pass in a function, so im my opinion should not be flag as error.
Ther is a way to turn this function off?
Thx

Variable-length argument lists ¶
PHP has support for variable-length argument lists in user-defined functions by using the ... token.
Note: It is also possible to achieve variable-length arguments by using func_num_args(), func_get_arg(), and func_get_args() functions. This technique is not recommended as it was used prior to the introduction of the ... token.
Argument lists may include the ... token to denote that the function accepts a variable number of arguments. The arguments will be passed into the given variable as an array; for example:
https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list

I Can see that says that its not RECOMMENDED but its possible to use.

Answers 3 : of Why does PHP not throw an error when I pass too many parameters to a function
because PHP functions support variable number of parameters.

for reference: https://www.anycodings.com/1questions/52765/why-does-php-not-throw-an-error-when-i-pass-too-many-parameters-to-a-function

(Sorry for any spelling mistakes, English is not my first language).

    @MarcosText thank you for your feedback!

    You are right, it is not a fatal error. But, if this happens, you have your code obviously wrong - passing unused arguments is definitely an unintended behavior.

    In the upcoming 1.20 we have improved this error for functions, that use variable amounts of arguments.

    If you want to ignore this error, please use the .editorconfig: https://docs.devsense.com/en/vs/code%20validation/configuration#editorconfig

    JakubMisek I got the error bcus one of the old library that is use this way:

    foo (){
      $args = func_get_args();
      "do some stuff"
      return somstuff;
    }
    
    #and we call like that:
    foo($arg1, $arg2, $arg3);

    And it works and dont trow any error as far as i can see.
    Any way will disable the error....
    Thx!

      MarcosText thank you Marcos for the details.

      Yes, we have fixed this and it will be available in the upcoming version 1.20 (or you can already install Pre-Release version with the fix)

        Write a Reply...