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.67k stars 1.48k forks source link

Expected type hint "Datetime"; found "DateTime" #3817

Closed BowFarmer closed 1 year ago

BowFarmer commented 1 year ago

Describe the bug I received this warning from running phpcs --standard=WordPress on a file. Expected type hint "Datetime"; found "DateTime" for $day_to_check

Code sample

class Foo {
    private function dbtn_get_issue_number_from_date( DateTime $day_to_check, int $start_or_end ) {
    }
}

Custom ruleset [Not sure where the problem ruleset is. I'm using the WordPress Coding Standards]

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
  <description>phpcodesniffer4wordpress</description>
</ruleset>

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
    PHPCS output here
    ---------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ---------------------------------------------------------------------------------------
    408 | ERROR | Expected type hint "Datetime"; found "DateTime" for $day_to_check
    ---------------------------------------------------------------------------------------

Expected behavior I expect that the hint ( DateTime $day_to_check ) has to be DateTime and not Datetime.

Versions (please complete the following information):

Additional context Add any other context about the problem here.

jrfnl commented 1 year ago

@BowFarmer This report contains too little information to reproduce the issue.

I suspect the error is the Squiz.Commenting.FunctionComment.IncorrectTypeHint error, but that sniff only kicks in when there is a docblock and the code sample provided does not contain a docblock, making the issue impossible to reproduce.

You can see the sniff code associated with the error message by running phpcs -s test.php --standard=WordPress

If the above sniff is the issue, I suggest you fix up your docblock.

If not, please report back with enough information to reproduce the issue if the error code starts with Generic, MySource, PEAR, PSR1, PSR2, PSR12, Squiz or Zend.

If the error code starts with something else, please report it to whatever external standard the error is coming from.

BowFarmer commented 1 year ago

Hi Juliette,

Thank you for your quick reply. This is what I get when I run phpcs -s --standard=WordPress on the file.

Expected type hint "Datetime"; found "DateTime" for $day_to_check (Squiz.Commenting.FunctionComment.IncorrectTypeHint)

jrfnl commented 1 year ago

@BowFarmer Thanks for confirming, but that means the issue is still not reproducible with the code sample given.

As I suggested before: I believe the typo is in your docblock (the case of the type hint for that same parameter in the @param tag). The error should go away when you fix the docblock.

jrfnl commented 1 year ago

Closing for lack of response. If this is still an issue, please provide the additional information requested and if the issue can then be reproduced, the ticket can be reopened.