Skip to content

Commit 5109c76

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 f6b5cd3 commit 5109c76

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 `Rule` to `RuleSet::getRules()` or `getRulesAssoc()` is deprecated,
5055
affecting the implementing classes `AtRuleSet` and `DeclarationBlock`
5156
(call e.g. `getRules($rule->getRule())` instead) (#1248)

src/RuleSet/RuleSet.php

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

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

0 commit comments

Comments
 (0)