Matoeil
Matoeil

Reputation: 7289

cleanup unused code in a large php project

Is there any monitoring, analysis tools that would facilitate the cleaning up of dead files , variables , functions and refactoring of a large , relatively messy php project/framework?

Upvotes: 3

Views: 3089

Answers (4)

Maxim Panasyuk
Maxim Panasyuk

Reputation: 126

If you have Opcache enabled and all your code fits into the cache, you can find uncached (and thus probably unused) PHP files using the following snippet:

$di = new RecursiveDirectoryIterator(__DIR__ . '/src');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
    if (substr($filename, -strlen('.php')) === '.php') {
        if (!opcache_is_script_cached($filename)) {
            echo $filename . "\n";
        }
    }
}

Upvotes: 2

Ira Baxter
Ira Baxter

Reputation: 95334

If the problem is cleaning up dead code, first you have find dead code.

You can use test coverage tools (my company offers one of these) to find out what code is likely to be dead. Exercise lots of functionality either via unit tests or by simply running the application for a day; no coverage of method X means X is probably dead.

Dead variables are harder; you need data access coverage information. I don't know of any such tools for PHP.

I don't know of any static analyzers that will reliably tell you if code/variables are dead. (HipHop, mentioned in another answer, might be able to do this for some methods, and especially for local variables, but eval can cause any function to be called or any variable to be referenced so it is hard to get this right in PHP).

Then you can decide if you want to remove the dead code, or retain it to improve future evolution.

.

Upvotes: 2

Promethius
Promethius

Reputation: 21

Facebook's HipHop also has a very fast static code analyzer which will help. Etsy's Nick Galbreath gives a nice presentation on this at http://www.slideshare.net/nickgsuperstar/static-analysis-for-php

Upvotes: 2

Pudge601
Pudge601

Reputation: 2068

Try http://jenkins-ci.org/, there are plugins for lots of code analysis tools which can look for messy PHP etc.

Upvotes: 1

Related Questions