In PHP 7 onwards, primitives can be used as type hints as well as class names and array, callable. For example:

public function sliceCheese(int $slices, string $cheeseType) : string {
  // Implementation.
}

Currently coder sniffer doesn't appear to support these types as hints.

I have attached a patch that is very basic but does allow for this. It's probably going to need a bit of cleaning up though, because this isn't the best way of implementing this, but it's hard for me without detailed knowledge of the sniffer code.

Comments

chriscohen created an issue. See original summary.

chriscohen’s picture

Issue summary: View changes
StatusFileSize
new1.23 KB
chriscohen’s picture

StatusFileSize
new2.05 KB

Sorry, noticed a bug in that the previous patch REQUIRED you to put the primitive type hint in place. This patch makes that optional but permissible.

klausi’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Makes sense! Can you file a pull request against https://github.com/klausi/coder so that we see tests and coding standards are passing?

You can also look at upstream https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Sta... because they also take care of 'integer' for example. I don't like the PHP version check, so we can ignore that.

Can you also add an example test function to good.php?

chriscohen’s picture

I really am truly sorry but my workload does not permit me to continue work on this issue: I felt it was important to report it and I filed what code I have, so hopefully this will be enough to get started on a fix here?

  • klausi committed b08682b on 8.x-2.x
    fix(FunctionCommentSniff): Allow PHP 7 primitive type hints in functions...
klausi’s picture

Status: Needs work » Fixed
Issue tags: -Needs tests

Changed the patch a bit and added a test. Thanks for reporting!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.