Czy znasz jakieś narzędzie, które może policzyć wszystkie linie kodu z projektu PHP?liczyć linie w projekcie PHP
Odpowiedz
W systemie operacyjnym POSIX (np Linux lub OS X) można napisać następujący w swojej powłoce Bash:
wc -l `find . -iname "*.php"`
Będzie to policzyć wiersze w wszystkich php-pliki w bieżącym katalogu, a także podkatalogi . (Zauważ, że te pojedyncze "cudzysłowy" to tylko odsłuchy, a nie rzeczywiste pojedyncze cudzysłowy)
SLOCCount to niesamowite narzędzie, które tworzy raport zliczania wierszy dla dużej liczby języków. To również idzie dalej dzięki innym, powiązanym statystykom, takim jak spodziewany koszt dewelopera.
Oto przykład:
$ 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'."
Niestety sloccount jest nieco długi w ząb i ból w szyi dla projektów PHP, szczególnie te, które mają zagnieżdżony vendor
katalogu nie chcesz policzone. Wydaje również ostrzeżenie dla każdego pliku PHP, który nie ma znacznika zamykającego (który powinien być wszystkim, jeśli nie mieszasz HTML i PHP).
CLOC jest bardziej nowoczesna alternatywa, która robi wszystko (edycja: prawie wszystko) sloccount robi, ale obsługuje również opcję --exclude-dir
i nie cierpią z powodu wspomnianego ścisłej problemu znacznika. Emituje także bazę danych SQLite, z której można wyodrębnić niektóre zaawansowane dane.
Nie robi wszystkiego, co SLOCCount ma, wciąż +1. –
Interesujące. Czego nie robi? – Shabbyrobe
Po prostu szukałem szacunków czasu/kosztów/osoby i nie mogłem znaleźć żadnej opcji na stronie podręcznika, która zwraca te dane. –
Zrobiłem sobie mały scenariusz, aby to zrobić w jednym z moich projektów. Po prostu użyj następującego kodu na stronie php w katalogu głównym projektu. Skrypt wykona kontrolę rekurencyjną w podfolderach.
<?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, co za idealny kod dla każdego programisty. Czy oblicza własne linie? – quantme
Powoduje duży hałas ... Tablice i inne wartości są niezainicjalizowane, gdy są używane. – Nux
Uwielbiam to. Dziękuję Ci. – EngineerCoder
SLOCs o PHP-projektu mogą liczone z sloccount użyciu coś takiego:
find . -not -wholename '*/libraries/*' -not -wholename '*/lib/*' -not -wholename '*/vendor/*' -type f xargs sloccount
Przykładowe wyjście dla projektu 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"`');
?>
Wystarczy uruchomić to w twoim bieżącym katalogu, w którym umieszczone są wszystkie pliki php, wyświetli linie liczników w przeglądarce.
Na Windows z linii poleceń:
findstr /R /N "^" *.php | find /C ":"
Dzięki this article.
Aby zawierać podkatalogi, należy \s
:
findstr /s /R /N "^" *.php | find /C ":"
Szybka metoda, która działa dobrze w systemie Windows, ale nie zawiera podfolderów. –
@Frogga Proste, po prostu dodaj '\ s', patrz edit – weston
A jeśli jesteś na Windows, można pobrać i zainstalować Cygwin, i ty czyń podobnie. Ponieważ Mac działa również na systemie BSD, uważam to za ostateczną odpowiedź. –
Należy pamiętać, że jeśli masz dużo plików szablonów PHP i/lub innych plików PHP z mieszanym kodem PHP/HTML, nie wyklucza to tylko linii HTML. –
Istnieje ograniczenie długości polecenia w powłoce; duże bazy kodów przekroczą to. – eukras