Jump to content

Counting Lines of code


ober

Recommended Posts

Does anyone know of a tool that could count the number of lines of code in a project or just in a bunch of files on a server?

I've been working on a number of applications for work that are all web-based and I'm just curious. For example, one of my backend ajax files is almost 900 lines of code by itself. If I had to guess, I'd say I have close to 20,000 - 30,000 lines of code in all the various applications. And I've written 99% of it myself (I'd say .2% is javascript I've found elsewhere and another .8% is PHP that I picked up on here or other places.
Link to comment
Share on other sites

Not exactly a tool, but with a short script you could read all .php files in a folder, read each file into an array, count array size, add to total, do next file. A few lines of code should be able to handle that.
Link to comment
Share on other sites

Quick and dirty in the folder where you want to count:
[code]<?php
$lines = 0;
foreach (glob("*.php") as $filename) {
    $thefile = file($filename);
    $lines+= count($thefile);
}
echo "Lines of code: ". $lines;
?> [/code]
Link to comment
Share on other sites

Guest askjames01
How about [b]TextPad[/b] ober?
It will make your life easier i think? :)

[a href=\"http://www.textpad.com/\" target=\"_blank\"]http://www.textpad.com/[/a]

it has Document Select for multiple files... and it can count lines of codes... and it's [b]FREE[/b].


try it!
and GOOD LUCK!


-/james
Link to comment
Share on other sites

Apparently I underestimated myself. Using Andy's code, I'm showing 44,167 lines of code. And that's only the top level of each application, but it also includes a little over 2000 lines that I didn't write. So if it all balances out, I'd say I've written at least 43000 lines of PHP/JS/HTML in the past year and a half.

Crazy.
Link to comment
Share on other sites

[!--quoteo(post=354505:date=Mar 13 2006, 02:37 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 13 2006, 02:37 PM) [snapback]354505[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Apparently I underestimated myself. Using Andy's code, I'm showing 44,167 lines of code. And that's only the top level of each application, but it also includes a little over 2000 lines that I didn't write. So if it all balances out, I'd say I've written at least 43000 lines of PHP/JS/HTML in the past year and a half.

Crazy.
[/quote]

who at the zend offices decides to make a function called 'glob'? lol lovely, but andy - thank you, i've been looking for something like that for another little project i'm testing out.

ober, my guess is that if you want a more accurate guess, then unless you neve have blank lines between your lines of code, it maybe a good idea to do a check for lines that are literally just empty.
Link to comment
Share on other sites

I don't have many blank lines, and I think it'd be hard to differentiate between blank lines and comment lines.

And I'd guess that if you took both of those out, I'd still be above 40,000.

Alright... I had to know:

[code]$lines = 0;
foreach (glob("*.php") as $filename) {
    $thefile = file($filename);
    foreach($thefile as $key => $val){
        if(trim($val) != '')
            $lines++;
    }
}
echo "Lines of code: ". $lines;[/code]

That takes one of my biggest applications from 9474 lines of code to 8630, about an 8.5% drop, which as I guessed, still leaves me about 40000 lines if the ratio is similar in the other apps. Keep in mind that this doesn't remove comment lines.
Link to comment
Share on other sites

[!--quoteo(post=354639:date=Mar 13 2006, 08:20 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 13 2006, 08:20 PM) [snapback]354639[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Alright... I had to know:
[/quote]
lol curiosity always wins in the end...
Link to comment
Share on other sites

Aahhh, why not check for commented lines too while you're at it...
[code]$lines = 0;
foreach (glob("*.php") as $filename) {
    $thefile = file($filename);
    foreach($thefile as $key => $val){
        if(trim($val) != '' || substr(trim($val),0,2) != "//")
            $lines++;
    }
}
echo "Pretty accurate estimate lines of code: ". $lines;[/code]
Link to comment
Share on other sites

Your || should be an &&.

Well let's just go full out and list both ;-)

[code]
$comment_lines = 0;
$code_lines = 0;
foreach (glob("*.php") as $filename) {
    $thefile = file($filename);
    foreach($thefile as $key => $val){
        if(trim($val) != '' && substr(trim($val),0,2) != "//" && substr(trim($val),0,2) != "/*")
            $code_lines++;
        if(trim($val) != '' && (substr(trim($val),0,2) == "//" || substr(trim($val),0,2) == "/*"))
            $comment_lines++;
    }
}
echo "Lines of code: $code_lines<br/>";
echo "Lines of comments: $comment_lines<br/>";
[/code]

And we could get more tricky with it... look for lines that have series of "========", "---------", etc... and you could single out full comment blocks as well, looking for the beginning and ending /* */

Crazy.
Link to comment
Share on other sites

  • 4 weeks later...
Here's my updated version, that checks for comment blocks... I'm sure there's probably a better way to do this, but none that I could think of at the moment. It's a quick hack anyways ;-)

[code]
<?php
$comment_lines = 0;
$code_lines = 0;
$in_comment = false;
foreach (glob("*.php") as $filename) {
    $thefile = file($filename);
    foreach($thefile as $key => $val){
        if (trim($val) != '') {
            if ($in_comment) {
                if (trim($val) != "*/") {
                    $comment_lines++;
                }else{
                    $comment_lines++;
                    $in_comment = false;
                }
            }else{
                if (substr(trim($val),0,2) == "//") {
                    $comment_lines++;
                }else if (substr(trim($val),0,2) == "/*") {
                    $comment_lines++;
                    if (substr(trim($val), -2) != "*/") {
                        $in_comment = true;
                    }
                }else{
                    $code_lines++;
                }
            }
        }
    }
}
echo "Lines of code: $code_lines\n";
echo "Lines of comments: $comment_lines\n";
?>
[/code]
Link to comment
Share on other sites

Ok, one more fix, this one includes checks to see how much code is between <?php and ?> tags... again there's probably a better or more accurate way, but this at least gives a rough idea. Used it because the project I'm doing includes alot of HTML outside the tags and I don't have time yet to get it separated like I'd like to. It also totals everything up at the end.

Here it is:

[code]
<?php
// Code Counter
$comment_lines = 0;
$code_lines = 0;
$other_lines = 0;
$total_lines = 0;
$in_comment = false;
foreach (glob("*.php") as $filename) {
    $thefile = file($filename);
    foreach($thefile as $key => $val){
        if (trim($val) != '') {
            if (preg_match('/\<\?php/', trim($val))) {
                $in_php = true;
            }
            if ($in_php) {
                if ($in_comment) {
                    if (trim($val) != "*/") {
                        $comment_lines++;
                    }else{
                        $comment_lines++;
                        $in_comment = false;
                    }
                }else{
                    if (substr(trim($val),0,2) == "//") {
                        $comment_lines++;
                    }else if (substr(trim($val),0,2) == "/*") {
                        $comment_lines++;
                        if (substr(trim($val), -2) != "*/") {
                            $in_comment = true;
                        }
                    }else{
                        $code_lines++;
                    }
                }
            }else{
                $other_lines++;
            }
            if (preg_match('/\?\>/', trim($val))) {
                $in_php = false;
            }
        }
    }
}
echo "Lines of PHP code: $code_lines\n";
echo "Lines of other code: $other_lines\n";
echo "Lines of comments: $comment_lines\n";
$total_lines = $code_lines + $other_lines;
echo "---------------------------------------------\n";
echo "Total Lines of code: $total_lines\n";
$total_lines += $comment_lines;
echo "Total Lines(including comments): $total_lines\n";
?>
[/code]
Link to comment
Share on other sites

[!--quoteo(post=364124:date=Apr 12 2006, 01:35 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Apr 12 2006, 01:35 PM) [snapback]364124[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Awesome work!
[/quote]


i just realized that with NONE of the above are you checking for comments starting with "#"... just figured i'd throw that into the mix ;-)

sorry, couldn't resist

now, for the trick i'm working on... make this recursive to read all the sub-folders and keep a log of each folder as well as a running total... that would be awesome

one other problem i just realized... i use quick echoes quite often ("<?= ?>"), and those as well as straight "<?" tags won't be recognized by the above code... something else to keep in mind.
Link to comment
Share on other sites

Also, to just count lines of numerous files of any type in a directory, you might also want to check out the *nix command "wc".

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
%wc -l /etc/passwd /etc/services
37 /etc/passwd
2114 /etc/services
2151 total
[/quote]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.