• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

keradus / PHP-CS-Fixer / 17279562118

27 Aug 2025 09:47PM UTC coverage: 94.693%. Remained the same
17279562118

push

github

keradus
CS

28316 of 29903 relevant lines covered (94.69%)

45.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.87
/src/RuleSet/RuleSet.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer\RuleSet;
16

17
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
18
use PhpCsFixer\Utils;
19

20
/**
21
 * Set of rules to be used by fixer.
22
 *
23
 * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
24
 *
25
 * @readonly
26
 *
27
 * @internal
28
 *
29
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
30
 */
31
final class RuleSet implements RuleSetInterface
32
{
33
    /**
34
     * Group of rules generated from input set.
35
     *
36
     * The key is name of rule, value is configuration array or true.
37
     * The key must not point to any set.
38
     *
39
     * @var array<string, array<string, mixed>|true>
40
     */
41
    private array $rules;
42

43
    public function __construct(array $set = [])
44
    {
45
        foreach ($set as $name => $value) {
62✔
46
            if ('' === $name) {
61✔
47
                throw new \InvalidArgumentException('Rule/set name must not be empty.');
1✔
48
            }
49

50
            if (\is_int($name)) {
60✔
51
                throw new \InvalidArgumentException(\sprintf('Missing value for "%s" rule/set.', $value));
1✔
52
            }
53

54
            if (!\is_bool($value) && !\is_array($value)) {
59✔
55
                $message = str_starts_with($name, '@') ? '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.';
1✔
56

57
                if (null === $value) {
1✔
58
                    $message .= ' To disable the '.(str_starts_with($name, '@') ? 'set' : 'rule').', use "FALSE" instead of "NULL".';
1✔
59
                }
60

61
                throw new InvalidFixerConfigurationException($name, $message);
1✔
62
            }
63
        }
64

65
        $this->rules = $this->resolveSet($set);
59✔
66
    }
67

68
    public function hasRule(string $rule): bool
69
    {
70
        return \array_key_exists($rule, $this->rules);
54✔
71
    }
72

73
    public function getRuleConfiguration(string $rule): ?array
74
    {
75
        if (!$this->hasRule($rule)) {
54✔
76
            throw new \InvalidArgumentException(\sprintf('Rule "%s" is not in the set.', $rule));
1✔
77
        }
78

79
        if (true === $this->rules[$rule]) {
53✔
80
            return null;
39✔
81
        }
82

83
        return $this->rules[$rule];
42✔
84
    }
85

86
    public function getRules(): array
87
    {
88
        return $this->rules;
56✔
89
    }
90

91
    /**
92
     * Resolve input set into group of rules.
93
     *
94
     * @param array<string, array<string, mixed>|bool> $rules
95
     *
96
     * @return array<string, array<string, mixed>|true>
97
     */
98
    private function resolveSet(array $rules): array
99
    {
100
        $resolvedRules = [];
59✔
101

102
        // expand sets
103
        foreach ($rules as $name => $value) {
59✔
104
            if (str_starts_with($name, '@')) {
58✔
105
                if (!\is_bool($value)) {
58✔
106
                    throw new \UnexpectedValueException(\sprintf('Nested rule set "%s" configuration must be a boolean.', $name));
1✔
107
                }
108

109
                $set = $this->resolveSubset($name, $value);
57✔
110
                $resolvedRules = array_merge($resolvedRules, $set);
56✔
111
            } else {
112
                $resolvedRules[$name] = $value;
3✔
113
            }
114
        }
115

116
        // filter out all resolvedRules that are off
117
        $resolvedRules = array_filter(
57✔
118
            $resolvedRules,
57✔
119
            static fn ($value): bool => false !== $value
57✔
120
        );
57✔
121

122
        return $resolvedRules;
57✔
123
    }
124

125
    /**
126
     * Resolve set rules as part of another set.
127
     *
128
     * If set value is false then disable all fixers in set,
129
     * if not then get value from set item.
130
     *
131
     * @return array<string, array<string, mixed>|bool>
132
     */
133
    private function resolveSubset(string $setName, bool $setValue): array
134
    {
135
        $ruleSet = RuleSets::getSetDefinition($setName);
57✔
136

137
        if ($ruleSet instanceof DeprecatedRuleSetDescriptionInterface) {
56✔
138
            $messageEnd = [] === $ruleSet->getSuccessorsNames()
2✔
139
                ? 'No replacement available'
×
140
                : \sprintf('Use %s instead', Utils::naturalLanguageJoin($ruleSet->getSuccessorsNames()));
2✔
141

142
            Utils::triggerDeprecation(new \RuntimeException("Rule set \"{$setName}\" is deprecated. {$messageEnd}."));
2✔
143
        }
144

145
        $rules = $ruleSet->getRules();
56✔
146

147
        foreach ($rules as $name => $value) {
56✔
148
            if (str_starts_with($name, '@')) {
56✔
149
                $set = $this->resolveSubset($name, $setValue);
49✔
150
                unset($rules[$name]);
49✔
151
                $rules = array_merge($rules, $set);
49✔
152
            } elseif (!$setValue) {
56✔
153
                $rules[$name] = false;
2✔
154
            } else {
155
                $rules[$name] = $value;
56✔
156
            }
157
        }
158

159
        return $rules;
56✔
160
    }
161
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc