Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find unused functions in a PHP project

Tags:

php

How can I find any unused functions in a PHP project?

Are there features or APIs built into PHP that will allow me to analyse my codebase - for example Reflection, token_get_all()?

Are these APIs feature rich enough for me not to have to rely on a third party tool to perform this type of analysis?

like image 451
Stacey Richards Avatar asked Aug 14 '08 19:08

Stacey Richards


People also ask

Where are unused methods in eclipse?

UCDetector (Unnecessary Code Detector) is a eclipse PlugIn tool to find unnecessary (dead) public java code. For example public classes, methods or fields which have no references.

Where are unused functions in Visual Studio code?

It is currently not possible to detect unused public methods in VSCode (you can check for updates here). However, if it's a private method, you can mark it as private and VSCode is able to look for whether it is used or not within the scope (although do remember: methods used in the HTML template are public).

Where are unused methods in Xcode?

Xcode has a number of settings you can enable to warn you about things like unused functions, parameters, and values. You can also easily enable strict warnings by setting your Other Warning Flags to -Wall -Wextra -Weverything . Another option for detecting unused code is by using Code Coverage.

How do you find unused functions in Python?

In Python you can find unused code by using dynamic or static code analyzers. Two examples for dynamic analyzers are coverage and figleaf . They have the drawback that you have to run all possible branches of your code in order to find unused parts, but they also have the advantage that you get very reliable results.


1 Answers

You can try Sebastian Bergmann's Dead Code Detector:

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.

Source: https://github.com/sebastianbergmann/phpdcd

Note that it's a static code analyzer, so it might give false positives for methods that only called dynamically, e.g. it cannot detect $foo = 'fn'; $foo();

You can install it via PEAR:

pear install phpunit/phpdcd-beta 

After that you can use with the following options:

Usage: phpdcd [switches] <directory|file> ...  --recursive Report code as dead if it is only called by dead code.  --exclude <dir> Exclude <dir> from code analysis. --suffixes <suffix> A comma-separated list of file suffixes to check.  --help Prints this usage information. --version Prints the version and exits.  --verbose Print progress bar. 

More tools:

  • https://phpqa.io/

Note: as per the repository notice, this project is no longer maintained and its repository is only kept for archival purposes. So your mileage may vary.

like image 166
Gordon Avatar answered Sep 20 '22 04:09

Gordon