Skip to content

Commit 993dc79

Browse files
committed
added rex console command
1 parent e0b9d31 commit 993dc79

File tree

5 files changed

+66
-10
lines changed

5 files changed

+66
-10
lines changed

lib/RexStan.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
final class RexStan {
4+
/**
5+
* @return array|string
6+
*/
7+
static public function runFromCli() {
8+
$phpstanBinary = realpath(__DIR__.'/../vendor/bin/phpstan');
9+
$configPath = realpath(__DIR__.'/../phpstan.neon');
10+
11+
$cmd = $phpstanBinary .' analyse -c '. $configPath;
12+
$output = shell_exec($cmd);
13+
14+
return $output;
15+
}
16+
17+
/**
18+
* @return array|string
19+
*/
20+
static public function runFromWeb() {
21+
$phpstanBinary = realpath(__DIR__.'/../vendor/bin/phpstan');
22+
$configPath = realpath(__DIR__.'/../phpstan.neon');
23+
24+
$cmd = $phpstanBinary .' analyse -c '. $configPath .' --error-format=json --no-progress 2>&1';
25+
26+
$output = shell_exec($cmd);
27+
if ($output[0] === '{') {
28+
// return the analysis result as an array
29+
return json_decode($output, true);
30+
}
31+
32+
// return the error string as is
33+
return $output;
34+
}
35+
}

lib/RexStanUserConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
class RexStanUserConfig {
3+
final class RexStanUserConfig {
44
/**
55
* @param int $level
66
* @param array<string> $paths

lib/command.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Exception\InvalidArgumentException;
4+
use Symfony\Component\Console\Input\InputInterface;
5+
use Symfony\Component\Console\Input\InputOption;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
class rexstan_command extends rex_console_command
9+
{
10+
protected function configure()
11+
{
12+
$this
13+
->setName('rexstan:analyze')
14+
->setDescription('Run static code analysis')
15+
;
16+
}
17+
18+
protected function execute(InputInterface $input, OutputInterface $output)
19+
{
20+
echo RexStan::runFromCli();
21+
22+
return 0;
23+
}
24+
}

package.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ page:
1212
settings: { title: translate:settings }
1313
readme: { title: translate:readme, subPath: README.md }
1414

15+
console_commands:
16+
rexstan:analyze: rexstan_command
17+
1518
requires:
1619
php:
1720
version: '>=7.2'

pages/analysis.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22

33
/** @var rex_addon $this */
44

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

8-
$cmd = $phpstanBinary .' analyse -c '. $configPath .' --error-format=json --no-progress 2>&1';
9-
10-
$output = shell_exec($cmd);
11-
if ($output[0] === '{') {
12-
$phpstanResult = json_decode($output, true);
13-
} else {
14-
echo '<span class="rexstan-error">'.nl2br(rex_escape($output)).'</span>';
7+
if (is_string($phpstanResult)) {
8+
echo '<span class="rexstan-error">'.nl2br(rex_escape($phpstanResult)).'</span>';
159
return;
1610
}
1711

0 commit comments

Comments
 (0)