ober Posted March 10, 2006 Share Posted March 10, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/ Share on other sites More sharing options...
AndyB Posted March 10, 2006 Share Posted March 10, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-16211 Share on other sites More sharing options...
ober Posted March 10, 2006 Author Share Posted March 10, 2006 That's kinda what I thought about doing... I'll have to play around with it. Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-16222 Share on other sites More sharing options...
AndyB Posted March 10, 2006 Share Posted March 10, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-16239 Share on other sites More sharing options...
Guest askjames01 Posted March 12, 2006 Share Posted March 12, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-16640 Share on other sites More sharing options...
ober Posted March 13, 2006 Author Share Posted March 13, 2006 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 Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-16989 Share on other sites More sharing options...
redbullmarky Posted March 13, 2006 Share Posted March 13, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-17108 Share on other sites More sharing options...
ober Posted March 13, 2006 Author Share Posted March 13, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-17122 Share on other sites More sharing options...
redbullmarky Posted March 14, 2006 Share Posted March 14, 2006 [!--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... Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-17289 Share on other sites More sharing options...
zq29 Posted March 16, 2006 Share Posted March 16, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-18106 Share on other sites More sharing options...
ober Posted March 16, 2006 Author Share Posted March 16, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-18108 Share on other sites More sharing options...
TuxToaster Posted April 11, 2006 Share Posted April 11, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-25993 Share on other sites More sharing options...
TuxToaster Posted April 11, 2006 Share Posted April 11, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-26003 Share on other sites More sharing options...
ober Posted April 12, 2006 Author Share Posted April 12, 2006 Awesome work! Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-26310 Share on other sites More sharing options...
obsidian Posted April 12, 2006 Share Posted April 12, 2006 [!--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 resistnow, 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 awesomeone 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. Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-26317 Share on other sites More sharing options...
AndyB Posted April 12, 2006 Share Posted April 12, 2006 I happy to see everyone working at turning one little glob into the All_Powerful_Code_Counting_Tool :) Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-26322 Share on other sites More sharing options...
ypirc Posted April 16, 2006 Share Posted April 16, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/4633-counting-lines-of-code/#findComment-27569 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.