2009-04-26 35 views

Trả lời

96

Trên một hệ điều hành POSIX (ví dụ như Linux hoặc OS X), bạn có thể viết như sau vào vỏ Bash của bạn:

wc -l `find . -iname "*.php"` 

này sẽ đếm các dòng trong tất cả php-tập tin trong thư mục hiện hành và cũng thư mục con . (Lưu ý rằng những 'dấu nháy đơn' đó là dấu gạch chéo ngược, không phải dấu nháy đơn thực)

+2

Và nếu bạn đang ở trên Windows, bạn có thể tải về và cài đặt Cygwin, và làm như vậy. Kể từ khi Mac chạy trên đầu trang của một hệ điều hành BSD bây giờ là tốt, tôi xem xét câu trả lời dứt khoát này. –

+1

Hãy nhớ rằng nếu bạn có rất nhiều tệp mẫu PHP và/hoặc các tệp PHP khác với mã PHP/HTML hỗn hợp, điều này sẽ không loại trừ các dòng chỉ HTML. –

+1

Có giới hạn về độ dài lệnh trong trình bao; các codebases lớn sẽ vượt quá mức này. – eukras

13

SLOCCount là một công cụ tuyệt vời tạo báo cáo số lượng dòng cho một số lượng lớn ngôn ngữ. Nó cũng đi xa hơn bằng cách tạo ra các số liệu thống kê liên quan khác như chi phí phát triển dự kiến.

Dưới đây là một ví dụ:

$ 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'." 
9

Thật không may, SLOCCount là một chút dài trong răng và một cơn đau ở cổ cho các dự án PHP, đặc biệt là những người mà có một vendor thư mục lồng nhau bạn không muốn đếm. Ngoài ra, nó phát ra một cảnh báo cho mọi tệp PHP không có thẻ đóng (mà nên là tất cả chúng nếu bạn không trộn lẫn HTML và PHP).

CLOC là phương án thay thế hiện đại hơn mọi thứ (chỉnh sửa: gần như mọi thứ) SLOCCount thực hiện, nhưng cũng hỗ trợ tùy chọn --exclude-dir và không gặp vấn đề về thẻ đóng gần đây. Nó cũng phát ra một cơ sở dữ liệu SQLite mà bạn có thể trích xuất một số chỉ số khá nâng cao từ đó.

+1

Không làm mọi thứ SLOCCount, vẫn +1. –

+0

Thú vị. Nó không làm gì? – Shabbyrobe

+0

Tôi chỉ đang tìm kiếm ước tính thời gian/chi phí/người và tôi không thể tìm thấy bất kỳ tùy chọn nào trong trang hướng dẫn trả về những người đó. –

20

Tôi tự tạo một kịch bản nhỏ để thực hiện điều đó trong một trong các dự án của mình. Chỉ cần sử dụng đoạn mã sau trên trang php trong thư mục gốc của dự án của bạn. Kịch bản sẽ kiểm tra đệ quy trên các thư mục con.

<?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; 
} 
+0

Ồ, một mã hoàn hảo cho bất kỳ lập trình viên nào. Liệu nó có tính toán các dòng riêng của nó không? – quantme

+0

Tạo ra nhiều nhiễu ... Mảng và các giá trị khác không được khởi tạo khi được sử dụng. – Nux

+0

Tôi thích điều đó. Cảm ơn bạn. – EngineerCoder

3

Các SLOCs của một PHP-dự án có thể tính với sloccount sử dụng một cái gì đó như thế này:

find . -not -wholename '*/libraries/*' -not -wholename '*/lib/*' -not -wholename '*/vendor/*' -type f xargs sloccount 

Mẫu đầu ra cho một dự án 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'." 
1
<?php 
passthru('wc -l `find . -iname "*.php"`'); 
?> 

Chỉ cần chạy này trên thư mục hiện tại của bạn, nơi tất cả các tập tin php được đặt, nó sẽ hiển thị số dòng trên trình duyệt.

5

Mở cửa sổ từ một dòng lệnh:

findstr /R /N "^" *.php | find /C ":" 

Nhờ this article.

Để bao gồm các thư mục phụ, sử dụng \s :

findstr /s /R /N "^" *.php | find /C ":" 
+0

Phương thức nhanh hoạt động tốt cho các cửa sổ nhưng không bao gồm các thư mục phụ. –

+1

@ Frogga Đơn giản, chỉ cần thêm '\ s', xem chỉnh sửa – weston

Các vấn đề liên quan