Search code examples
phpcomposer-phppearusage-statistics

Find all PEAR packages needed in project (source code)


I have come across an issue for which I need a list of all PEAR packages which are needed (=used) in my project. Is there a tool which can get me a list of all PEAR packages used in my source code, by simply reading the code? Read all the packages which are installed is not enough, I really need just those which are really used. I need this so I can upgrade the packages, or better - replace them by new libraries installable through composer.

I came across the PEAR package PHP_CompatInfo but this does not give me good results, and it also does only list packages which are registred in the plugin itself.


Solution

  • I would first assess what PEAR classes are installed on the system

    if you think that some of Libs used in the project may be missing, you need to download locally whole Pear repository

    to download all packages execute:

    pear config-set preferred_state alpha
    pear remote-list | awk '{print $1}' > tmp-list ; tail -n +5 tmp-list > pear-list ; rm tmp-list
    cat pear-list | xargs -n 1 pear install
    

    Then to create a list of all classes:

    cd /usr/share/php #( or different path to pear libs )
    
    find . ! -path "*/tests/*" ! -path "*/examples/*" -name "*.php" -type f | xargs grep -Pho "^class ([a-zA-Z0-9_]+)" | sed -e "s/^class //" -e "" > all_pear.txt
    

    That would create a list of all Pear classes saved into all_pear.txt

    Then I would search for all references in my code

    cd my_project;
    
    while read line; do find . -name "*.php" -type f | xargs grep -Pho $line; done < path/to/all_pear.txt | sort | uniq > used_classes.txt
    

    That will create list off all PEAR classes used in the project and it saves the list into file used_classes.txt