Conosci qualche strumento che possa contare tutte le linee di codice da un progetto PHP?contare le linee in un progetto PHP
risposta
In un sistema operativo POSIX (ad esempio Linux o OS X) è possibile scrivere quanto segue nel tuo shell Bash:
wc -l `find . -iname "*.php"`
Ciò contare le righe in tutti i file php nella directory corrente e anche le sottodirectory . (Si noti che le singole "virgolette" sono apici inversi, non virgolette singole effettive)
SLOCCount è uno strumento straordinario che produce un rapporto di conteggio delle righe per un gran numero di lingue. Va anche oltre producendo altre statistiche correlate come il costo previsto per gli sviluppatori.
Ecco un esempio:
$ sloccount .
Creating filelist for experimental
Creating filelist for prototype
Categorizing files.
Finding a working MD5 command....
Found a working MD5 command.
Computing results.
SLOC Directory SLOC-by-Language (Sorted)
10965 experimental cpp=5116,ansic=4976,python=873
832 prototype cpp=518,tcl=314
Totals grouped by language (dominant language first):
cpp: 5634 (47.76%)
ansic: 4976 (42.18%)
python: 873 (7.40%)
tcl: 314 (2.66%)
Total Physical Source Lines of Code (SLOC) = 11,797
Development Effort Estimate, Person-Years (Person-Months) = 2.67 (32.03)
(Basic COCOMO model, Person-Months = 2.4 * (KSLOC**1.05))
Schedule Estimate, Years (Months) = 0.78 (9.33)
(Basic COCOMO model, Months = 2.5 * (person-months**0.38))
Estimated Average Number of Developers (Effort/Schedule) = 3.43
Total Estimated Cost to Develop = $ 360,580
(average salary = $56,286/year, overhead = 2.40).
SLOCCount, Copyright (C) 2001-2004 David A. Wheeler
SLOCCount is Open Source Software/Free Software, licensed under the GNU GPL.
SLOCCount comes with ABSOLUTELY NO WARRANTY, and you are welcome to
redistribute it under certain conditions as specified by the GNU GPL license;
see the documentation for details.
Please credit this data as "generated using David A. Wheeler's 'SLOCCount'."
Purtroppo, SLOCCount è un po 'lungo nel dente e un dolore al collo per progetti di PHP, in particolare quelli che hanno un vendor
directory nidificato che non si desidera contato. Inoltre, emette un avviso per ogni file PHP che non ha un tag di chiusura (che dovrebbe essere tutto se non si mischiano HTML e PHP).
CLOC è un'alternativa più moderna che fa tutto (modifica: quasi tutto) SLOCCount fa, ma supporta anche un'opzione --exclude-dir
e non soffre del suddetto problema del tag vicino. Emette anche un database SQLite da cui è possibile estrarre alcune metriche piuttosto avanzate.
Non fa tutto ciò che fa SLOCCount, ancora +1. –
Interessante. Cosa non fa? – Shabbyrobe
Stavo solo cercando le stime tempo/costo/persona e non sono riuscito a trovare alcuna opzione nella pagina man che restituisce quelle. –
Mi sono fatto una piccola sceneggiatura per farlo in uno dei miei progetti. Basta usare il seguente codice su una pagina php nella radice del progetto. Lo script eseguirà il controllo ricorsivo nelle sottocartelle.
<?php
/**
* A very simple stats counter for all kind of stats about a development folder
*
* @author Joel Lord
* @copyright Engrenage (www.engrenage.biz)
*
* For more information: [email protected]
*/
$fileCounter = array();
$totalLines = countLines('.', $fileCounter);
echo $totalLines." lines in the current folder<br>";
echo $totalLines - $fileCounter['gen']['commentedLines'] - $fileCounter['gen']['blankLines'] ." actual lines of code (not a comment or blank line)<br><br>";
foreach($fileCounter['gen'] as $key=>$val) {
echo ucfirst($key).":".$val."<br>";
}
echo "<br>";
foreach($fileCounter as $key=>$val) {
if(!is_array($val)) echo strtoupper($key).":".$val." file(s)<br>";
}
function countLines($dir, &$fileCounter) {
$_allowedFileTypes = "(html|htm|phtml|php|js|css|ini)";
$lineCounter = 0;
$dirHandle = opendir($dir);
$path = realpath($dir);
$nextLineIsComment = false;
if($dirHandle) {
while(false !== ($file = readdir($dirHandle))) {
if(is_dir($path."/".$file) && ($file !== '.' && $file !== '..')) {
$lineCounter += countLines($path."/".$file, $fileCounter);
} elseif($file !== '.' && $file !== '..') {
//Check if we have a valid file
$ext = _findExtension($file);
if(preg_match("/".$_allowedFileTypes."$/i", $ext)) {
$realFile = realpath($path)."/".$file;
$fileHandle = fopen($realFile, 'r');
$fileArray = file($realFile);
//Check content of file:
for($i=0; $i<count($fileArray); $i++) {
if($nextLineIsComment) {
$fileCounter['gen']['commentedLines']++;
//Look for the end of the comment block
if(strpos($fileArray[$i], '*/')) {
$nextLineIsComment = false;
}
} else {
//Look for a function
if(strpos($fileArray[$i], 'function')) {
$fileCounter['gen']['functions']++;
}
//Look for a commented line
if(strpos($fileArray[$i], '//')) {
$fileCounter['gen']['commentedLines']++;
}
//Look for a class
if(substr(trim($fileArray[$i]), 0, 5) == 'class') {
$fileCounter['gen']['classes']++;
}
//Look for a comment block
if(strpos($fileArray[$i], '/*')) {
$nextLineIsComment = true;
$fileCounter['gen']['commentedLines']++;
$fileCounter['gen']['commentBlocks']++;
}
//Look for a blank line
if(trim($fileArray[$i]) == '') {
$fileCounter['gen']['blankLines']++;
}
}
}
$lineCounter += count($fileArray);
}
//Add to the files counter
$fileCounter['gen']['totalFiles']++;
$fileCounter[strtolower($ext)]++;
}
}
} else echo 'Could not enter folder';
return $lineCounter;
}
function _findExtension($filename) {
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
Wow, che codice perfetto per ogni programmatore. Calcola le proprie linee? – quantme
Produce molto rumore ... Le matrici e altri valori non sono inizializzati quando utilizzati. – Nux
Lo adoro. Grazie. – EngineerCoder
La SLOCs di un PHP-progetto può contare con sloccount usando qualcosa di simile:
find . -not -wholename '*/libraries/*' -not -wholename '*/lib/*' -not -wholename '*/vendor/*' -type f xargs sloccount
Esempio di output per un progetto SizeY Drupal:
[...]
SLOC Directory SLOC-by-Language (Sorted)
44892 top_dir pascal=33176,php=10293,sh=1423
Totals grouped by language (dominant language first):
pascal: 33176 (73.90%)
php: 10293 (22.93%)
sh: 1423 (3.17%)
Total Physical Source Lines of Code (SLOC) = 44,892
Development Effort Estimate, Person-Years (Person-Months) = 10.86 (130.31)
(Basic COCOMO model, Person-Months = 2.4 * (KSLOC**1.05))
Schedule Estimate, Years (Months) = 1.33 (15.91)
(Basic COCOMO model, Months = 2.5 * (person-months**0.38))
Estimated Average Number of Developers (Effort/Schedule) = 8.19
Total Estimated Cost to Develop = $ 1,466,963
(average salary = $56,286/year, overhead = 2.40).
SLOCCount, Copyright (C) 2001-2004 David A. Wheeler
SLOCCount is Open Source Software/Free Software, licensed under the GNU GPL.
SLOCCount comes with ABSOLUTELY NO WARRANTY, and you are welcome to
redistribute it under certain conditions as specified by the GNU GPL license;
see the documentation for details.
Please credit this data as "generated using David A. Wheeler's 'SLOCCount'."
<?php
passthru('wc -l `find . -iname "*.php"`');
?>
basta eseguire questo nella tua directory corrente in cui sono posizionati tutti i file php, mostrerà le linee di conteggio sul browser.
Su Windows da riga di comando:
findstr /R /N "^" *.php | find /C ":"
Grazie a this article.
Per includere gli indici secondari, utilizzare \s
:
findstr /s /R /N "^" *.php | find /C ":"
Metodo rapido che funziona bene per Windows, ma non include le sottocartelle. –
@Frogga Semplice, basta aggiungere '\ s', vedi modifica – weston
E se siete su Windows, è possibile scaricare e installare Cygwin, e fare altrettanto. Visto che ora Mac funziona anche su un sistema operativo BSD, considero questa la risposta definitiva. –
Ricorda che se hai un sacco di file modello PHP e/o altri file PHP con codice PHP/HTML misto, questo non esclude le sole righe HTML. –
C'è un limite alla lunghezza del comando nella shell; grandi basi di codice supereranno questo. – eukras