squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.66k stars 1.48k forks source link

Doesn't report invalid argument count #3860

Closed momala454 closed 1 year ago

momala454 commented 1 year ago

Describe the bug

It doesn't detect if the argument count is not correct

Code sample

<?php

declare(strict_types=1);

function hello(int $a): void
{
}

hello(1, 2);

print_r('1', true, 3, 4);

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
    ----------------------------------------------------------------------
    FOUND 2 ERRORS AFFECTING 2 LINES
    ----------------------------------------------------------------------
    2 | ERROR | Missing file doc comment
    5 | ERROR | Missing doc comment for function hello()
    ----------------------------------------------------------------------

Expected behavior

print_r line does crash: Uncaught ArgumentCountError: print_r() expects at most 2 arguments, 4 given The hello(1,2); line doesn't crash, but one could want to detect it too (via an option)

Versions (please complete the following information)

Operating System windows 10
PHP version 8.1.20
PHP_CodeSniffer version 3.7.2
Standard Custom
Install type [e.g. Composer (global/local), PHAR, PEAR, git clone, other (please expand)]

Please confirm:

jrfnl commented 1 year ago

@momala454 This is not a bug, but a feature request.

Having said that, PHPCS is not typically suitable for those type of checks as it doesn't have awareness of the functions available and their requirements (the code being scanned does not get loaded in memory).

While it would be possible to create a sniff for this for the PHP native functions (using reflection), this would still not satisfy the feature request as the check would not be able to check calls to non-PHP native functions.

You may be better off with Psalm or PHPStan for a check like that.

momala454 commented 1 year ago

Ok thanks i'll try asking to intelephense dev if they can add this

jrfnl commented 1 year ago

Marking this as a duplicate of #2466 and closing.