Q: how specify PHP version? (not recognizing PHP 7.4 arrow functions)

  • SonarCloud
  • PHP using the “Sonar way” Quality Profile

This php arrow function code:

$func = fn ($param) => $this->myVoidFunc($param);

Is being flagged for rule “The output of functions that don’t return anything should not be used”

  • Rule: php:S3699 link
  • I believe the code is correct PHP arrow function syntax. $func is not being assigned the result of a function call, it is being assigned the function itself.

I don’t see any issue reports in the community about this, so perhaps I’m missing something. Arrow functions were introduced in PHP 7.4, that seems the most likely cause. I have looked and not found documentation about how SonarCloud determines which PHP version to use for the analysis. This question was asked 3 years ago, How to set PHP version?, but the thread does not contain the answer to the question.

Q: What does SonarCloud use to determine which PHP version to use in its analysis?
… and specifically, how can I ensure it uses PHP 7.4?

Thanks

Hey @daryn,

This is indeed a false positive with that rules implementation. I did create SONARPHP-1268 to track it. It is not really related to setting the right PHP version.

There is no such mechanism currently in the PHP analyzer (which might change). The analyzer does parse PHP syntax up to the latest PHP version (8.1 with the latest release). Sometimes, with the addition of new syntax, existing rules have to be adapted. This should have been done for S3699 when arrow functions were introduced.

Thanks very much for reporting this!

Best,
Karim.