Run the fix command and return the process exit code. Returns `0` when fixes were applied (or nothing to fix), `1` on error, `2` when `--dry-run` found fixable issues.
(options: FixOptions)
| 197 | /// Returns `0` when fixes were applied (or nothing to fix), `1` on error, |
| 198 | /// `2` when `--dry-run` found fixable issues. |
| 199 | pub async fn run(options: FixOptions) -> i32 { |
| 200 | let root = &options.workspace_root; |
| 201 | |
| 202 | if !root.join("composer.json").is_file() { |
| 203 | eprintln!("Error: no composer.json found in {}", root.display()); |
| 204 | eprintln!("The fix command currently only supports single Composer projects."); |
| 205 | return 1; |
| 206 | } |
| 207 | |
| 208 | // ── Validate rules ────────────────────────────────────────────── |
| 209 | let rule_errors = validate_rules(&options.rules, options.with_phpstan); |
| 210 | if !rule_errors.is_empty() { |
| 211 | for err in &rule_errors { |
| 212 | eprintln!("Error: {err}"); |
| 213 | } |
| 214 | return 1; |
| 215 | } |
| 216 | |
| 217 | let native_rules = effective_native_rules(&options.rules); |
| 218 | if native_rules.is_empty() && !options.with_phpstan { |
| 219 | eprintln!("No applicable rules to run."); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | // ── 1. Load config ────────────────────────────────────────────── |
| 224 | let cfg = match config::load_config(root) { |
| 225 | Ok(c) => c, |
| 226 | Err(e) => { |
| 227 | eprintln!("Warning: failed to load .phpantom.toml: {e}"); |
| 228 | config::Config::default() |
| 229 | } |
| 230 | }; |
| 231 | |
| 232 | // ── 2. Index project ──────────────────────────────────────────── |
| 233 | let backend = Backend::new_headless(); |
| 234 | *backend.workspace_root().write() = Some(root.to_path_buf()); |
| 235 | *backend.config.lock() = cfg.clone(); |
| 236 | |
| 237 | let composer_package = composer::read_composer_package(root); |
| 238 | |
| 239 | let php_version = cfg |
| 240 | .php |
| 241 | .version |
| 242 | .as_deref() |
| 243 | .and_then(crate::types::PhpVersion::from_composer_constraint) |
| 244 | .unwrap_or_else(|| { |
| 245 | composer_package |
| 246 | .as_ref() |
| 247 | .and_then(composer::detect_php_version_from_package) |
| 248 | .unwrap_or_default() |
| 249 | }); |
| 250 | backend.set_php_version(php_version); |
| 251 | |
| 252 | backend |
| 253 | .init_single_project(root, php_version, composer_package, None) |
| 254 | .await; |
| 255 | |
| 256 | // ── 3. Discover files ─────────────────────────────────────────── |
no test coverage detected