PHP for Java (using PtoJ)

I would like to translate our codebase from poorly written PHP code to poorly written Java, as I find it easier to remove Java code. What are the pros and cons, and for those who did this on their own, would you recommend a PtoJ project with around 300k ugly lines of code? Tips and tricks are welcome; thanks!

+7
source share
5 answers

Poorly written PHP will most likely be very difficult to convert, because many bad things in PHP simply do not exist in Java (the same is true and vice versa), so do not accept this because I say that Java is better - I will keep a good watch this fiery war).

If you are talking about an outdated PHP application, it is highly likely that your code contains a lot of procedural code and embedded HTML, none of which are going to convert well to Java.

If you're really unlucky, you'll have things like eval() , dynamic variable names (using the $$ syntax), looped include() statements, register_globals flag dependency, and even worse. Such things will completely hinder any conversion attempt.

Another serious problem is that debugging the result after the conversion will be hellish, even if you have beautiful code to start with. If you want to avoid regressions, you will basically need to go through the entire code base on both sides with a thin comb.

The only time you get a satisfactory result from an automated conversion of this type is if you start with a database with enough tides, written, at least mainly in the updated OOP code.

In my opinion, it would be better for you to do refactoring before the transition. But of course, given your question, this is more likely to win. So my recommendation is to stick with it in PHP. PHP code can be very good, and even bad PHP can be polished with a bit of refactoring.

[EDIT]

In response to a @Jonas question in the comments: โ€œWhat is the best way to reorganize horrible PHP code?โ€

It really depends on the nature of the code. A large monolithic block of code (which describes a lot of bad PHP that I saw) can be very difficult (if not impossible) to implement use tests. You may find that functional tests are the only kind of tests that you can write on the old code base. They will use Selenium or similar tools to run code through a browser, as if it were a user. If you can get a set of robust functional tests, it helps you stay confident that you are not introducing regressions.

The good news is that it can be very easy - and satisfying - to break up bad code and rebuild it.

The way I turned to him in the past is a two-step approach.

The first step rewrites the monolithic code into a procedural code of acceptable quality. This is relatively easy, and the new code can be dropped into place when you go. Here, the bulk of the work happens, but you still get the procedural code. Just the best procedural code.

Stage two: after you receive a critical mass of a reasonable high-quality procedural code, you can again transform it into an OOP model again. This has to wait longer because it is usually quite difficult to convert the old poor quality PHP directly into a collection of objects. This should also be done in fairly large chunks, because you will be moving a large amount of code into objects at once. But if you did a good job in the first stage, then the second stage should be quite simple.

When you hit objects, you can seriously think about unit tests.

+8
source

I would say that the automatic conversion from PHP to Java has the following:

pros

  • quick and dirty, perhaps pleases the project manager associated with short-term delivery (provided that you are lucky and the automatically generated code works without too much debugging, which I doubt)

against

  • Ugly code: I doubt automatic conversion from ugly PHP generates anything other than ugly Java

  • unsupported code: auto-generated code is likely to be unreachable or at least very difficult to maintain

  • Bad approach: I assume you have a PHP web application; in this case, I think that automatic translation is unlikely to use the best Java practices for a web application or available frameworks

Finally

I would avoid automatically translating from PHP to Java, and I will at least consider rewriting the application from scratch using Java. Especially if you have a web application, select a good Java framework for webapps, do a thorough design and proceed with incremental implementation (one feature of your original PHP Webapp at a time). With this approach, you will get cleaner code that will be easier to maintain and develop ... and you can find out that the required time is not so long that you will need to clean / debug the automatically generated code :)

+3
source

Unlike the other answers here, I would agree with your strategy to convert โ€œPHP code to poorly written Java, because I find Java code easier to cleanโ€, but you need to make sure that the tool you use does not introduce any more errors than you can handle.

The optimal strategy would be: 1) Automatic conversion 2) Get MVP with some basic tests 3) Start using the amazing Eclipse / IntelliJ refractoriness tool to make the code more readable.

The modern Java IDE can reorganize code with zero errors when executed correctly. It can also tell you which functions are never called, and many other checks.

I donโ€™t know how โ€œPtoJโ€ was, since their website has disappeared, but ideally you want something that does not just translate the syntax, but the logic. I recently used php2java.com and it worked very well. I also used various "syntax" converters (not only for PHP in Java, but also ObjC โ†’ Swift, Java โ†’ Swift), and even they work fine if you set the time to get everything working after.

Also found this interesting blog entry about what could happen to numiton PtoJ ( http://www.runtimeconverter.com/single-post/2017/11/14/What-happened-to-numition ).

+2
source
+1
source

P2J is now disabled, but I wrote a proof of concept that converts a subset of PHP into Java. It uses the transpiler library for SWI-Prolog:

 :- use_module(library(transpiler)). :- set_prolog_flag(double_quotes,chars). :- initialization(main). main :- Input = "function add($a,$b){ print $a.$b; return $a.$b;} function squared($a){ return $a*$a; } function add_exclamation_point($parameter){return $parameter.\"!\";}", translate(Input,'php','java',X), atom_chars(Y,X), writeln(Y). 

This is the output of the program:

 public static String add(String a,String b){ System.out.println(a+b); return a+b; } public static int squared(int a){ return a*a; } public static String add_exclamation_point(String parameter){ return parameter+"!"; } 
+1
source

All Articles