0

I'm faced with the challenge of refactoring and adding new features to a legacy PHP web "application", it consists of about 788 php scripts, which have no folder structure, they are in the most part scripts which contain JavaScript, PHP, HTML, and CSS code in the same file, the scripts persist and use data from 3 different database types, MSSQL, MYSQL and DB2, there authentication is session based, most of it it's basic php and there's also some pages which use pure html and ajax to communicate to the php scripts, needless to say there is no class definitions, on the most part not even functions.

My goal is to translate this into a more maintainable application most likely using symfony components to which I'm familiar, introduce HTTP request abstraction, routing and if possible MVC, also refactor the code into usable classes and implement auto loading using composer to take advantage of 3rd party libraries.

Frankly the only option in my mind is to ditch all of it and start a new project.

Julio Villalba
  • 149
  • 1
  • 2
  • 17
  • 1
    "Frankly the only option in my mind is to ditch all of it and start a new project." sounds like a plan to me –  Nov 16 '16 at 03:14
  • 1
    I just wanted to be sure that there might be no other viable option, it's a pain even attempting to read it. – Julio Villalba Nov 16 '16 at 03:31
  • 2
    well there is no magic "fix shit php" function. –  Nov 16 '16 at 03:39
  • Since, I hope you found a solution... The state of this legacy application is quite classic and suitable by the processus of refactoring demonstrated by the excellent book "Modernizing Legacy Applications In PHP" by Paul M. Jones. I suggest to clean up before thinking on migrating all to a framework – Michel May 08 '20 at 15:16

3 Answers3

1

I think that wrapping your legacy application inside a symfony controller could be a nice solution.

https://www.youtube.com/watch?v=WbBf4p4FcqA&t=348s

https://stovepipe.systems/post/migrating-your-project-to-symfony

So you

  • keep application legacy code working
  • you can write functional tests
  • implement new features or port some piece of your legacy code into symfony
Francesco
  • 443
  • 1
  • 8
  • 16
0

Even if rewriting all seems the best idea, sometimes it's not the best business choice and could be quite hard for huge codebase.

One solution could be to introduce a Symfony project and start refactoring module by module, but you will mainly face the following problems :

  • Web server intégration : how to start a symfony application alongside a legacy application (with custom nginx location for example)
  • Share session between the 2 applications, but symfony already has something to bridge a legacy Application with Symfony Sessions,
  • Have a consistent web design, which is many work but not something technically hard
  • I guess many others problems
LFI
  • 654
  • 5
  • 11
  • I'm actually aware about the business drawbacks of rewriting, one of the biggest flaws I encounter is using the session bridge would require to not end the session in the legacy application otherwise it does not work properly for me, I'm currently refactoring step by step and adding a bit more stability since it's plan to move it to another server and there are many system dependent paths and the old mysql_connect will be replaced by PDO, my plan is put a little work into it, make it less prone to falling apart and then build something new, once I have more understanding about the business. – Julio Villalba Nov 16 '16 at 17:47
  • I would say the project is small, but the main database might have to be restructured, as there are unused fields and tables everywhere. – Julio Villalba Nov 16 '16 at 17:50
  • Each time I face the same problem, I ended by rewriting the legacy code module by module. You have others options than integrating SF2 or other framework inside the legacy codebase: split by application (frontend/backend), use micro service, create clean libs. – LFI Nov 17 '16 at 07:20
  • Last point, not test myself, but maybe Laravel is easier to integrate in this case – LFI Nov 17 '16 at 07:21
  • I'd like to use symfony just because I'm more familiar with it, besides laravel is kind of already very centered on MVC while symfony is really pick what you want since it's all components. – Julio Villalba Nov 17 '16 at 14:58
0

In 2019+ you can use Instant Refactoring

I'm an author of Rector, tool that can migrate huge amount of PHP files in few seconds. E.g. upgrade PHP 5.3 to PHP 7.4, upgrade Symfony 2.8 to 4.2 or migrate from Nette to Symfony (read the case study).

Spaghetti to MVC set is one of the future features (already few projects are interested). But features mentioned above are supported out of the box, but you can also add your own rules that migrate your specific code.

Read more on Github repository.

Tomas Votruba
  • 23,240
  • 9
  • 79
  • 115