| 
 | 1 | +<?php  | 
 | 2 | + | 
 | 3 | +declare(strict_types=1);  | 
 | 4 | + | 
 | 5 | +// Autoloader  | 
 | 6 | +if (  | 
 | 7 | +	!(is_file($file = ($vendorDir = __DIR__ . '/vendor') . '/autoload.php') && include $file) &&  | 
 | 8 | +	!(is_file($file = ($vendorDir = __DIR__ . '/../..') . '/autoload.php') && include $file)  | 
 | 9 | +) {  | 
 | 10 | +	fwrite(STDERR, "Install packages using Composer.\n");  | 
 | 11 | +	exit(1);  | 
 | 12 | +}  | 
 | 13 | + | 
 | 14 | + | 
 | 15 | +// Argument Parsing  | 
 | 16 | +$paths = [];  | 
 | 17 | +$preset = null;  | 
 | 18 | +$dryRun = true;  | 
 | 19 | + | 
 | 20 | +for ($i = 1; $i < $argc; $i++) {  | 
 | 21 | +	$arg = $argv[$i];  | 
 | 22 | +	if ($arg === '--preset' && isset($argv[$i + 1])) {  | 
 | 23 | +		$preset = $argv[++$i];  | 
 | 24 | +	} elseif ($arg === '--fix' || $arg === 'fix') {  | 
 | 25 | +		$dryRun = false;  | 
 | 26 | +	} elseif ($arg === 'check') {  | 
 | 27 | +		$dryRun = true;  | 
 | 28 | +	} elseif ($arg === '--help' || $arg === '-h') {  | 
 | 29 | +		echo "Usage: php fix.php [check|fix] [--preset <name>] [path1 path2 ...]\n";  | 
 | 30 | +		echo " check (default): Run tools in dry-run mode.\n";  | 
 | 31 | +		echo " fix: Run tools and apply fixes.\n";  | 
 | 32 | +		echo " --preset <name>: Specify preset (e.g., php81). Autodetected if omitted.\n";  | 
 | 33 | +		echo " path1 path2 ...: Specific files or directories to process. Defaults to src/, tests/ or ./\n";  | 
 | 34 | +		exit(0);  | 
 | 35 | +	} elseif (!str_starts_with($arg, '-')) {  | 
 | 36 | +		$paths[] = $arg;  | 
 | 37 | +	} else {  | 
 | 38 | +		fwrite(STDERR, "Warning: Ignoring unknown option '{$arg}'\n");  | 
 | 39 | +	}  | 
 | 40 | +}  | 
 | 41 | + | 
 | 42 | + | 
 | 43 | +// Determine Project Root (essential for finding composer.json and relative paths)  | 
 | 44 | +$root = getcwd(); // Start from current working directory  | 
 | 45 | +while (!is_file("$root/composer.json") && substr_count($root, DIRECTORY_SEPARATOR) > 1) {  | 
 | 46 | +	$root = dirname($root);  | 
 | 47 | +}  | 
 | 48 | +if (!is_file("$root/composer.json")) {  | 
 | 49 | +	$root = getcwd();  | 
 | 50 | +	echo "Warning: Could not find composer.json, using current directory '{$root}' as project root.\n";  | 
 | 51 | +}  | 
 | 52 | + | 
 | 53 | + | 
 | 54 | + | 
 | 55 | +// Instantiate and Configure Checker  | 
 | 56 | +$checker = new Checker($vendorDir, $root, $dryRun, $preset);  | 
 | 57 | +echo 'Mode: ' . ($dryRun ? 'Check (dry-run)' : 'Fix') . "\n";  | 
 | 58 | + | 
 | 59 | +// Determine and set paths  | 
 | 60 | +$paths = $paths ?: array_filter(['src', 'tests'], 'is_dir') ?: ['.'];  | 
 | 61 | +$checker->setPaths($paths);  | 
 | 62 | +echo 'Paths: ' . implode(', ', $paths) . "\n";  | 
 | 63 | + | 
 | 64 | +// Determine and set preset  | 
 | 65 | +if ($preset) {  | 
 | 66 | +	echo "Preset: {$preset}\n";  | 
 | 67 | +} else {  | 
 | 68 | +	echo "Preset: {$checker->derivePresetFromVersion()} (detected from PHP version)\n";  | 
 | 69 | +}  | 
 | 70 | + | 
 | 71 | +// Signal Handling  | 
 | 72 | +if (function_exists('pcntl_signal')) {  | 
 | 73 | +	pcntl_signal(SIGINT, function () use ($checker) {  | 
 | 74 | +		pcntl_signal(SIGINT, SIG_DFL);  | 
 | 75 | +		throw new Exception;  | 
 | 76 | +	});  | 
 | 77 | +} elseif (function_exists('sapi_windows_set_ctrl_handler')) {  | 
 | 78 | +	sapi_windows_set_ctrl_handler(function () use ($checker) {  | 
 | 79 | +		throw new Exception;  | 
 | 80 | +	});  | 
 | 81 | +}  | 
 | 82 | + | 
 | 83 | +// Run  | 
 | 84 | +try {  | 
 | 85 | +	$fixerOk = $checker->runFixer();  | 
 | 86 | +	echo "\n\n";  | 
 | 87 | +	$snifferOk = $checker->runSniffer();  | 
 | 88 | +} catch (Exception) {  | 
 | 89 | +	echo "Terminated\n";  | 
 | 90 | +	$checker->cleanup();  | 
 | 91 | +	exit(1);  | 
 | 92 | +}  | 
 | 93 | + | 
 | 94 | +$checker->cleanup();  | 
 | 95 | + | 
 | 96 | +if ($fixerOk && $snifferOk) {  | 
 | 97 | +	echo $dryRun ? "Code style checks passed.\n" : "Code style fixed successfully.\n";  | 
 | 98 | +	exit(0);  | 
 | 99 | +} else {  | 
 | 100 | +	echo $dryRun ? "Code style issues found.\n" : "Code style fixing failed or issues remain.\n";  | 
 | 101 | +	exit(1);  | 
 | 102 | +}  | 
0 commit comments