How to make a little function that given a directory it returns the number of lines (counts
) of .php
files between <?php and ?>
?
Usage:
echo countsLineOfCode('D:/dir/code/');
// returns 323;
How to make a little function that given a directory it returns the number of lines (counts
) of .php
files between <?php and ?>
?
Usage:
echo countsLineOfCode('D:/dir/code/');
// returns 323;
function countLinesOfCode($path) {
$lines = 0;
$items = glob(rtrim($path, '/') . '/*');
foreach($items as $item) {
if (is_file($item) AND pathinfo($item, PATHINFO_EXTENSION) == 'php') {
$fileContents = file_get_contents($item);
preg_match_all('/<\?(?:php)?(.*?)($|\?>)/s', $fileContents, $matches);
foreach($matches[1] as $match) {
$lines += substr_count($match, PHP_EOL);
}
} else if (is_dir($item)) {
$lines += countLinesOfCode($item);
continue;
}
}
return $lines;
}
var_dump(countLinesOfCode(dirname(__FILE__))); // int(31) (works for me)
Keep in mine this is counting newlines, not end of line character ;
. For example, the line below will be considered one line...
var_dump($files); echo 'something'; exit;
It also counts lines without any PHP code, e.g. the below code will be 4 lines...
<?php
$a = 3;
It also doesn't count the <?php
or closing (if present).
Let me know if it (a) shouldn't match empty lines, (b) should match semi colons instead (will need to ensure they are not appearing within a string) and/or (c) it should match the opening and closing tag (will be easy as changing $matches[1]
in the foreach
to $matches[0]
).