Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save clementtalleu/3b670ea98f75f990bcf403d94ee74528 to your computer and use it in GitHub Desktop.
Save clementtalleu/3b670ea98f75f990bcf403d94ee74528 to your computer and use it in GitHub Desktop.
<?php
$filePath = $argv[1];
main($filePath);
function main($path) {
$startTime = microtime(true);
$memoryBefore = memory_get_usage();
$filesInspected = 0;
if (\is_file($path)) {
if (!is_PHP_file($path)) {
echo "Not a php file.\n";
return;
}
$occurrences = check_dump_die_occurrences($path);
$memoryAfter = memory_get_usage();
$elapsedTime = microtime(true) - $startTime;
display_time_and_files($startTime, $filesInspected, $occurrences, $memoryBefore, $memoryAfter, $elapsedTime);
return;
}
if (is_dir($path)) {
$occurrences = 0;
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
foreach ($iterator as $file) {
if ($file->isDir()) {
continue;
}
$filePath = $file->getPathname();
if (is_PHP_file($filePath)) {
$occurrences += check_dump_die_occurrences($filePath);
$filesInspected++;
}
}
$memoryAfter = memory_get_usage();
$elapsedTime = microtime(true) - $startTime;
display_time_and_files($startTime, $filesInspected, $occurrences, $memoryBefore, $memoryAfter, $elapsedTime);
}
}
function is_PHP_file($path) {
return strtolower(pathinfo($path, PATHINFO_EXTENSION)) === 'php';
}
function check_dump_die_occurrences($filePath) {
$occurrencesFound = 0;
$lines = file($filePath);
foreach ($lines as $lineNumber => $line) {
$line = strtolower($line);
if (str_contains($line, 'dump(')) {
echo "$filePath: dump/var_dump found on line " . ($lineNumber + 1) . "\n";
$occurrencesFound++;
}
if (str_contains($line, 'die(') || str_contains($line, 'die;')) {
echo "$filePath: dump/var_dump found on line " . ($lineNumber + 1) . "\n";
$occurrencesFound++;
}
}
return $occurrencesFound;
}
function display_time_and_files($startTime, $filesInspected, $occurrences, $memoryBefore, $memoryAfter, $elapsedTime) {
$memoryInBytes = $memoryAfter - $memoryBefore;
$memoryInKb = $memoryInBytes / 1024;
echo "Elapsed time: $elapsedTime\n";
echo "Number of files inspected: $filesInspected\n";
echo "$occurrences occurrences found\n";
echo round($memoryInKb, 2) . "kb memory allocated\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment