Skip to content

Instantly share code, notes, and snippets.

@divinity76
Created December 13, 2021 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divinity76/b62c8a08292f60c384e00f71e690b567 to your computer and use it in GitHub Desktop.
Save divinity76/b62c8a08292f60c384e00f71e690b567 to your computer and use it in GitHub Desktop.
php linter built-in run
<?php
declare(strict_types = 1);
if (! defined("APP_ROOT_DIR")) {
define("APP_ROOT_DIR", realpath(__DIR__ . "/../"));
}
// https://www.imdb.com/title/tt2741602/
$blacklist = array(
'/docroot/external_libs/',
'/docroot/phpmyadmin/',
'/vendor/',
'/data/vitec/',
'/docroot/js/highcharts/',
'/hotpatch_backups/',
'/.git/',
'/tests/boilerplate.php',
);
$blacklist_regex = '\\(';
foreach ($blacklist as $blacklisted) {
$blacklist_regex .= '.*' . $blacklisted . '.*\\|';
}
$blacklist_regex = substr($blacklist_regex, 0, - strlen('\\|'));
$blacklist_regex .= '\\)';
$extensions = array(
"php"
);
$find_regex = ".*\\.\\(" . implode("\\|", $extensions) . "\\)";
// find -iname "*.php" -print0 | xargs -0 -n 1 -i sh -c "php -l {} || true" | grep -v -E '^No syntax errors detected in*'
// $cmd looks like:
// find '/home/hans/projects/app' -regex '.*\.\(php\)' -not -regex '\(.*/docroot/external_libs/.*\|.*/docroot/phpmyadmin/.*\|.*/vendor/.*\|.*/data/vitec/.*\|.*/docroot/js/highcharts/.*\|.*/hotpatch_backups/.*\|.*/.git/.*\)' -print0 2>/dev/null | xargs -0 -n 1 -I '{}' sh -c "V=\$(php -l \"{}\" || /bin/echo \"\n\n\" ); /bin/echo -e -n \"\033[2K\" && echo \$V && /bin/echo -e -n \"\033[1A\" || printf \n"
$cmd = implode(" ", array(
'find',
escapeshellarg(APP_ROOT_DIR),
'-regex ' . escapeshellarg($find_regex),
'-not -regex ' . escapeshellarg($blacklist_regex),
'-print0',
'2>/dev/null',
'|',
// i went a bit overboard here..
// /bin/echo -e -n \"\\033[2K\" means "clear everything on the current line"
// /bin/echo -e -n \"\\033[1A\" means "go up to the previous line, but only if php didn't find any errors on the last file"
// the || printf \n makes sure sh always returns 0 to not terminate xargs (xargs terminate if the program returns nonzero)
// the reason for all that stuff is to make kindof-cool-looking progress indication: all files are printed on a single line saying
// "no errors found in this file", except those files with an actual error
'xargs -0 -n 1 -I \'{}\' sh -c "V=\\$(php -l \"{}\" || /bin/echo \\"\n\n\\" ); /bin/echo -e -n \"\\033[2K\" && echo \\$V && /bin/echo -e -n \"\\033[1A\" || printf \\n"'
));
echo "executing:\n{$cmd}\nthis may take several minutes..\n\n";
$ret = null;
passthru($cmd, $ret);
echo "\n";
die($ret);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment