Search code examples
phpimportcompilationincluderequire

Is there any way to find out every isolated PHP file in a project?


I'm working on legacy PHP based E-cormmerce project, and there are so many files that are not being used.

I just want to remove every PHP file that are not included or required by another file.

However, PHP supports something like runtime dynamic import. This makes it hard to find out every file.

So I'm wondering if there is a good way or even idea to find out every isolated PHP file in a project.

I tried to use grep, but it does not work well because the PHP source uses dynamic include a lot.


Solution

  • You could try to find this out in a few steps, assuming you're using Linux:

    1. Note down the time on your server.
    2. Try to use all the functionalities of the project.
    3. Use ls -l --time=atime to find all the files that were read after you started with step 2. These all the files that are being used.
    4. Remove all unused files.
    5. Now when you run the code and a file is missing you have to add it back in.

    It's not a perfect method, but it doesn't rely on the PHP code at all, which can be an advantage.