From 2261d765bf8016223d0b7cb3f9f4bdd93f3ee4a1 Mon Sep 17 00:00:00 2001 From: Dariusz Ruminski Date: Fri, 12 Mar 2021 20:56:17 +0100 Subject: [PATCH] DX: RuleSet - convert null handling to soft-warning --- src/RuleSet/RuleSet.php | 24 +++++++++++++++++++++--- tests/FixerFactoryTest.php | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/RuleSet/RuleSet.php b/src/RuleSet/RuleSet.php index 22f50287a71..ea57d8512d0 100644 --- a/src/RuleSet/RuleSet.php +++ b/src/RuleSet/RuleSet.php @@ -39,13 +39,31 @@ class RuleSet implements RuleSetInterface public function __construct(array $set = []) { - foreach ($set as $key => $value) { - if (\is_int($key)) { + foreach ($set as $name => $value) { + if ('' === $name) { + throw new \InvalidArgumentException('Rule/set name must not be empty.'); + } + + if (\is_int($name)) { throw new \InvalidArgumentException(sprintf('Missing value for "%s" rule/set.', $value)); } if (true !== $value && false !== $value && !\is_array($value)) { - throw new InvalidFixerConfigurationException($key, 'Configuration must be an array and may not be empty.'); + // @TODO drop me on 3.0 + if (null === $value) { + $messageForNullIssue = 'To disable the rule, use "FALSE" instead of "NULL".'; + if (getenv('PHP_CS_FIXER_FUTURE_MODE')) { + throw new InvalidFixerConfigurationException($name, $messageForNullIssue); + } + + @trigger_error($messageForNullIssue, E_USER_DEPRECATED); + + continue; + } + + $message = '@' === $name[0] ? 'Set must be enabled (true) or disabled (false). Other values are not allowed.' : 'Rule must be enabled (true), disabled (false) or configured (non-empty, assoc array). Other values are not allowed.'; + + throw new InvalidFixerConfigurationException($name, $message); } } diff --git a/tests/FixerFactoryTest.php b/tests/FixerFactoryTest.php index abc0e8c8b66..f4a932fcb60 100644 --- a/tests/FixerFactoryTest.php +++ b/tests/FixerFactoryTest.php @@ -308,7 +308,7 @@ public function testConfigureFixerWithNonArray($value) \PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class ); $this->expectExceptionMessage( - '[foo] Configuration must be an array and may not be empty.' + '[foo] Rule must be enabled (true), disabled (false) or configured (non-empty, assoc array). Other values are not allowed.' ); $factory->useRuleSet(new RuleSet([