-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrun.php
98 lines (83 loc) · 2.7 KB
/
run.php
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
<?php
declare(strict_types=1);
// Autoloader
if (
!(is_file($file = ($vendorDir = __DIR__ . '/vendor') . '/autoload.php') && include $file) &&
!(is_file($file = ($vendorDir = __DIR__ . '/../..') . '/autoload.php') && include $file)
) {
fwrite(STDERR, "Install packages using Composer.\n");
exit(1);
}
// Argument Parsing
$paths = [];
$preset = null;
$dryRun = true;
for ($i = 1; $i < $argc; $i++) {
$arg = $argv[$i];
if ($arg === '--preset' && isset($argv[$i + 1])) {
$preset = $argv[++$i];
} elseif ($arg === '--fix' || $arg === 'fix') {
$dryRun = false;
} elseif ($arg === 'check') {
$dryRun = true;
} elseif ($arg === '--help' || $arg === '-h') {
echo "Usage: php fix.php [check|fix] [--preset <name>] [path1 path2 ...]\n";
echo " check (default): Run tools in dry-run mode.\n";
echo " fix: Run tools and apply fixes.\n";
echo " --preset <name>: Specify preset (e.g., php81). Autodetected if omitted.\n";
echo " path1 path2 ...: Specific files or directories to process. Defaults to src/, tests/ or ./\n";
exit(0);
} elseif (!str_starts_with($arg, '-')) {
$paths[] = $arg;
} else {
fwrite(STDERR, "Warning: Ignoring unknown option '{$arg}'\n");
}
}
// Determine Project Root (essential for finding composer.json and relative paths)
$root = getcwd(); // Start from current working directory
while (!is_file("$root/composer.json") && substr_count($root, DIRECTORY_SEPARATOR) > 1) {
$root = dirname($root);
}
if (!is_file("$root/composer.json")) {
$root = getcwd();
echo "Warning: Could not find composer.json, using current directory '{$root}' as project root.\n";
}
// Instantiate and Configure Checker
$checker = new Checker($vendorDir, $root, $dryRun, $preset);
echo 'Mode: ' . ($dryRun ? 'Check (dry-run)' : 'Fix') . "\n";
// Determine and set paths
$paths = $paths ?: array_filter(['src', 'tests'], 'is_dir') ?: ['.'];
$checker->setPaths($paths);
echo 'Paths: ' . implode(', ', $paths) . "\n";
if ($preset) {
echo "Preset: {$preset}\n";
}
// Signal Handling
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGINT, function () use ($checker) {
pcntl_signal(SIGINT, SIG_DFL);
throw new Exception;
});
} elseif (function_exists('sapi_windows_set_ctrl_handler')) {
sapi_windows_set_ctrl_handler(function () use ($checker) {
throw new Exception;
});
}
// Run
try {
$fixerOk = $checker->runFixer();
echo "\n\n";
$snifferOk = $checker->runSniffer();
} catch (Exception) {
echo "Terminated\n";
$checker->cleanup();
exit(1);
}
$checker->cleanup();
if ($fixerOk && $snifferOk) {
echo $dryRun ? "Code style checks passed.\n" : "Code style fixed successfully.\n";
exit(0);
} else {
echo $dryRun ? "Code style issues found.\n" : "Code style fixing failed or issues remain.\n";
exit(1);
}