Last active
September 1, 2018 03:07
-
-
Save johnalarcon/115dad7742ca586da7de028c606ea2f4 to your computer and use it in GitHub Desktop.
This bit of code takes a relative path, scans that directory, then determines how many lines of code and how many lines of comments are within.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$dir = 'C:/xampp/htdocs/wordpress/wp-content/plugins/my-plugin-name/'; | |
echo get_code_size($dir); | |
function get_code_size($dir) { | |
// The directory to scan; include trailing slash. | |
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST); | |
// Get code from each file into an array. | |
$files = array(); | |
foreach ($iterator as $result) { | |
if (!isset($weight)) $weight = 0; | |
if (strstr($result, 'ignore') || (is_dir($result))) { | |
continue; | |
} | |
$file = $result->getPathname(); | |
$weight += filesize($file); | |
$files[$file] = file($file); | |
} | |
// Get number of files in plugin | |
$total_files = count($files); | |
// Get number of lines in all files | |
$total_lines = 0; | |
foreach ($files as $file) { | |
$total_lines += count($file); | |
} | |
// Initialize vars for lines of comments and blanks. | |
$lines_of_comments = 0; | |
$lines_of_blanks = 0; | |
// Iterate over files, then lines. | |
foreach ($files as $target_file=>$lines) { | |
foreach ($lines as $n=>$line) { | |
// Trim the line. | |
$line = trim($line); | |
// Remove blank lines; add each line to the count. | |
if ($line==='') { | |
unset($files[$target_file][$n]); | |
$lines_of_blanks++; | |
continue; | |
} | |
// Remove comments; add each line to the count. | |
if (substr($line, 0, 2)==='//' || | |
substr($line, 0, 3)==='/**' || | |
substr($line, 0, 1)==='*' || | |
substr($line, 0, 2)==='*/') { | |
unset($files[$target_file][$n]); | |
$lines_of_comments++; | |
continue; | |
} | |
} | |
} | |
// Total lines of code and comments. | |
$lines_of_code = $total_lines - $lines_of_blanks - $lines_of_comments; | |
$lines_of_comments = $lines_of_comments + $lines_of_blanks; | |
// Percentage of code and comments. | |
$percent_code = round($lines_of_code / $total_lines * 100, 1); | |
$percent_comments = (round(100-$percent_code, 1)); | |
$plugin_info = '<p>'; | |
$plugin_info .= 'Total size: '.round($weight/1000, 1).' Kb<br />'; | |
$plugin_info .= 'There are '.number_format($total_files).' files in the plugin.<br>'; | |
$plugin_info .= 'There are '.number_format($lines_of_code).' lines of code.<br>'; | |
$plugin_info .= 'There are '.number_format($lines_of_comments).' blank or commented lines.<br>'; | |
$plugin_info .= 'The plugin is '.$percent_code.'% code and '.$percent_comments.'% comments.<br>'; | |
$plugin_info .= '</p>'; | |
return $plugin_info; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment