DEV Community

Christophe Avonture
Christophe Avonture

Posted on • Updated on


Docker - Run/Refactor PHP code from a web interface

As a loyal reader of several forums for years, I frequently find unreadable PHP code posted by beginners asking for help. Before we can try to help them, it is sometimes useful to reformat the code and rewrite it partially. This can take times...

To execute the code to see where the problem is before fixing it, I have been using the Run-PHP-Code tool for a few years but it does not meet the need for reformatting (php-cs-fixer / php code-beautifier) or refactoring (rector). Second disadvantage here (since I don't trust the PHP code), Run-PHP-Code is running on my localhost i.e. on my machine and that can represent a risk (creation/deletion of files, ...). The ideal is to execute the code in a sandbox.

It had been a while since I wanted to do it: I took some hours to take the code of Run-PHP-Code to make his own Docker image, based on PHP 7.4.26 (version 1.0) or 8.1 (version 1.1) and thus to allow to run it in a secure environment.

I also took the time to add a "refactor" button which then calls php-cs-fixer, phpcbf and Rector.

The result is this ready-to-use Docker image:

In only two CLI commands, you will have a web interface with an editor where, on the left side, you can copy/paste or type a PHP code and on the right side visualize the result of its execution (button Run) or its refactoring (button Refactor); the whole running in a Docker container and thus isolated from your machine.

Pretty easy and helpful when trying to give support on forums.

Run and refactor php from an isolated environment

Top comments (1)

deadpool2022 profile image

Thank you for your article🔥