Skip to content

Commit

Permalink
added rex console command
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jun 11, 2022
1 parent e0b9d31 commit 993dc79
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
35 changes: 35 additions & 0 deletions lib/RexStan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

final class RexStan {
/**
* @return array|string
*/
static public function runFromCli() {
$phpstanBinary = realpath(__DIR__.'/../vendor/bin/phpstan');
$configPath = realpath(__DIR__.'/../phpstan.neon');

$cmd = $phpstanBinary .' analyse -c '. $configPath;
$output = shell_exec($cmd);

return $output;
}

/**
* @return array|string
*/
static public function runFromWeb() {
$phpstanBinary = realpath(__DIR__.'/../vendor/bin/phpstan');
$configPath = realpath(__DIR__.'/../phpstan.neon');

$cmd = $phpstanBinary .' analyse -c '. $configPath .' --error-format=json --no-progress 2>&1';

$output = shell_exec($cmd);
if ($output[0] === '{') {
// return the analysis result as an array
return json_decode($output, true);
}

// return the error string as is
return $output;
}
}
2 changes: 1 addition & 1 deletion lib/RexStanUserConfig.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class RexStanUserConfig {
final class RexStanUserConfig {
/**
* @param int $level
* @param array<string> $paths
Expand Down
24 changes: 24 additions & 0 deletions lib/command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Symfony\Component\Console\Exception\InvalidArgumentException;
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
{
protected function configure()
{
$this
->setName('rexstan:analyze')
->setDescription('Run static code analysis')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
echo RexStan::runFromCli();

return 0;
}
}
3 changes: 3 additions & 0 deletions package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ page:
settings: { title: translate:settings }
readme: { title: translate:readme, subPath: README.md }

console_commands:
rexstan:analyze: rexstan_command

requires:
php:
version: '>=7.2'
Expand Down
12 changes: 3 additions & 9 deletions pages/analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@

/** @var rex_addon $this */

$phpstanBinary = realpath(__DIR__.'/../vendor/bin/phpstan');
$configPath = realpath(__DIR__.'/../phpstan.neon');
$phpstanResult = RexStan::runFromWeb();

$cmd = $phpstanBinary .' analyse -c '. $configPath .' --error-format=json --no-progress 2>&1';

$output = shell_exec($cmd);
if ($output[0] === '{') {
$phpstanResult = json_decode($output, true);
} else {
echo '<span class="rexstan-error">'.nl2br(rex_escape($output)).'</span>';
if (is_string($phpstanResult)) {
echo '<span class="rexstan-error">'.nl2br(rex_escape($phpstanResult)).'</span>';
return;
}

Expand Down

0 comments on commit 993dc79

Please sign in to comment.