Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement rexstan:analyze path arg and level option #584

Merged
merged 8 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/RexStan.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
31 changes: 30 additions & 1 deletion lib/command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}