Identifying type juggling issues before PHP upgrade to PHP8

Viewed 222

Currently, I am sitting on PHP5.6 and would be updating to PHP8 soon. I used PHPCS to check the compatibility issues and was able to fix them before upgrading to PHP8.

However, I found some issues related to type juggling in the code during testing such as:

$x = "";
$x['test'] = "xyz";
print_r($x); //Warning: Only the first byte will be assigned to the string offset in /tmp/8dw2cmkrbq0pcq/tester.php on line 4 Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /tmp/8dw2cmkrbq0pcq/tester.php:4 Stack trace: #0 {main} thrown in /tmp/8dw2cmkrbq0pcq/tester.php on line 4

This piece of code worked well in PHP5.6.

Unfortunately, the issue was not detected by PHPCS and now I am wondering if there are more issues like this in the product.

Now, I want to know if there is a way to detect such issues before I upgrade to PHP8? It would be really helpful if I could get a hint on how to find such issues before I upgrade to PHP8.

1 Answers

I was able to detect this type of issues using https://github.com/phan/phan. Here is the sample issue that was caught.

{
"type": "issue",
"type_id": 10030,
"check_name": "PhanTypeMismatchDimAssignment",
"description": "TypeError PhanTypeMismatchDimAssignment When appending to a value of type '', found an array access index of type 'hobbies', but expected the index to be of type int",
"severity": 5,
"location": {
    "path": "PQR/models/XYZModel.php",
    "lines": {
        "begin": 171,
        "end": 171
    }
}}

Thank you for your support guys.

Related