-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.php
More file actions
147 lines (120 loc) · 3.5 KB
/
run.php
File metadata and controls
147 lines (120 loc) · 3.5 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?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 the 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();
fixSpaces($argv);
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);
}
// Space fixer (Latte, Twig, Neon)
function fixSpaces(array $arguments)
{
$files = '';
$count = 0;
$finder = new \Symfony\Component\Finder\Finder;
$finder->files()->name(['*.latte', '*.twig', '*.neon'])->in($arguments[2]);
foreach($finder as $file)
{
$path = $file->getRealPath();
$content = file_get_contents($path);
if(strpos($content, ' '))
{
if(in_array('--fix', $arguments, true))
{
$content = str_replace(' ', "\t", $content);
file_put_contents($path, $content);
}
$files .= $path . PHP_EOL;
$count++;
}
}
if($count != 0)
{
print PHP_EOL;
if(in_array('--fix', $arguments, true))
{
print "Files converted to tabs:" . PHP_EOL;
}
else
{
print "Files can be converted to tabs:" . PHP_EOL;
}
print $files . PHP_EOL;
}
}