-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpstorm-phpcs
More file actions
executable file
·126 lines (103 loc) · 3.19 KB
/
phpstorm-phpcs
File metadata and controls
executable file
·126 lines (103 loc) · 3.19 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env php
<?php
declare(strict_types=1);
if (\PHP_VERSION_ID < 80000) {
function str_starts_with(string $haystack, string $needle): bool {
return 0 === \strncmp($haystack, $needle, \strlen($needle));
}
}
/**
* Possible names of config file. Ordered by priority.
*/
const CONFIG_FILENAMES = [
'phpcs.xml',
'phpcs.xml.dist',
];
/**
* Name of tool to run.
*/
const TOOL_NAME = 'phpcs';
/**
* Paths to search for tool relative to directory of config dir starting with
* "/". Ordered by priority.
*/
const RELATIVE_TOOL_PATHS = [
'/tools/phpcs/vendor/bin/phpcs',
'/vendor/bin/phpcs',
];
/**
* 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;
function findOriginalFile(array $argv, string $projectDir): string {
array_shift($argv);
foreach ($argv as $arg) {
if (!\str_starts_with($arg, '-')) {
// Previous PhpStorm versions put the file to analyze below /tmp using the
// same directory structure as in the project directory.
if (1 !== \preg_match('#^/tmp/[^/]+(?<file>.+)$#', $arg, $matches)) {
throw new \RuntimeException('Could not determine original file from ' . $arg);
}
return $projectDir . $matches['file'];
}
// Recent PhpStorm versions pass the file via STDIN and make use of the --stdin-path option.
if (\str_starts_with($arg, '--stdin-path=')) {
return substr($arg, 13);
}
}
throw new \RuntimeException('Could not determine original file');
}
function buildArgs(array $argv, int $argc, string $configFile): array {
for ($i = 1; $i < $argc; ++$i) {
if (\str_starts_with($argv[$i], '--standard=')) {
$argv[$i] = '--standard=' . $configFile;
return $argv;
}
}
throw new \RuntimeException('Argument "--standard=" 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;
}
$projectDir = \getcwd();
$origFile = findOriginalFile($argv, $projectDir);
$configFile = findConfigFile($origFile, $projectDir);
$configDir = \dirname($configFile);
$argv[0] = findTool($configDir);
$argv = buildArgs($argv, $argc, $configFile);
\chdir($configDir);
\putenv('XDEBUG_MODE=off');
\passthru(PHP_BINARY . ' ' . \implode(' ', $argv), $exitCode);
exit($exitCode);