开发者

How to tell which PHP files are actually used and which are not? [closed]

开发者 https://www.devze.com 2022-12-21 22:51 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.

Closed 9 years ago.

Improve this question 开发者_StackOverflow

I got a large PHP website which I'm 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?


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

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


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.


Have a look at phpdcd

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.


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.


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.

Our 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消