Skip to content

Commit

Permalink
minor #5540 DX: RuleSet - convert null handling to soft-warning (kera…
Browse files Browse the repository at this point in the history
…dus)

This PR was squashed before being merged into the 2.18 branch.

Discussion
----------

DX: RuleSet - convert null handling to soft-warning

closes #5537

Commits
-------

2261d76 DX: RuleSet - convert null handling to soft-warning
  • Loading branch information
keradus committed Mar 15, 2021
2 parents 88e1fd1 + 2261d76 commit c4b84e0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/FixerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit c4b84e0

Please sign in to comment.