-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpstorm-phpstan
More file actions
executable file
·108 lines (86 loc) · 2.54 KB
/
phpstorm-phpstan
File metadata and controls
executable file
·108 lines (86 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env php
<?php
declare(strict_types=1);
/**
* Possible names of config file. Ordered by priority.
*/
const CONFIG_FILENAMES = [
'phpstan.neon',
'phpstan.neon.dist',
];
/**
* Name of tool to run.
*/
const TOOL_NAME = 'phpstan';
/**
* Paths to search for tool relative to directory of config dir starting with
* "/". Ordered by priority.
*/
const RELATIVE_TOOL_PATHS = [
'/tools/phpstan/vendor/bin/phpstan',
'/vendor/bin/phpstan',
];
/**
* Absolute path to tool, name of command in PATH, or NULL. Used if tool could
* not be found in relative paths.
*/
const TOOL_FALLBACK = TOOL_NAME;
$fileToAnalyze = $argv[$argc - 1];
function buildArgs(array $argv, int $argc, string $configFile): array {
for ($i = 1; $i < $argc; ++$i) {
if ('-c' === $argv[$i]) {
$argv[$i + 1] = $configFile;
return $argv;
}
}
throw new \RuntimeException('Argument "-c" not found');
}
function findConfigFile(string $origFile, string $projectDir): string {
$configDir = \dirname($origFile);
$projectParentDir = dirname($projectDir);
while ($configDir !== $projectParentDir && $configDir !== '/') {
foreach (CONFIG_FILENAMES as $configFilename) {
$configFile = $configDir . '/' . $configFilename;
if (\file_exists($configFile)) {
return $configFile;
}
}
$configDir = \dirname($configDir);
}
\fprintf(STDERR, "No configuration file found for $origFile\n");
exit(0);
}
function findTool(string $configDir): string {
foreach (RELATIVE_TOOL_PATHS as $path) {
$tool = $configDir . $path;
if (\file_exists($tool)) {
return $tool;
}
}
$command = locateCommand(TOOL_FALLBACK);
if (NULL !== $command) {
return $command;
}
throw new \RuntimeException(TOOL_NAME . ' not found');
}
function locateCommand(?string $command): ?string {
if (NULL === $command) {
return NULL;
}
return \exec('which ' . $command) ?: NULL;
}
// File to analyze should be below /tmp with the same directory structure as in the project directory.
if (1 !== \preg_match('#^/tmp/[^/]+(?<file>.+)$#', $fileToAnalyze, $matches)) {
throw new \RuntimeException('Could not determine original file from ' . $fileToAnalyze);
}
$projectDir = \getcwd();
$origFile = $projectDir . $matches['file'];
$configFile = findConfigFile($origFile, $projectDir);
$configDir = \dirname($configFile);
$tool = findTool($configDir);
$argv[0] = $tool;
$argv = buildArgs($argv, $argc, $configFile);
\chdir($configDir);
\putenv('XDEBUG_MODE=off');
\passthru(PHP_BINARY . ' ' . \implode(' ', $argv), $exitCode);
exit($exitCode);