diff --git a/lib/RexStan.php b/lib/RexStan.php index b0f1860a7..0fc287547 100644 --- a/lib/RexStan.php +++ b/lib/RexStan.php @@ -17,18 +17,24 @@ final class RexStan /** * @param int $exitCode * @param-out int $exitCode + * @param-out string $errorOutput * * @return string */ - public static function runFromCli(&$exitCode) + public static function runFromCli(&$exitCode, ?string $path, ?int $level, ?string &$errorOutput) { $phpstanBinary = self::phpstanBinPath(); $configPath = self::phpstanConfigPath(__DIR__.'/../phpstan.neon'); - $cmd = $phpstanBinary .' analyse -c '. $configPath; - $output = RexCmd::execCmd($cmd, $stderrOutput, $exitCode); + $cmd = $phpstanBinary .' analyse --no-progress -c '. $configPath; + if ($path !== null) { + $cmd .= ' '. escapeshellarg($path); + } + if ($level !== null) { + $cmd .= ' --level '. $level; + } - return $output; + return RexCmd::execCmd($cmd, $errorOutput, $exitCode); } /** diff --git a/lib/command.php b/lib/command.php index a9879ceee..fc13b9ffd 100644 --- a/lib/command.php +++ b/lib/command.php @@ -3,7 +3,9 @@ namespace rexstan; use rex_console_command; +use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class rexstan_command extends rex_console_command @@ -16,13 +18,40 @@ protected function configure() $this ->setName('rexstan:analyze') ->setDescription('Run static code analysis') + ->addArgument('path', InputArgument::OPTIONAL, 'File or directoy path to analyze') + ->addOption('level', 'l', InputOption::VALUE_REQUIRED, 'Rule level (0-9)') ; } protected function execute(InputInterface $input, OutputInterface $output) { - echo RexStan::runFromCli($exitCode); + $io = $this->getStyle($input, $output); + $path = null; + $level = null; + if ($input->getArgument('path') !== null) { + $analyzePath = getcwd() .'/../'. $input->getArgument('path'); + $path = realpath($analyzePath); + + if ($path === false) { + throw new \Exception('Invalid path: '. $input->getArgument('path')); + } + } + + if ($input->hasOption('level')) { + $level = $input->getOption('level'); + if ($level !== null && !preg_match('/^[0-9]$/', $level)) { + throw new \Exception('Invalid level: '. $level); + } + } + + $result = RexStan::runFromCli($exitCode, $path, $level, $errorOutput); + + if ($result !== '') { + $io->write($result); + } + + // pass PHPStan exit code 1:1 return $exitCode; } }