Domanda

Come posso trovare eventuali funzioni inutilizzate in un progetto PHP?

Esistono funzionalità o API integrate in PHP che mi consentiranno di analizzare la mia base di codice, ad esempio Riflessione, token_get_all()?

Queste API sono sufficientemente ricche di funzionalità da non dover fare affidamento su uno strumento di terze parti per eseguire questo tipo di analisi?

È stato utile?

Soluzione 2

Grazie Greg e Dave per il feedback.Non era proprio quello che stavo cercando, ma ho deciso di dedicare un po' di tempo alla ricerca e ho trovato questa soluzione rapida e sporca:

<?php
    $functions = array();
    $path = "/path/to/my/php/project";
    define_dir($path, $functions);
    reference_dir($path, $functions);
    echo
        "<table>" .
            "<tr>" .
                "<th>Name</th>" .
                "<th>Defined</th>" .
                "<th>Referenced</th>" .
            "</tr>";
    foreach ($functions as $name => $value) {
        echo
            "<tr>" . 
                "<td>" . htmlentities($name) . "</td>" .
                "<td>" . (isset($value[0]) ? count($value[0]) : "-") . "</td>" .
                "<td>" . (isset($value[1]) ? count($value[1]) : "-") . "</td>" .
            "</tr>";
    }
    echo "</table>";
    function define_dir($path, &$functions) {
        if ($dir = opendir($path)) {
            while (($file = readdir($dir)) !== false) {
                if (substr($file, 0, 1) == ".") continue;
                if (is_dir($path . "/" . $file)) {
                    define_dir($path . "/" . $file, $functions);
                } else {
                    if (substr($file, - 4, 4) != ".php") continue;
                    define_file($path . "/" . $file, $functions);
                }
            }
        }       
    }
    function define_file($path, &$functions) {
        $tokens = token_get_all(file_get_contents($path));
        for ($i = 0; $i < count($tokens); $i++) {
            $token = $tokens[$i];
            if (is_array($token)) {
                if ($token[0] != T_FUNCTION) continue;
                $i++;
                $token = $tokens[$i];
                if ($token[0] != T_WHITESPACE) die("T_WHITESPACE");
                $i++;
                $token = $tokens[$i];
                if ($token[0] != T_STRING) die("T_STRING");
                $functions[$token[1]][0][] = array($path, $token[2]);
            }
        }
    }
    function reference_dir($path, &$functions) {
        if ($dir = opendir($path)) {
            while (($file = readdir($dir)) !== false) {
                if (substr($file, 0, 1) == ".") continue;
                if (is_dir($path . "/" . $file)) {
                    reference_dir($path . "/" . $file, $functions);
                } else {
                    if (substr($file, - 4, 4) != ".php") continue;
                    reference_file($path . "/" . $file, $functions);
                }
            }
        }       
    }
    function reference_file($path, &$functions) {
        $tokens = token_get_all(file_get_contents($path));
        for ($i = 0; $i < count($tokens); $i++) {
            $token = $tokens[$i];
            if (is_array($token)) {
                if ($token[0] != T_STRING) continue;
                if ($tokens[$i + 1] != "(") continue;
                $functions[$token[1]][1][] = array($path, $token[2]);
            }
        }
    }
?>

Probabilmente ci dedicherò più tempo in modo da poter trovare rapidamente i file e i numeri di riga delle definizioni e dei riferimenti delle funzioni;queste informazioni vengono raccolte, ma non visualizzate.

Altri suggerimenti

Puoi provare il rilevatore di codici morti di Sebastian Bergmann:

phpdcd è un Dead Code Detector (DCD) per il codice PHP.Esegue la scansione di un progetto PHP per tutte le funzioni e i metodi dichiarati e segnala quelli come "codice morto" che non vengono chiamati almeno una volta.

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

Tieni presente che è un analizzatore di codice statico, quindi potrebbe fornire falsi positivi per metodi che chiamano solo dinamicamente, ad es.non è in grado di rilevare $foo = 'fn'; $foo();

Puoi installarlo tramite PEAR:

pear install phpunit/phpdcd-beta

Successivamente è possibile utilizzare le seguenti opzioni:

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.

Altri strumenti:


Nota: come da avviso di deposito, questo progetto non è più mantenuto e il suo repository è conservato solo per scopi di archivio.Quindi il tuo chilometraggio può variare.

Questo po' di scripting bash potrebbe aiutare:

grep -rhio ^function\ .*\(  .|awk -F'[( ]'  '{print "echo -n " $2 " && grep -rin " $2 " .|grep -v function|wc -l"}'|bash|grep 0

Questo fondamentalmente grep ricorsivamente la directory corrente per le definizioni delle funzioni, passa gli hit a awk, che forma un comando per fare quanto segue:

  • stampa il nome della funzione
  • grep ricorsivamente di nuovo
  • reindirizzando l'output a grep -v per filtrare le definizioni di funzione in modo da conservare le chiamate alla funzione
  • invia questo output a wc -l che stampa il conteggio delle righe

Questo comando viene quindi inviato per l'esecuzione a bash e l'output viene greppato per 0, che indicherebbe 0 chiamate alla funzione.

Tieni presente che questo lo farà non risolvere il problema citato da Calebbrown sopra, quindi potrebbero esserci dei falsi positivi nell'output.

UTILIZZO: find_unused_functions.php <directory_root>

NOTA:Questo è un approccio “rapido” al problema.Questo script esegue solo un passaggio lessicale sui file e non rispetta le situazioni in cui moduli diversi definiscono funzioni o metodi con nomi identici.Se utilizzi un IDE per lo sviluppo PHP, potrebbe offrire una soluzione più completa.

Richiede PHP 5

Per salvarti sono disponibili un copia e incolla, un download diretto e eventuali nuove versioni disponibile qui.

#!/usr/bin/php -f

<?php

// ============================================================================
//
// find_unused_functions.php
//
// Find unused functions in a set of PHP files.
// version 1.3
//
// ============================================================================
//
// Copyright (c) 2011, Andrey Butov. All Rights Reserved.
// This script is provided as is, without warranty of any kind.
//
// http://www.andreybutov.com
//
// ============================================================================

// This may take a bit of memory...
ini_set('memory_limit', '2048M');

if ( !isset($argv[1]) ) 
{
    usage();
}

$root_dir = $argv[1];

if ( !is_dir($root_dir) || !is_readable($root_dir) )
{
    echo "ERROR: '$root_dir' is not a readable directory.\n";
    usage();
}

$files = php_files($root_dir);
$tokenized = array();

if ( count($files) == 0 )
{
    echo "No PHP files found.\n";
    exit;
}

$defined_functions = array();

foreach ( $files as $file )
{
    $tokens = tokenize($file);

    if ( $tokens )
    {
        // We retain the tokenized versions of each file,
        // because we'll be using the tokens later to search
        // for function 'uses', and we don't want to 
        // re-tokenize the same files again.

        $tokenized[$file] = $tokens;

        for ( $i = 0 ; $i < count($tokens) ; ++$i )
        {
            $current_token = $tokens[$i];
            $next_token = safe_arr($tokens, $i + 2, false);

            if ( is_array($current_token) && $next_token && is_array($next_token) )
            {
                if ( safe_arr($current_token, 0) == T_FUNCTION )
                {
                    // Find the 'function' token, then try to grab the 
                    // token that is the name of the function being defined.
                    // 
                    // For every defined function, retain the file and line
                    // location where that function is defined. Since different
                    // modules can define a functions with the same name,
                    // we retain multiple definition locations for each function name.

                    $function_name = safe_arr($next_token, 1, false);
                    $line = safe_arr($next_token, 2, false);

                    if ( $function_name && $line )
                    {
                        $function_name = trim($function_name);
                        if ( $function_name != "" )
                        {
                            $defined_functions[$function_name][] = array('file' => $file, 'line' => $line);
                        }
                    }
                }
            }
        }
    }
}

// We now have a collection of defined functions and
// their definition locations. Go through the tokens again, 
// and find 'uses' of the function names. 

foreach ( $tokenized as $file => $tokens )
{
    foreach ( $tokens as $token )
    {
        if ( is_array($token) && safe_arr($token, 0) == T_STRING )
        {
            $function_name = safe_arr($token, 1, false);
            $function_line = safe_arr($token, 2, false);;

            if ( $function_name && $function_line )
            {
                $locations_of_defined_function = safe_arr($defined_functions, $function_name, false);

                if ( $locations_of_defined_function )
                {
                    $found_function_definition = false;

                    foreach ( $locations_of_defined_function as $location_of_defined_function )
                    {
                        $function_defined_in_file = $location_of_defined_function['file'];
                        $function_defined_on_line = $location_of_defined_function['line'];

                        if ( $function_defined_in_file == $file && 
                             $function_defined_on_line == $function_line )
                        {
                            $found_function_definition = true;
                            break;
                        }
                    }

                    if ( !$found_function_definition )
                    {
                        // We found usage of the function name in a context
                        // that is not the definition of that function. 
                        // Consider the function as 'used'.

                        unset($defined_functions[$function_name]);
                    }
                }
            }
        }
    }
}


print_report($defined_functions);   
exit;


// ============================================================================

function php_files($path) 
{
    // Get a listing of all the .php files contained within the $path
    // directory and its subdirectories.

    $matches = array();
    $folders = array(rtrim($path, DIRECTORY_SEPARATOR));

    while( $folder = array_shift($folders) ) 
    {
        $matches = array_merge($matches, glob($folder.DIRECTORY_SEPARATOR."*.php", 0));
        $moreFolders = glob($folder.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR);
        $folders = array_merge($folders, $moreFolders);
    }

    return $matches;
}

// ============================================================================

function safe_arr($arr, $i, $default = "")
{
    return isset($arr[$i]) ? $arr[$i] : $default;
}

// ============================================================================

function tokenize($file)
{
    $file_contents = file_get_contents($file);

    if ( !$file_contents )
    {
        return false;
    }

    $tokens = token_get_all($file_contents);
    return ($tokens && count($tokens) > 0) ? $tokens : false;
}

// ============================================================================

function usage()
{
    global $argv;
    $file = (isset($argv[0])) ? basename($argv[0]) : "find_unused_functions.php";
    die("USAGE: $file <root_directory>\n\n");
}

// ============================================================================

function print_report($unused_functions)
{
    if ( count($unused_functions) == 0 )
    {
        echo "No unused functions found.\n";
    }

    $count = 0;
    foreach ( $unused_functions as $function => $locations )
    {
        foreach ( $locations as $location )
        {
            echo "'$function' in {$location['file']} on line {$location['line']}\n";
            $count++;
        }
    }

    echo "=======================================\n";
    echo "Found $count unused function" . (($count == 1) ? '' : 's') . ".\n\n";
}

// ============================================================================

/* EOF */

Se ricordo bene puoi usare phpCallGraph fare quello.Genererà un bel grafico (immagine) per te con tutti i metodi coinvolti.Se un metodo non è connesso a nessun altro, è un buon segno che il metodo è orfano.

Ecco un esempio: classGallerySystem.png

Il metodo getKeywordSetOfCategories() è orfano.

A proposito, non è necessario scattare un'immagine: anche phpCallGraph può farlo creare un file di testo o un array PHP, ecc.

Poiché le funzioni/metodi PHP possono essere richiamati dinamicamente, non esiste un modo programmatico per sapere con certezza se una funzione non verrà mai chiamata.

L’unico modo certo è attraverso l’analisi manuale.

Aggiornamento 2019+

Mi sono lasciato ispirare La risposta di Andrey e lo ha trasformato in uno sniff standard di codifica.

Il rilevamento è molto semplice ma potente:

  • trova tutti i metodi public function someMethod()
  • quindi trova tutte le chiamate ai metodi ${anything}->someMethod()
  • e semplicemente segnala quelle funzioni pubbliche che non sono mai state convocate

Mi ha aiutato a rimuovere Sopra Oltre 20 metodi Dovrei mantenere e testare.


3 passaggi per trovarli

Installa ECS:

composer require symplify/easy-coding-standard --dev

Impostare ecs.yaml configurazione:

# ecs.yaml
services:
    Symplify\CodingStandard\Sniffs\DeadCode\UnusedPublicMethodSniff: ~

Esegui il comando:

vendor/bin/ecs check src

Vedi i metodi segnalati e rimuovi quelli che non ti sembrano utili 👍


Puoi leggere di più a riguardo qui: Rimuovi i metodi pubblici morti dal tuo codice

afaik non c'è modo.Per sapere quali funzioni "appartengono a chi" è necessario eseguire il sistema (ricerca della funzione di associazione tardiva in runtime).

Ma gli strumenti di refactoring si basano sull'analisi statica del codice.Mi piacciono molto i linguaggi tipizzati dinamici, ma a mio avviso sono difficili da scalare.La mancanza di refactoring sicuri in basi di codice di grandi dimensioni e linguaggi tipizzati dinamici rappresenta un grave svantaggio per la manutenibilità e la gestione dell'evoluzione del software.

phpxref identificherà da dove vengono chiamate le funzioni che faciliterebbero l'analisi, ma è comunque necessario un certo sforzo manuale.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top