PHPCompatibility / PHPCompatibility

PHP Compatibility check for PHP_CodeSniffer

Home Page:http://techblog.wimgodden.be/tag/codesniffer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Passing objects to round() as of PHP8 not getting detected

hchonov opened this issue · comments

Bug Description

Since PHP8 the num parameter of the round() function no longer accepts internal objects which support numeric conversion. In our case we were passing an object of type SimpleXMLElement to round() which was not detected by the rules for PHP 8 comparability.

Given the following reproduction Scenario

The following code that used to pass SimpleXMLElement to round() was not detected as false in our case:

$svg_xml = simplexml_load_file($image_path);
$width = $svg_xml->attributes()->width;
round($width);

which was then throwing an Error:

TypeError: round(): Argument #1 ($num) must be of type int|float,

This could've been detected though since the ::attributes() returns an SimpleXMLElement object with attributes that are also of the same type.

@hchonov This is not typically the type of check which PHPCompatibility is suitable to detect. PHPCompatibility does not have access to the runtime value of variable, so also doesn't have access to the type.

attributes() can return SimpleXMLElement or null, the width attribute (property) may or may not exist and can be of any XML supported types (can't be determined by examining the code).

In general, if a check would more often than not lead to a false positive, it is not suitable for PHPCompatibility and IMO, this is one of those cases.