Skip to content

Commit f60f089

Browse files
committed
Implement more suggestions from code review
1 parent 686e9db commit f60f089

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/RuleSet/RuleSet.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
abstract class RuleSet implements Renderable, Commentable
2626
{
2727
/**
28+
* the rules in this rule set, using the property name as the key
29+
*
2830
* @var array<string, Rule>
2931
*/
3032
private $rules = [];
@@ -153,14 +155,15 @@ public function getRules($searchPattern = null)
153155
}
154156
/** @var array<int, Rule> $result */
155157
$result = [];
156-
foreach ($this->rules as $ruleName => $rule) {
158+
foreach ($this->rules as $propertyName => $rule) {
157159
// Either no search rule is given or the search rule matches the found rule exactly
158160
// or the search rule ends in “-” and the found rule starts with the search rule.
159161
if (
160-
!$searchPattern || $ruleName === $searchPattern
162+
!$searchPattern || $propertyName === $searchPattern
161163
|| (
162164
\strrpos($searchPattern, '-') === \strlen($searchPattern) - \strlen('-')
163-
&& (\strpos($ruleName, $searchPattern) === 0 || $ruleName === \substr($searchPattern, 0, -1))
165+
&& (\strpos($propertyName, $searchPattern) === 0
166+
|| $propertyName === \substr($searchPattern, 0, -1))
164167
)
165168
) {
166169
$result = \array_merge($result, $rule);
@@ -223,21 +226,21 @@ public function getRulesAssoc($searchPattern = null)
223226
* Note: this is different from pre-v.2.0 behaviour of PHP-CSS-Parser, where passing a Rule instance would
224227
* remove all rules with the same name. To get the old behaviour, use `removeRule($rule->getRule())`.
225228
*
226-
* @param Rule|string|null $removalPattern
227-
* pattern to remove. If $mRule is null, all rules are removed. If the pattern ends in a dash,
229+
* @param Rule|string|null $searchPattern
230+
* pattern to remove. If this is `null`, all rules are removed. If the pattern ends in a dash,
228231
* all rules starting with the pattern are removed as well as one matching the pattern with the dash
229232
* excluded. Passing a Rule behaves matches by identity.
230233
*/
231-
public function removeRule($removalPattern): void
234+
public function removeRule($searchPattern): void
232235
{
233-
if ($removalPattern instanceof Rule) {
234-
$removalRule = $removalPattern->getRule();
235-
if (!isset($this->rules[$removalRule])) {
236+
if ($searchPattern instanceof Rule) {
237+
$propertyToRemove = $searchPattern->getRule();
238+
if (!isset($this->rules[$propertyToRemove])) {
236239
return;
237240
}
238-
foreach ($this->rules[$removalRule] as $key => $rule) {
239-
if ($rule === $removalPattern) {
240-
unset($this->rules[$removalRule][$key]);
241+
foreach ($this->rules[$propertyToRemove] as $key => $rule) {
242+
if ($rule === $searchPattern) {
243+
unset($this->rules[$propertyToRemove][$key]);
241244
}
242245
}
243246
} else {
@@ -246,9 +249,9 @@ public function removeRule($removalPattern): void
246249
// or the search rule ends in “-” and the found rule starts with the search rule or equals it
247250
// (without the trailing dash).
248251
if (
249-
!$removalPattern || $ruleName === $removalPattern
250-
|| (\strrpos($removalPattern, '-') === \strlen($removalPattern) - \strlen('-')
251-
&& (\strpos($ruleName, $removalPattern) === 0 || $ruleName === \substr($removalPattern, 0, -1)))
252+
!$searchPattern || $ruleName === $searchPattern
253+
|| (\strrpos($searchPattern, '-') === \strlen($searchPattern) - \strlen('-')
254+
&& (\strpos($ruleName, $searchPattern) === 0 || $ruleName === \substr($searchPattern, 0, -1)))
252255
) {
253256
unset($this->rules[$ruleName]);
254257
}

0 commit comments

Comments
 (0)