PHP产品从PHP 5.2迁移到PHP 5.4的工具或方法

Existing product developed in core PHP is built in PHP 5.2 and now we want to migrate the product to PHP 5.4.

CodeSniffer can detect compatibility for version and generate report with warnings and errors.

Current approach could be:

  • Scan whole product and get report from CodeSniffer

  • Prepare a plan to fix each type of warning or error, like replace ereg_replace with preg_replace function and in first argument add delimiters

  • Example:

    Original: $new = ereg_replace(“oldstring“, “newstring“, $old);
    Replace:  $new = preg_replace(“/oldstring/“, “newstring“, $old);
    
  • Make a PHP script to implement the plan

  • Run the script, test with CodeSniffer again and run a test on whole product again

Is there any better tool or approach for migration PHP 5.2 to PHP 5.4?

I have PHPUnit tests for my project. The tests also record all warnings and notices. With PHP5.2 I made sure that had no warnings or notices. Then I upgraded to PHP 5.4 and got lots of warnings and notices, plus many tests failed. Then it was just a matter of fixing all of them. The most common issue that I had: assigning properties of uninitialized objects.

I would collect info about these Warnings or Errors and parse them for a filename and string number on which "failure" occurs and in manual mode (simplest approach) correct all these issues.

Even if project is too big, I think such cases wouldn't be too much, otherwise better think about refactoring than correcting compatibility issues.