Skip to content

Instantly share code, notes, and snippets.

@mrspartak
Last active December 15, 2015 22:58
Show Gist options
  • Select an option

  • Save mrspartak/5336322 to your computer and use it in GitHub Desktop.

Select an option

Save mrspartak/5336322 to your computer and use it in GitHub Desktop.
Count lines of code Usage: run in current directory, or just pass the path to directory as parameter in CLI. Returns: json
<?php
ini_set('xdebug.max_nesting_level', 0);
$dir = $argv[1] ? $argv[1] : dirname(__FILE__);
if(!is_dir($dir))
exit('Error');
$global = array();
read($dir);
function read($path) {
global $global;
$dir = opendir($path);
while (($file = readdir($dir)) !== false) {
if ($file == '.' || $file == '..')
continue;
$filepath = $path . '/' . $file;
if (is_link($filepath))
continue;
if (is_file($filepath)) {
$info = pathinfo($filepath);
$global['ext'][ $info['extension'] ] += count( file($filepath) );
$global['files']++;
}
else if (is_dir($filepath)) {
$global['recursions']++;
read($filepath);
}
}
closedir($dir);
}
echo json_encode($global);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment