Format PHP source code using the built-in mago-formatter. Returns the formatted source string, or an error if parsing fails. Uses PER-CS 2.0 style defaults.
(
content: &str,
php_version: mago_php_version::PHPVersion,
)
| 209 | /// Returns the formatted source string, or an error if parsing fails. |
| 210 | /// Uses PER-CS 2.0 style defaults. |
| 211 | fn format_with_mago( |
| 212 | content: &str, |
| 213 | php_version: mago_php_version::PHPVersion, |
| 214 | ) -> Result<String, String> { |
| 215 | let arena = bumpalo::Bump::new(); |
| 216 | let settings = mago_formatter::settings::FormatSettings::default(); |
| 217 | let formatter = mago_formatter::Formatter::new(&arena, php_version, settings); |
| 218 | |
| 219 | let formatted = formatter |
| 220 | .format_code( |
| 221 | Cow::Borrowed("phpantom-fmt"), |
| 222 | Cow::Owned(content.to_string()), |
| 223 | ) |
| 224 | .map_err(|e| format!("Built-in formatter failed to parse PHP: {}", e))?; |
| 225 | |
| 226 | Ok(formatted.to_string()) |
| 227 | } |
| 228 | |
| 229 | /// Convert a project [`PhpVersion`](crate::types::PhpVersion) into the |
| 230 | /// mago-formatter's [`PHPVersion`](mago_php_version::PHPVersion). |
no outgoing calls