Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ impl DiagnosticsConfig {
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
pub struct FormattingConfig {
/// Explicitly disable all formatting
pub enabled: Option<bool>,
/// Command (path or name) to run php-cs-fixer.
///
/// - `None` (default) — check `require-dev` in `composer.json`;
Expand Down Expand Up @@ -354,12 +356,9 @@ impl FormattingConfig {
self.timeout.unwrap_or(10_000)
}

/// Whether formatting is entirely disabled (all tools explicitly
/// set to empty strings).
/// Whether formatting is explicitly disabled
pub fn is_disabled(&self) -> bool {
self.php_cs_fixer.as_deref() == Some("")
&& self.phpcbf.as_deref() == Some("")
&& self.pint.as_deref() == Some("")
self.enabled == Some(false)
}
}

Expand Down Expand Up @@ -1495,7 +1494,7 @@ paths = ["database/schema", "extra/schema.sql"]
}

#[test]
fn formatting_empty_string_disables_tool() {
fn formatting_empty_string_enables_tool() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(CONFIG_FILE_NAME);
std::fs::write(
Expand All @@ -1507,6 +1506,22 @@ paths = ["database/schema", "extra/schema.sql"]
assert_eq!(config.formatting.php_cs_fixer.as_deref(), Some(""));
assert_eq!(config.formatting.phpcbf.as_deref(), Some(""));
assert_eq!(config.formatting.pint.as_deref(), Some(""));
assert!(!config.formatting.is_disabled());
}

#[test]
fn formatting_disabled_disables_tool() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(CONFIG_FILE_NAME);
std::fs::write(
&path,
"[formatting]\nenabled = false\nphp-cs-fixer = \"\"\nphpcbf = \"\"\npint = \"\"\n",
)
.unwrap();
let config = load_config(dir.path()).unwrap();
assert_eq!(config.formatting.php_cs_fixer.as_deref(), Some(""));
assert_eq!(config.formatting.phpcbf.as_deref(), Some(""));
assert_eq!(config.formatting.pint.as_deref(), Some(""));
assert!(config.formatting.is_disabled());
}

Expand Down
5 changes: 5 additions & 0 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ mod tests {
#[test]
fn strategy_both_disabled() {
let config = FormattingConfig {
enabled: Some(true),
pint: Some(String::new()),
php_cs_fixer: Some(String::new()),
phpcbf: Some(String::new()),
Expand All @@ -804,6 +805,7 @@ mod tests {
#[test]
fn strategy_explicit_commands() {
let config = FormattingConfig {
enabled: Some(true),
pint: None,
php_cs_fixer: Some("/usr/bin/php-cs-fixer".to_string()),
phpcbf: Some("/usr/bin/phpcbf".to_string()),
Expand All @@ -825,6 +827,7 @@ mod tests {
#[test]
fn strategy_one_explicit_one_disabled() {
let config = FormattingConfig {
enabled: Some(true),
pint: None,
php_cs_fixer: Some("/usr/bin/php-cs-fixer".to_string()),
phpcbf: Some(String::new()),
Expand Down Expand Up @@ -1131,6 +1134,7 @@ mod tests {

// User explicitly set a different path.
let config = FormattingConfig {
enabled: Some(true),
pint: None,
php_cs_fixer: Some("/opt/php-cs-fixer".to_string()),
phpcbf: Some(String::new()),
Expand Down Expand Up @@ -1385,6 +1389,7 @@ mod tests {
fn execute_disabled_returns_none() {
let content = "<?php\necho 'hello';\n";
let config = FormattingConfig {
enabled: Some(true),
pint: None,
php_cs_fixer: Some(String::new()),
phpcbf: Some(String::new()),
Expand Down
Loading