counting line numbers in my project

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

counting line numbers in my project

Post by mydimension »

has anyone tried doing something like this? i would like to have a "fun facts section of my site and on this page show the current number of lines of code in my project. couls someone point me in a direction to go with this? i would really appreciate it.

(note: i am not asking you to code this for me ;) )
User avatar
jonsyd
Forum Commoner
Posts: 36
Joined: Fri Sep 20, 2002 9:28 am
Location: Oxford, UK

Post by jonsyd »

how about just counting the number of ";" s? Thats what I do when a client asks why it takes 2 weeks to program something. Telling them you've written 3500 LOC tends to shut them up :)
davro
Forum Newbie
Posts: 8
Joined: Fri Nov 01, 2002 6:54 am

Post by davro »

Try some thing like this
Only had a moment to test it, but it should return your selected file $file
In a textarea, enlightened by the length of the file.

Code: Select all

<? 

$file = "links.php"; 
$contents = file($file); 
$length = sizeof($contents); 

for($i=0; $i<$length; $i++) 
&#123; 
// buffers length, textarea for resizing 
&#125; 

// display enlightened form for display 
print "<p>File: $file has $length lines of text</p>\n"; 
print "<textarea border='0' name='text_area' wrap='off' rows='$i' cols='85'>\n"; 

for($j=0; $j<$length; $j++) 
&#123; 
// show the file line by line, in the textarea. 
print "$contents&#1111;$j]"; 
&#125; 

// finish textarea 
print "</textarea>\n"; 
?>
Hope helped

davro
Last edited by davro on Fri Nov 01, 2002 7:01 am, edited 2 times in total.
seg
Forum Commoner
Posts: 38
Joined: Thu Oct 31, 2002 12:08 pm
Location: Northern, VA
Contact:

Post by seg »

Well. if the file was named .phps, or anything but .php then it is easy to do. A simple 'while !eof $fs' scheme will work fine. If it is named .php, and constantly make a renamed copy is just too much work, (i can sympathize with that) then I'm not sure. I would think that if you used PHP to access another PHP file, even just to read the number of lines, it might try to execute the code. I could be wrong, though, I've never tried.

Code: Select all

&lt;?php
 $fd = fopen($bufferfile, "r");
 while (!feof ($fd)) {
// count code
}
close($fd);
?&gt;
that example has a LOT of overhead, and I don't recommend using it literally. I grabbed it from one of my corporate projects here. But the concept of it would work great for counting lines.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

when a script is executed a struct _zend_op_array is given as parameter to the worker C-function. It contains a count for all known opcodes. If only one could access this :D
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

or simply do a:

Code: Select all

Currently there are: <b>

<?php

$file = ("yourfile.whatever");
$count = count($file);

print "$count";

?>

</b> lines in the project file
User avatar
jonsyd
Forum Commoner
Posts: 36
Joined: Fri Sep 20, 2002 9:28 am
Location: Oxford, UK

Post by jonsyd »

sorry to repeat myself, but why not count the ";" s?

Surely we want to count the number of LOC, not the number of lines in each file (unless you count blank lines, HTML, brackets and <?php / ?> as LOC)
seg
Forum Commoner
Posts: 38
Joined: Thu Oct 31, 2002 12:08 pm
Location: Northern, VA
Contact:

Post by seg »

jonsyd wrote:sorry to repeat myself, but why not count the ";" s?

Surely we want to count the number of LOC, not the number of lines in each file (unless you count blank lines, HTML, brackets and <?php / ?> as LOC)
I'm glad you expounded on that, good point. As long as you can read a .php file via a php script without executing it, then yeah, its easy to count the ';'s I'm tempted to write the code when I get home and post it.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

thanx for the ideas. im leaning towards counting the semi-colons. i have about ten files to count through in two seperate directores, so the overhead is going to be substatial. good thing its only going to be one one page.

thx again guys/gals
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

for anyone interested, i have written a script which counts the number semi-colons and the number of lines containg either an opened brace or a closed brace BUT NOT both. here it is for your viewing pleasure.

(edit: i mod'ed the function so that all variables were declared inside the function)

Code: Select all

&lt;?php
function countCodeLines ($directory) {
	static $totalCodeLines;
	$dir = opendir($directory);
	
	while ($item = readdir($dir)) {
		if ((is_dir($item)) &amp;&amp; ($item != ".") &amp;&amp; ($item != "..")) {
			countCodeLines($item); //recursive directory walking
		} elseif (strrchr($item, ".") == ".php") { //count only php files
			$lines = file($directory . "/" . $item);
			
			foreach ($lines as $line) {
				//count lines containg a semi-colon or
				//lines containing either an opened brace or a closed brace BUT NOT both
				if (preg_match("/\;/", $line) xor (preg_match("/\{/", $line) xor preg_match("/\}/", $line))) {
					$totalCodeLines++;
				}
			}
		}
	}
	return $totalCodeLines; //return the final result
}

print countCodeLines($_SERVER&#1111;'DOCUMENT_ROOT']);
?&gt;
note: wherever you see & replace with &

again i thank you all for your input. consider this your pay ;) :D
Post Reply