Skip to content

Commit 4e1dbe7

Browse files
authored
extracted duplicated REXSTAN_PATHFIX config, windows fixes (#19)
1 parent e4c39d0 commit 4e1dbe7

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

lib/RexStan.php

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22

33
final class RexStan {
44
/**
5-
* @return array|string
5+
* @return string
66
*/
77
static public function runFromCli() {
8-
$phpstanBinary = realpath(__DIR__.'/../vendor/bin/phpstan');
9-
$configPath = realpath(__DIR__.'/../phpstan.neon');
8+
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
9+
$phpstanBinary = realpath(__DIR__.'/../vendor/bin/phpstan.bat');
10+
} else {
11+
$phpstanBinary = realpath(__DIR__.'/../vendor/bin/phpstan');
12+
}
13+
$configPath = realpath(__DIR__.'/../phpstan.neon');
1014

1115
if (rex::getConsole()) {
12-
$cmd = 'REXSTAN_PATHFIX=1 '.$phpstanBinary .' analyse -c '. $configPath;
16+
$pathFix = true;
1317
} else {
14-
$cmd = $phpstanBinary .' analyse -c '. $configPath;
18+
$pathFix = false;
1519
}
16-
17-
$output = shell_exec($cmd);
20+
21+
$cmd = $phpstanBinary .' analyse -c '. $configPath;
22+
$output = self::execCmd($cmd, $pathFix, $lastError);
1823

1924
return $output;
2025
}
@@ -23,18 +28,15 @@ static public function runFromCli() {
2328
* @return array|string
2429
*/
2530
static public function runFromWeb() {
26-
$phpstanBinary = realpath(__DIR__.'/../vendor/bin/phpstan');
31+
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
32+
$phpstanBinary = realpath(__DIR__.'/../vendor/bin/phpstan.bat');
33+
} else {
34+
$phpstanBinary = realpath(__DIR__.'/../vendor/bin/phpstan');
35+
}
2736
$configPath = realpath(__DIR__.'/../phpstan.neon');
2837

29-
$cmd = 'REXSTAN_PATHFIX=1 '. $phpstanBinary .' analyse -c '. $configPath .' --error-format=json --no-progress 2>&1';
30-
31-
$lastError = '';
32-
set_error_handler(function ($type, $msg) use (&$lastError) { $lastError = $msg; });
33-
try {
34-
$output = @shell_exec($cmd);
35-
} finally {
36-
restore_error_handler();
37-
}
38+
$cmd = $phpstanBinary .' analyse -c '. $configPath .' --error-format=json --no-progress 2>&1';
39+
$output = self::execCmd($cmd, true, $lastError);
3840

3941
if ($output[0] === '{') {
4042
// return the analysis result as an array
@@ -48,4 +50,29 @@ static public function runFromWeb() {
4850
// return the error string as is
4951
return $output;
5052
}
53+
54+
/**
55+
* @param string $lastError
56+
* @return string
57+
*/
58+
static private function execCmd(string $cmd, bool $pathFix, &$lastError) {
59+
$lastError = '';
60+
set_error_handler(function ($type, $msg) use (&$lastError) { $lastError = $msg; });
61+
try {
62+
if ($pathFix) {
63+
// cross os compatible way of setting a env var.
64+
// the var will be inherited by the child process
65+
putenv('REXSTAN_PATHFIX=1');
66+
}
67+
$output = @shell_exec($cmd);
68+
} finally {
69+
if ($pathFix) {
70+
// remove the env var
71+
putenv('REXSTAN_PATHFIX');
72+
}
73+
restore_error_handler();
74+
}
75+
76+
return $output;
77+
}
5178
}

0 commit comments

Comments
 (0)