I am building a testing tool to execute on directories of legacy PHP code that is extremely difficult to unit test, although I am using PHPUnit for this tool.
One of the cool things about PHPUnit is that it will bubble up PHP notices, warnings, and errors into an exception. Using that bit of knowledge I’ve built a tool to recursively include() files from a directory in a try/catch block and log any exceptions.
The problem is, this can quickly become a memory hog and crash if I include hundreds of files. I’ve considered using this but I’m not sure if this “clears” it from memory:
// Include file into buffer
ob_start();
include($file);
// Clear file from buffer
ob_clean();
What is the best way to handle something of this nature and manage memory/resources appropriately?
[EDIT]
Also, this is an internal tool that I am developing to help manage the thousands of files containing untestable (in a time constraint sense) legacy procedural code.
What about this as a thought?
// Enable garbage collector (in case this helps?)
gc_enable();
// Get procedural php and execute it
$fileData = file_get_contents($file);
eval($fileData);
// Clear variable
$fileData = null;
What if you create a separate CLI script (the “tester”) that will include one file at a time and log the exceptions, and have your main script (the “iterator”) iterate through / recurse into the directories of scripts you need to test and invoke the tester script via the shell for each file?