Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Line lengths for controllers and views recursively in a folder.
<?php
snap('init');
define('LL_TAB_SIZE', 4);
define('LL_TOO_LONG', 130);
define('LL_WAY_TOO_LONG', 220);
ini_set('html_errors', 0);
header('Content-type: text/plain');
ini_set('auto_detect_line_endings', '1');
snap('set-up');
$dir = __DIR__ . '/baanreserveren/source';
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
$files = iterator_to_array($it);
snap('find files');
$files = array_keys($files);
// var_dump(count($files));
printf("% 10d files found\n", count($files));
$files = array_filter($files, function($file) {
return
preg_match('/\.(html|php)$/', $file) &&
preg_match('#[\\\\/](Controllers|views|Models|include)[\\\\/]#', $file) &&
!preg_match('#[\\\/]include[\\\/].+[\\\/]#', $file);
});
$relevant = count($files);
printf("% 10d files are relevant\n", $relevant);
snap('filter files');
$_name = function($name) use ($dir) {
return substr($name, ll_line_length($dir)+1);
};
$contents = array_combine(array_map($_name, $files), array_map('file', $files));
// print_r($contents);
snap('read files');
foreach ( $contents as $file => $lines ) {
$lines = array_map('ll_line_length', $lines);
$max = $lines ? max($lines) : 0;
$line = array_search($max, $lines);
// echo $file . ' => ' . $max . "\n";
$contents[$file] = $max * 1e6 + $line + 1;
}
snap('calc lines');
arsort($contents, SORT_NUMERIC);
snap('sort files');
$way_too_long = 0;
$too_long = array_reduce($contents, function($index, $num) use (&$way_too_long) {
$length = floor($num / 1e6);
$line = $num - $length * 1e6;
$length > LL_TOO_LONG && $index++;
$length > LL_WAY_TOO_LONG && $way_too_long++;
return $index;
}, 0);
snap('splice length constants');
printf("% 4d / %d files (% 2d %%) have lines too long (%d cols)\n", $too_long, $relevant, ceil($too_long / $relevant * 100), LL_TOO_LONG);
printf("% 4d / %d files (% 2d %%) have lines WAY too long (%d cols)\n", $way_too_long, $relevant, ceil($way_too_long / $relevant * 100), LL_WAY_TOO_LONG);
echo "\n";
snap('print summary');
$contents = array_map(function($num) {
$length = floor($num / 1e6);
$line = $num - $length * 1e6;
return $length . ' (line ' . $line . ')';
}, $contents);
snap('prepare results');
$way_too_long and array_splice($contents, $way_too_long, 0, array('^ WAY TOO LONG ^ WAY TOO LONG ^ WAY TOO LONG ^ WAY TOO LONG ^ WAY TOO LONG ^ WAY TOO LONG ^ WAY TOO LONG ^'));
array_splice($contents, $too_long, 0, array('^ STILL TOO LONG ^ STILL TOO LONG ^ STILL TOO LONG ^ STILL TOO LONG ^ STILL TOO LONG ^ STILL TOO LONG ^'));
print_r($contents);
print_r($snaps);
/*
Array
(
[views\admin\clubs\colorchart.tpl.html] => 2098 (2)
[controllers\inc.cls.mod_reports.php] => 868 (122)
...
[views\reservations\confirm.tpl.html] => 413 (114)
[0] => ^ WAY TOO LONG ^ WAY TOO LONG ^ WAY TOO LONG ^
[views\blockreservations\confirm.tpl.html] => 408 (83)
...
[views\statistics\statistics2_frame.tpl.html] => 161 (41)
[1] => ^ STILL TOO LONG ^ STILL TOO LONG ^ STILL TOO LONG ^
[views\groups\overview.tpl.html] => 160 (6)
...
*/
function ll_line_length($line) {
return strlen(str_replace("\t", str_repeat('_', LL_TAB_SIZE), $line));
}
function snap($name) {
global $snaps, $_start, $_last;
$snaps or $snaps = array();
$_start or $_start = microtime(1);
$snaps[] = array(
$name,
round((microtime(1) - $_start) * 1000),
$_last ? round((microtime(1) - $_last) * 1000) : 0,
round(memory_get_peak_usage() / 1e6, 2),
);
$_last = microtime(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.