views:

132

answers:

7

Hello,

I got a large PHP website which Im now about to take care of. It contains hundreds of separate PHP files, but I suspect only less than a half is really being used. Most of them probably can be deleted.

But the last thing I really want to do is going through the code of each file and check whether its linked, included, required...etc. to others or whether it can be safely deleted.

Do you know whether there's any tool which is capable of doing this? It would save me hours of work.

Thanks a lot for any tips.

+7  A: 

Take a look at phpxref, It might do what you need.

Cross-references PHP classes, functions, variables, constants and require/include usage.

Pekka
That won't handle invocations via **eval**. Nor will it handle groups of files that reference one another, but for which there is no reference from outside.
Ira Baxter
A: 

There are multiple ways to achieve this, but all will involve some manual work.

You can probably best install and use a debugger (like Xdebug) that can show you the path the PHP travels as you click through them.

Another way is to write a script that matches on 'include', 'include_once', 'require', 'require_once'. Possibly check for 'eval' and 'fopen', 'file_get_contents' etc too. Make sure you test / backup.

Matt
And I just realized - any href=file.php is also a possible match !So the script / grep approach should take that into consideration too.
Matt
+1  A: 

Have a look at phpcd

phpdcd is a Dead Code Detector (DCD) for PHP code. It scans a PHP project for all declared functions and methods and reports those as being "dead code" that are not called at least once.

But don't expect any wonders from it.

Gordon
+1  A: 

The problem with analyzing the code with an automated tool is that you may find batches of files that link to each other, but the batch of files are never used. Conversely, there may be a file that is accessed directly, but doesn't use other files, nor is it included by any other files.

Typically what I do, in desperation, is add logging to each file. Simple write __FILE__ out to a log file when the file is accessed. This does add overhead across the board. But after a certain length of time, you then have your list of files are are actually accessed and used.

You could also analyze the log file on a regular basis and remove the logging from the files you know are used, reducing overhead as you go. In the end, you can search for files that still have your logging code to see which ones haven't been used.

Brent Baisley
Test coverage basically adds all this logging for you. Automatically. See my answer.
Ira Baxter
A: 

You can use __construct methode and log it if it is used. You can't set the __construct methode after creating an instance. So add them manually;

class Someclass{
private $clsName;
public function __construct(){
$this->clsName = get_class($this);
YourStaticLogger::yourlogFunction("whatever you want to log" . $this->clsName);
}
//other things
}

You can only track the called classes. That's what i would do.

eyurdakul
This is the same answer as "add logging to each file".
Ira Baxter
nice but it won't work for abstract classes and parent classes that are extended by child classes since the child class will override the __construct methode of the parent class and there will only one __construct run for each instance.
eyurdakul
A: 

Try this one also: http://php.net/inclued

Shein Alexey
A: 

If you use a test coverage tool, it will tell which ones are definitely used, for whatever functionality of the software you have exercised. (Obviously, the more you exercise the software, the more of it gets executed by the non-dead part.). This includes any file accessible via an external html link; of course, you have to exercise that link, as it is part of your application functionality.

Then you can inspect the ones it says are not used do decide what the case really is.

The SD PHP Test Coverage tool will accept a list of all the files you wish to check out, and enable you to easily collect such test coverage data. It provides a summary report showing which files have any coverage at all; those with 0% coverage are the ones that are likely dead.

Ira Baxter