View unused (unrelated and not included) PHP files in Eclipse

I have a rather large Ecplise project with more than 2000 php files, and I suspect there are a lot of them that are not used.

I would like to create a list of all php files that are not mentioned in any other file. With a mention, I mean things like:

  • include('file.php')
  • require_once('file.php')
  • $linktoimportantpage = "file.php"

So, almost all files that do not contain a string of any other file name in this project.

Is there any way to do this?

+4
source share
4 answers

This script that I wrote should do what you are looking for. It will search for all PHP files in your project. Then it will search the contents of each file for that file name, if the file name exists, it is obvious that you do not want to delete it, so we can remove it from the array. Then you are left with an array of files that are not mentioned in your project (the array will also contain the path to the file).

First configure the script:

 $path = realpath('/path/to/your/files'); $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); $php_files = array(); 

Then run the loop by adding each file name to the array so that we can search later:

 foreach ($objects as $name => $object) { // only add php files to the array if($object->getExtension() !== 'php') { continue; } $php_files[$name] = $object->getBasename(); } 

Repeat the loop, but this time find each file:

 foreach ($objects as $name => $object) { $path_parts = pathinfo($name); // again, only search php files to the array if($path_parts['extension'] == 'php') { // get the contents of each php file $file_contents = file_get_contents($name); // check each file name for an include foreach($php_files as $path => $filename) { // check if the file exist in this file contents if(strpos($file_contents, $filename) !== false) { // remove it from array if it exists in a file unset($php_files[$path]); } } } } 

Print the array to see which files remain unchanged:

 print_r($php_files); 

This returns an array as follows:

 Array ( [/path/to/file1.php] => file1.php [/path/to/file2.php] => file2.php [/path/to/file3.php] => file3.php ) 
+3
source

I'm not sure if you can do this in Eclipse (the PHP plugin is just not so powerful at the moment).

You can try some source analyzer tools. Take a look at this related Q & A , for example:

+1
source

I created a short script that will grep for your project for unused php file import.

Usage: /path/to/file.sh application/directory

Make sure the script has execute permission; for example: chmod 777 /path/to/file.sh

 #!/bin/bash MYPATH=$1 find "$MYPATH" -name \*.php -exec basename {} \; > /tmp/patterns for p in $(cat /tmp/patterns); do grep -R $p "$MYPATH" > /dev/null || echo $p; done 
0
source

Just ahead: you did not indicate in your question which PHP Eclipse plugin you are talking about, so I assume you are talking about PDT.

As far as I know - and it is a little rusty, because now I use PHPStorm - it is impossible for PDT to include deleting files from the box.

This is also true for everything that static code analysis does, because many things cannot be resolved without a run-time context. For the examples you give:

  • include('file.php') and require_once('file.php') contain links to files related to the working directory and the inclusion path. Both are unknown during static code analysis. Most tools can extract the inclusion path from the project configuration; in PDT, this is usually the build path. The working directory problem is most often "solved" by simply expecting the working directory to be the directory in which the file containing these include directives is included, but this should not be true.

  • $linktoimportantpage = "file.php" will need to track the evaluation of the variable value. This may be possible with static code analysis, but only very advanced tools will do it. I suspect PDT really supports this.

Since the appearance seems rather limited for static code analysis, to get a list of files used, I personally would do a regular expression search based on a file name template. Here, all files end with .php , often used in a string, so the regular expression to search for such places:

 \.php['"] 

Eclipse PDT supports regular expressions in finding files from the GUI. Currently - even I use a different IDE mainly - I would personally do this on commanline because it is more flexible. This can be done using the find and grep commands. They come with their help pages and are easy to use. If you use windows, you can install git - bash for windows with bash, which contains all the useful command line tools for such things.

If you need a better experience, you can run the application and control everything, including analyzer software called inclued . This is the PECL package, and it is documented in the PHP manual: http://php.net/inclued - it is fairly straightforward and does not have the drawbacks of static code analysis tools, as it processes runtime information.

0
source

Source: https://habr.com/ru/post/1500951/


All Articles