How can a PHP programmer identify orphaned PHP files or images for sure?

333 Views Asked by At

If a C/C++ programmer wants to tidy out a large C or C++ project, then he can remove some code files (*.c, *.h, *.cpp, ...) that he thinks as possibly orphaned. When he starts the compiler, and it runs without errors, then he was right. When the compiler throws errors, because the removed files are needed, then the programmer knows for sure that a particular code file is really needed by the large project.

We have a large web project here, that was written in PHP. It consists of a lot of PHP files and image files. The problem is that there is no compiler for a large PHP based web project.

One person here read an article 10 or 15 years ago. This article showed a solution to get the same result for a PHP project as a C/C++ programmer with a C/C++ project would get using his compiler. He remembered only the mere fact that this solution exists, but he lost that article and he forgot the solution in detail.

Does anybody know whether such a solution exists? If so, what is the name? Are there URLs? Key words for search engines etc.?

1

There are 1 best solutions below

1
On

An exact solution doesn't exist. The reason is that php is free to do any includes it wishes, based on any output it can produce. It's impossible (literally) do generically determine what that can be.

All is not lost however, because typically php programs doesn't use this, so we can find a set of files that are needed and assume the rest probably aren't. Mostly we will get lucky.

The right google term is static analysis. I haven't used any such tools for php, so can't give you specifics but they do seem to exist.

Another approach would be to make a few test cases covering the functionality of you project and then get a code coverage report from that. This is even more likely to miss stuff, but might still give some good insight in what parts are used.