3

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?

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Kulin Choksi
  • 761
  • 1
  • 11
  • 30

2 Answers2

1

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.

Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
  • I think, manual mode may be simplest but for large project wouldn't be feasible. Instead, making a script could be better approach. In both ways, we will test the project. – Kulin Choksi Dec 25 '12 at 14:45
  • I think, here refactoring means, you want to suggest customised functions instead of built-in functions or features, which could be good solution for long time or for further versions but it could lower the performance (please correct me, if I've missunderstood) – Kulin Choksi Dec 25 '12 at 14:49
  • @CoolZeroInfinity, under refactoring I mean real refactoring of the project. Bec. if most of the part is not able to work with particular version of engine correcting project is not effective, bec. anyway it will lead to low performance. In this situation it is better to think about refactoring. Regarding functions, sometimes new ver. of function can lead to revision of logic what is actually again, a performance issue. – Paul T. Rawkeen Dec 26 '12 at 08:07
  • @CoolZeroInfinity, and one more thing about automation of corrections ... think about! It looks like one more application. Compare possible time spent on refactoring (correction) within the time that can be spent on writing this, _narrow purpose_ application and decide about effectiveness of these things. – Paul T. Rawkeen Dec 26 '12 at 08:10
  • I'm not sure I get you about refactoring. I think for most of the cases it would be as simple as mentioned in example of this question. Can you please explain with example? – Kulin Choksi Dec 27 '12 at 06:06
  • 1
    @CoolZeroInfinity, I mean changes in versions like `DateTime` class for example. Previously there was no `DateTime`, now it exists and it is recommended to use _this_ class for computing dates. Considering this, `strtotime` is _kinda_ deprecated and less accurate than new proposed class and it (_strtotime_) has known issues/limitations. With `ereg_replace` everything is simple, change function name and add slashes to the expression, but considering situation described above - simple _replace_ will not help and it leads not to correction of code, but to it's refactoring. – Paul T. Rawkeen Dec 27 '12 at 10:25
  • Ok, agreed, so we need to do refactoring when pattern replacing won't work. – Kulin Choksi Dec 27 '12 at 11:30
1

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.

Artem Goutsoul
  • 733
  • 5
  • 17
  • The same warning, notices or errors we can get with CodeSniffer without even building PHPUnit tests. Detecting issues is fine but major issue is to fix them by automation. I'm seeking for some proven/successful tool/script or better approach for that. Can we fix the issues with PHPUnit or is there any advantage of using it over my suggested approach for this case? – Kulin Choksi Dec 25 '12 at 14:55
  • 1
    I use both CodeSniffer and PHPUnit all the time, and they have slightly different purposes. CodeSniffer mostly detects stylistic issues, and just a few basic logical issues. Using a testing framework like PHPUnit will actually run pieces of your code and will really let you see if something breaks with the PHP version change. – Artem Goutsoul Dec 25 '12 at 18:32
  • Ok,I agree, codesniffer would detect only syntactical errors but with phpunit can test semantics also which we will need to do manually in current case but it would be better to design unit test first which will help in future also. – Kulin Choksi Dec 27 '12 at 04:40