Skip to content

Instantly share code, notes, and snippets.

@k-msalehi
Last active October 13, 2018 17:36
Show Gist options
  • Save k-msalehi/9fc835f73a12dfe8784cf1db60a992b9 to your computer and use it in GitHub Desktop.
Save k-msalehi/9fc835f73a12dfe8784cf1db60a992b9 to your computer and use it in GitHub Desktop.
Count number of lines by suffix of files
<?php
/**
* get files in folders and subfolders with $extensions extensions in $path.
* @param array $extentios
* @param string $path optinal. default is path of execution directory
*/
//default path is current directory and sub directories
function listFolderFiles($extensions ,$path = '.')
{
//files to count line numbers sort in $files
static $files;
//extensions of files that you will count line numbers
$extensions = ['php'];
foreach (new DirectoryIterator($path) as $fileInfo) {
if (!$fileInfo->isDot()) {
if (in_array($fileInfo->getExtension(), $extensions)) {
$files[] = $fileInfo->getPathname();
}
if ($fileInfo->isDir()) {
listFolderFiles($fileInfo->getPathname());
}
}
}
return $files;
}
function counter($extensions, $path = '.')
{
$counter = 0;
$files = (listFolderFiles($extensions,$path));
foreach ($files as $key) {
$counter = $counter + count(file($key));
}
return $counter;
}
//usage
echo counter(['php','css',], 'path/to/dir');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment