Listing du fichier listefnsV3.php
00001<?php
00002 # # (gH) -_- listefnsV2.php ; TimeStamp (unix) : 22 Novembre 2017 vers 15:59
00003
00004 error_reporting(E_ALL | E_NOTICE | E_STRICT ) ;
00005
00006 include("listefnsV3-inc.php") ;
00007
00008 ### 1. vérification des paramètres
00009
00010 if ($argc<2) {
00011
00012 echo "\n" ;
00013 echo "Appel incorrect du programme. Il faut donner un nom de fichier PHP à analyser.\n " ;
00014 echo "Exemple : php listefnsV1.php index.php\n\n" ;
00015 exit(-1) ;
00016
00017 } ; # fin de si
00018
00019 $nomFic = $argv[1] ;
00020
00021 if (!file_exists($nomFic)) {
00022
00023 echo "\n" ;
00024 echo "Exécution impossible, le fichier indiqué, soit $nomFic n'est pas vu.\n " ;
00025 exit(-2) ;
00026
00027 } ; # fin de si
00028
00029 echo "\n" ;
00030 $msg = "Analyse du fichier $nomFic" ;
00031 echo " ".$msg." dans ".getcwd()."\n" ;
00032 echo " ".souligne($msg,"=")."\n" ;
00033 echo " ".dateEtHeure()."\n\n" ;
00034
00035 ### 2. on cherche les noms de fichiers via les fonctions include_once() et require_once()
00036
00037 $tdmar = [ "include(","include_once(","require(","require_once("] ; # tableau des mots à rechercher
00038 $lstFicTmp = $nomFic ; # liste des fichiers à traiter
00039 $lstFonc = " " ; # liste des fonctions
00040 $lstFicVus = $nomFic ; # liste des fichiers déjà vus
00041
00042 while (nbMots($lstFicTmp)>0) {
00043 $nomf = trim(mot($lstFicTmp,1)) ;
00044 if (strlen($nomf)>0) {
00045 $fh = fopen($nomf,"r") or die("\n impossible de lire dans le fichier $nomf\n\n\n") ;
00046 # recherche d'inclusion
00047 while ($lig=fgets($fh)) {
00048 foreach ($tdmar as $mac) {
00049 $pdi = strpos($lig,$mac) ; # position de include ou assimilé
00050 if ($pdi>-1) {
00051 $ll = substr($lig,0,strpos($lig,')')) ; #
00052 $dl = substr($ll,1+strpos($ll,'"')) ; # début de ligne
00053 $nn = substr($dl,0,strpos($dl,'"')) ; # nom de fichier
00054 $nn = trim($nn) ;
00055 if (positionDans($nn,$lstFicTmp)==0) { # ajout éventuel de ce nom de fichier
00056 $lstFicTmp .= " $nn" ;
00057 if (positionDans($nn,$lstFicVus)==0) { # ajout éventuel
00058 $lstFicVus .= " $nn" ;
00059 } ; # fin de si
00060 } ; # fin de si
00061 } ; # fin si on a vu le mot à chercher
00062 } ; # fin pour chaque
00063 } ; # fin de lecture
00064 fclose($fh) ;
00065 $lstFicTmp = phraseSansPremierMot($lstFicTmp) ;
00066 } ; # fin de si sur strlen($nomf)>0
00067 } ; # fin de tant que sur lstFic
00068
00069 ### 3. affichage numéroté et trié des fichiers à lister avec nombre de lignes
00070
00071 $msg = "Liste des fichiers inclus" ;
00072 echo " ".$msg."\n" ;
00073 echo " ".souligne($msg,"-")."\n" ;
00074
00075 ## 3.1 tri de la liste via explode/sort/implode
00076
00077 $tdf = explode(" ",$lstFicVus) ;
00078 sort($tdf) ;
00079 $lstFicVus = implode(" ",$tdf) ;
00080
00081 $nbLigTot = 0 ;
00082
00083 ## 3.2 affichage de la liste des fichiers
00084
00085 $nbm = nbMots($lstFicVus) ;
00086 for ($idf=1;$idf<=$nbm;$idf++) {
00087 echo sprintf(" %3d",$idf) ;
00088 $motc = mot($lstFicVus,$idf) ;
00089 if ($motc==$nomFic) {
00090 echo " * " ;
00091 } else {
00092 echo " " ;
00093 } ; # fin si
00094 echo sprintf("%-20s",$motc) ;
00095 $nbl = nbLignes($motc) ;
00096 $nbLigTot += $nbl ;
00097 echo sprintf("%6d",$nbl) ;
00098 echo "\n" ;
00099 } ; # fin pour chaque
00100
00101 echo "\n total ".sprintf("%18d",$nbLigTot)," lignes\n\n" ;
00102
00103 ### 4. recherche des définitions de fonction
00104
00105 # on utilise un tableau simple listeFonc
00106 # et deux tableaux associatifs : listeFonc, numLigne et ficSource
00107 # la clé est le nom de la fonction et la valeur soit le numéro de ligne soit le nom du fichier impliqué
00108
00109 $listeFonc = array() ;
00110 $numLigne = array() ;
00111 $ficSource = array() ;
00112
00113 ## 4.1 on passe en revue tous les fichiers pour trouver function comme mot 1
00114
00115 $nbFonc = 0 ;
00116 for ($idf=1;$idf<=$nbm;$idf++) {
00117 $fic = mot($lstFicVus,$idf) ;
00118 $fc = file_get_contents($fic) ;
00119 $tl = preg_split("/\n/",$fc) ;
00120 $nbl = 0;
00121 foreach ($tl as $ligne) {
00122 $nbl++ ;
00123 $mot1 = mot($ligne,1) ;
00124 if ($mot1=="function") {
00125 $mot2 = mot($ligne,2)."(" ;
00126 $nomFonc = trim(substr($mot2,0,strpos($mot2,"("))) ; # à détailler, à cause de strfun.php
00127 $nbFonc++ ;
00128 $listeFonc[$nbFonc] = $nomFonc ;
00129 $numLigne[ $nomFonc ] = $nbl ;
00130 $ficSource[$nomFonc ] = $fic ;
00131 } ; # fin si
00132 } ; # fin pour chaque
00133 } ; # fin pour chaque
00134
00135 ## 4.2 on trie la liste des fonctions et on l'affiche
00136
00137 $msg = "Liste des $nbFonc fonctions" ;
00138 echo " ".$msg . " (et position dans le fichier)\n" ;
00139 echo " ".souligne($msg,"-")."\n\n" ;
00140
00141 asort($listeFonc) ;
00142 $nbf = 0 ;
00143 foreach ($listeFonc as $num => $fonction) {
00144 $nbf++ ;
00145 echo sprintf(" %3d",$nbf) ;
00146 echo ". " ;
00147 echo sprintf("%-40s",$fonction) ;
00148 echo sprintf("%-25s",$ficSource[$fonction]) ;
00149 echo sprintf("%9d" ,$numLigne[$fonction]) ;
00150 echo "\n" ;
00151 } # fin pour chaque fonction
00152
00153 ## 4.3 liste par fichier (algorihtme de parcours naif)
00154
00155 echo "\n" ;
00156
00157 for ($idf=1;$idf<=$nbm;$idf++) {
00158
00159 echo "\n" ;
00160 $fic = mot($lstFicVus,$idf) ;
00161 $msg = "Fonctions du fichier $fic" ;
00162 echo " ".$msg." et position dans le fichier\n" ;
00163 echo " ".souligne($msg,"=")."\n" ;
00164
00165 $nbf = 0 ;
00166 foreach ($listeFonc as $num => $fonction) {
00167 if ($ficSource[$fonction]==$fic) {
00168 $nbf++ ;
00169 echo sprintf(" %3d",$nbf) ;
00170 echo ". " ;
00171 echo sprintf("%-40s",$fonction) ;
00172 echo sprintf("%-25s",$ficSource[$fonction]) ;
00173 echo sprintf("%9d" ,$numLigne[$fonction]) ;
00174 echo "\n" ;
00175 } ; # fin si
00176 } # fin pour chaque fonction
00177
00178 } ; # fin pour chaque
00179
00180 echo "\n\n" ;
00181
00182
00183 ?>
Pour ne pas voir les numéros de ligne, ajoutez &nl=non à la suite du nom du fichier.
Retour à la page principale de (gH)