PHP count number of lines in every and each function and class

profileJimmymaboi
code.zip

CodeLineB/CodeLinesBhh.php

<?php $filename = 'test.txt'; if (file_exists($filename)){ // load code file $code = file_get_contents($filename) ; // get code lines $code_line = explode("\n", $code); // initiat counters $code_counter = 0 ; $comment_counter = 0 ; $function_counter = 0 ; $class_counter = 0 ; // flag to indecate if we are in multiline comment $multiline_comment = false ; $class_flag = false ; $function_flag = false ; //loop code lines foreach ($code_line as $line) { //remove spaces from code line $line = trim($line) ; if($line != "") { // check if we in multiline comment , then add counter and check if comment end if($multiline_comment) { $comment_counter++; // cehck if line start with /* or end with */ then switch multiline falg off if(substr($line,0,2) == "/*" || substr($line,-2) == "*/") $multiline_comment = false ; } else { // start parsing comment //1- single line comment : if(substr($line,0,2) == "//") { $comment_counter++ ; } // cehck speacail case if /* */ in one line if(substr($line,0,2)=="/*" && substr($line,-2)=="*/") { $comment_counter++ ; } // check if multiline comment start if(substr($line,0,2)=="/*") { $comment_counter++ ; $multiline_comment = true ; } // here we will check for class & functions elseif(strtoupper(substr($line,0,5))== "CLASS" || strtoupper(substr($line,0,8)) == "FUNCTION") { if(strtoupper(substr($line,0,5))== "CLASS") { $class_counter++; $class_flag = true ; } else { $function_counter ++ ; $function_flag = true ; } // now we get the class || function code line $code_counter++; } else { // now we get the code line $code_counter++; } } } } echo "Total of lines : $code_counter <br>"; echo "Number of Function : $function_counter <br>"; echo "Number of Class : $class_counter <br>"; echo "Number of Comments : $comment_counter <br>"; }else{ echo "The file $filename does not exist."; } ?> <br> <textarea rows="15" style='width:50%'> <?=$code?> </textarea>

CodeLineB/test.txt

<?php // load code file $code = file_get_contents("test.txt") ; // get code lines $code_line = explode("\n", $code); // initiat counters $code_counter = 0 ; $commint_counter = 0 ; // flag to indecate if we are in multiline commint $multiline_commint = false ; class TestClass { // test class for test the code function A() { return true ; } function B() { /* in this function we will test multiline comment I am looooong comment */ if(A()) return false ; } } //loop code lines foreach ($code_line as $line) { //remove spaces from code line $line = trim($line) ; if($line != "") { // check if we in multiline commint , then add counter and check if comment end if($multiline_commint) { $commint_counter++; // cehck if line start with /* or end with */ then switch multiline falg off if(substr($line,0,2)=="/*" || substr($line,-2)=="*/") $multiline_commint = false ; } else { // start parsing commints //1- single line commints : if(substr($line,0,2) == "//") { $commint_counter++ ; } // cehck speacail case if /* */ in one line elseif(substr($line,0,2)=="/*" && substr($line,-2)=="*/") { $commint_counter++ ; } // check if multyline comment start elseif(substr($line,0,2)=="/*") { $commint_counter++ ; $multiline_commint = true ; } else { // now we get the code line $code_counter++; } } } } echo "Code Lines : $code_counter <br>"; echo "Commints : $commint_counter <br>"; ?>

__MACOSX/CodeLineB/._test.txt