G
G
gregorypetrov2017-12-04 16:34:56
PHP
gregorypetrov, 2017-12-04 16:34:56

How to analyze the code of a large PHP project for unused pieces of code and files?

There is a project in PHP (more than 400 files). Changes were made to the code, but code that was no longer needed was not always removed.
Some of the php files with classes are not connected anywhere else, and some of the functions are not called anywhere else.
Are there any automatic search tools for:
-php files that are not included anywhere else and are not used through class autoloading
- individual functions / methods inside the php file that are not called anywhere else
so that you can delete these files and parts without consequences code?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
H
Hated, 2017-12-05
@gregorypetrov

In PhpStorm - right click on the project folder, Inspect Code, Whole Project, Section Unused.

A
Adamos, 2017-12-05
@Adamos

If the files in the project are connected exclusively via PSR - analyzers may help.
But PHP does not prevent anyone from connecting them through the remote depths of asses, like templates and localization in Bitrix, for example. Not a single robot picks up these jungles in life.
So try analyzers, of course. But do not expect that, based on the results of the analysis, you can simply take everything selected and painlessly delete it. You still have to think with your head.
For example, the same Storm allows you to quickly search for a string throughout the project. And if the name of the class does not occur anywhere except in the file with this class - yes, perhaps it is a candidate for deletion ...

I
Igor Kopyrin, 2017-12-14
@kopyrin

There are a number of other useful tools that can be useful for testing code quality:
PHPDCD - Dead Code Detector (DCD) for PHP. It scans the PHP project for all unused functions and methods and reports them.
$ composer global require 'sebastian/phpdcd=*'
$ sudo ln -s ~/.composer/vendor/bin/phpdcd /usr/local/bin/phpdcd Check
example:
project_directory$ phpdcd .
PHPMD - PHP Mess Detector. Helps to find potential problems in the code, such as possible errors, suboptimal code, complicated expressions, unused parameters, methods, properties.
$ composer global require 'phpmd/phpmd=2.2.*'
$ sudo ln -s ~/.composer/vendor/bin/phpmd /usr/local/bin/phpmd Test
example:
project_directory$ phpmd . text codesize,unusedcode,naming
PHP Depend - Indicates the quality of code design for extensibility, reusability and maintainability.
$ composer global require 'pdepend/pdepend=*'
$ sudo ln -s ~/.composer/vendor/bin/pdepend /usr/local/bin/pdepend Run
example
phpDocumentor is a tool for generating documentation from PHP code.
$ composer global require 'phpdocumentor/phpdocumentor=*'
$ sudo ln -s ~/.composer/vendor/bin/phpdoc /usr/local/bin/phpdoc Run
example:
project_directory$ mkdir docs && phpdoc -d . -t docs
PHP CodeBrowser is a tool for creating HTML presentations of PHP code, highlighting areas with identified violations by quality assurance tools such as PHP CodeSniffer or PHPMD.
$ composer global require 'mayflower/php-codebrowser=~1.1'
$ sudo ln -s ~/.composer/vendor/bin/phpcb /usr/local/bin/phpcb Run
example:
project_directory$ mkdir cb && phpcb -s . -o cb
PHP Copy/Paste Detector (PHPCPD) is a tool for finding duplicate code.
$ composer global require 'sebastian/phpcpd=*'
$ sudo ln -s ~/.composer/vendor/bin/phpcpd /usr/local/bin/phpcpd Check
example:
project_directory$ phpcpd .
PHPLOC is a tool to quickly measure the size and structure of a PHP project.
$ composer global require 'phploc/phploc=*'
$ sudo ln -s ~/.composer/vendor/bin/phploc /usr/local/bin/phploc Check
example:
project_directory$ phploc --log-xml phploc.xml .
PHP CodeSniffer is a set of two PHP tools. The main one - phpcs, allows you to detect violations of coding standards in PHP, CSS and JS files. And the second - phpcbf, allows for automatic correction of standards. PHP CodeSniffer is an essential tool to keep your code clean and consistent.
$ composer global require 'squizlabs/php_codesniffer=*'
$ sudo ln -s ~/.composer/vendor/bin/phpcs /usr/local/bin/phpcs
Additional Symfony2 standard check for PHP CodeSniffer:
$ cd ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards
$ git clone git://github.com/escapestudios/Symfony2-coding-standard.git Symfony2
$ cd Symfony2
$ git checkout 2.0.1 Checkout
example:
project_directory $ find . -type f -name '*.php' -exec phpcs --standard=Symfony2 '{}' ';'

S
sehimmelen, 2017-12-04
@sehimmelen

Download the PhpStotm trial - there is a good code analyzer that will help you clean up the project from various garbage.

A
Aleksei Podgaev, 2017-12-15
@alexiusp

We use SonarQube for this kind of code analysis, at the same time we control test coverage and possible security issues: https://www.sonarqube.org/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question