Skip to content

Commit 171e5ee

Browse files
committed
[TASK] Add RuleSet::removeMatchingRules(), with deprecation
Passing `string` or `null` to `removeRule()` is deprecated. The new method handles that functionality. Relates to #1247.
1 parent 9dbc6a6 commit 171e5ee

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Please also have a look at our
1010

1111
### Added
1212

13+
- `RuleSet::removeMatchingRules()` method for implementing classes `AtRuleSet`
14+
and `DeclarationBlock` (#1249)
1315
- Add Interface `CSSElement` (#1231)
1416
- Methods `getLineNumber` and `getColumnNumber` which return a nullable `int`
1517
for the following classes:
@@ -46,6 +48,9 @@ Please also have a look at our
4648

4749
### Deprecated
4850

51+
- Passing a `string` or `null` to `RuleSet::removeRule()` is deprecated
52+
(implementing classes are `AtRuleSet` and `DeclarationBlock`);
53+
use `removeMatchingRules()` instead (#1249)
4954
- Passing a string as the first argument to `getAllValues()` is deprecated;
5055
the search pattern should now be passed as the second argument (#1241)
5156
- Passing a Boolean as the second argument to `getAllValues()` is deprecated;

src/RuleSet/RuleSet.php

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -209,18 +209,12 @@ public function getRulesAssoc($searchPattern = null): array
209209
}
210210

211211
/**
212-
* Removes a rule from this RuleSet. This accepts all the possible values that `getRules()` accepts.
213-
*
214-
* If given a Rule, it will only remove this particular rule (by identity).
215-
* If given a name, it will remove all rules by that name.
216-
*
217-
* Note: this is different from pre-v.2.0 behaviour of PHP-CSS-Parser, where passing a Rule instance would
218-
* remove all rules with the same name. To get the old behaviour, use `removeRule($rule->getRule())`.
212+
* Removes a `Rule` from this `RuleSet` by identity.
219213
*
220214
* @param Rule|string|null $searchPattern
221-
* pattern to remove. If null, all rules are removed. If the pattern ends in a dash,
222-
* all rules starting with the pattern are removed as well as one matching the pattern with the dash
223-
* excluded. Passing a Rule behaves matches by identity.
215+
* `Rule` to remove.
216+
* Passing a `string` or `null` is deprecated in version 8.9.0, and will no longer work from v9.0.
217+
* Use `removeMatchingRules()` instead.
224218
*/
225219
public function removeRule($searchPattern): void
226220
{
@@ -235,18 +229,31 @@ public function removeRule($searchPattern): void
235229
}
236230
}
237231
} else {
238-
foreach ($this->rules as $propertyName => $rules) {
239-
// Either no search rule is given or the search rule matches the found rule exactly
240-
// or the search rule ends in “-” and the found rule starts with the search rule or equals it
241-
// (without the trailing dash).
242-
if (
243-
$searchPattern === null || $propertyName === $searchPattern
244-
|| (\strrpos($searchPattern, '-') === \strlen($searchPattern) - \strlen('-')
245-
&& (\strpos($propertyName, $searchPattern) === 0
246-
|| $propertyName === \substr($searchPattern, 0, -1)))
247-
) {
248-
unset($this->rules[$propertyName]);
249-
}
232+
$this->removeMatchingRules($searchPattern);
233+
}
234+
}
235+
236+
/**
237+
* Removes rules by property name or search pattern.
238+
*
239+
* @param string|null $searchPattern
240+
* pattern to remove. If null, all rules are removed. If the pattern ends in a dash,
241+
* all rules starting with the pattern are removed as well as one matching the pattern with the dash
242+
* excluded.
243+
*/
244+
public function removeMatchingRules(?string $searchPattern): void
245+
{
246+
foreach ($this->rules as $propertyName => $rules) {
247+
// Either no search rule is given or the search rule matches the found rule exactly
248+
// or the search rule ends in “-” and the found rule starts with the search rule or equals it
249+
// (without the trailing dash).
250+
if (
251+
$searchPattern === null || $propertyName === $searchPattern
252+
|| (\strrpos($searchPattern, '-') === \strlen($searchPattern) - \strlen('-')
253+
&& (\strpos($propertyName, $searchPattern) === 0
254+
|| $propertyName === \substr($searchPattern, 0, -1)))
255+
) {
256+
unset($this->rules[$propertyName]);
250257
}
251258
}
252259
}

0 commit comments

Comments
 (0)