Instantly share code, notes, and snippets.

Embed
What would you like to do?
A wrapper for phpstan, so you can use PHP7.0 and PHP7.1
#!/usr/bin/php
<?php
use Composer\XdebugHandler\XdebugHandler;
require_once __DIR__ . '/YOUR_PATH_THE_AUTOLOADER.php';
$xdebug = new XdebugHandler('phpstan-code-check');
$xdebug->check();
unset($xdebug);
if (isset($argv[1])) {
$FILES = $argv[1];
} else {
$FILES = '.';
}
if (isset($argv[2])) {
$LEVEL = $argv[2];
} else {
$LEVEL = '2';
}
if (isset($argv[3])) {
$CONFIG = $argv[3];
} else {
$CONFIG = __DIR__ . '/YOUR_PATH_TO_THE_CONFIG_DIR/phpstan_legacy.neon';
}
if (isset($argv[4])) {
$EXTRA_DEBUG_TEXT = $argv[4];
} else {
$EXTRA_DEBUG_TEXT = '';
}
check_code_phpstan($FILES, $LEVEL, $CONFIG, $EXTRA_DEBUG_TEXT);
/**
* @return string
*/
function get_newest_local_php_version(): string {
$phpCmd = 'php';
for ($i = 2; $i > 0; $i--) {
$phpCmdTmp = 'php7.' . $i;
if (shell_command_exist($phpCmdTmp)) {
return $phpCmdTmp;
}
}
for ($i = 6; $i > 0; $i--) {
$phpCmdTmp = 'php5.' . $i;
if (shell_command_exist($phpCmdTmp)) {
return $phpCmdTmp;
}
}
return $phpCmd;
}
/**
* @param string $FILES
* @param string $LEVEL
* @param string $CONFIG
* @param string $EXTRA_DEBUG_TEXT
*/
function check_code_phpstan($FILES, $LEVEL, $CONFIG, $EXTRA_DEBUG_TEXT) {
$newest_local_php_version = get_newest_local_php_version();
if ($newest_local_php_version >= 'php7.1') {
$phpstan_phar = 'phpstan_php7_1.phar';
} else {
$phpstan_phar = 'phpstan_php7_0.phar';
}
echo 'Running PhpStan [' . $phpstan_phar . '] ' . ($EXTRA_DEBUG_TEXT ? '(' . $EXTRA_DEBUG_TEXT . ')' : '') . ' ...' . "\n";
// https://github.com/phpstan/phpstan
$phpstan_cmd = $newest_local_php_version . ' ' . 'YOUR_PATH_THE_VENDOR_DIR/' . $phpstan_phar . ' analyse \
--level ' . $LEVEL . ' \
--memory-limit 4G \
--no-progress \
--configuration ' . $CONFIG . ' \
' . $FILES . '
';
// DEBUG
// echo "\n" . "command: " . $phpstan_cmd . "\n";
exec($phpstan_cmd, $phpstan_output, $return);
if ($return != 0) {
echo print_r($phpstan_output, true) . "\n"
. 'PhpStan [' . $return . ']: ¯\(°_o)/¯ Please fix the code smells...' . "\n";
exit($return);
}
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment