Skip to content

Commit b15a3ab

Browse files
committed
Add removeAllRules() method
1 parent 0352420 commit b15a3ab

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Please also have a look at our
1212

1313
- `RuleSet::removeMatchingRules()` method
1414
(for the implementing classes `AtRuleSet` and `DeclarationBlock`) (#1249)
15+
- `RuleSet::removeAllRules()` method
16+
(for the implementing classes `AtRuleSet` and `DeclarationBlock`) (#1249)
1517
- Add Interface `CSSElement` (#1231)
1618
- Methods `getLineNumber` and `getColumnNumber` which return a nullable `int`
1719
for the following classes:
@@ -50,7 +52,7 @@ Please also have a look at our
5052

5153
- Passing a `string` or `null` to `RuleSet::removeRule()` is deprecated
5254
(implementing classes are `AtRuleSet` and `DeclarationBlock`);
53-
use `removeMatchingRules()` instead (#1249)
55+
use `removeMatchingRules()` or `removeAllRules()` instead (#1249)
5456
- Passing a `Rule` to `RuleSet::getRules()` or `getRulesAssoc()` is deprecated,
5557
affecting the implementing classes `AtRuleSet` and `DeclarationBlock`
5658
(call e.g. `getRules($rule->getRule())` instead) (#1248)

src/RuleSet/RuleSet.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function getRulesAssoc($searchPattern = null): array
217217
* @param Rule|string|null $searchPattern
218218
* `Rule` to remove.
219219
* Passing a `string` or `null` is deprecated in version 8.9.0, and will no longer work from v9.0.
220-
* Use `removeMatchingRules()` instead.
220+
* Use `removeMatchingRules()` or `removeAllRules()` instead.
221221
*/
222222
public function removeRule($searchPattern): void
223223
{
@@ -231,27 +231,30 @@ public function removeRule($searchPattern): void
231231
unset($this->rules[$nameOfPropertyToRemove][$key]);
232232
}
233233
}
234-
} else {
234+
} elseif ($searchPattern !== null) {
235235
$this->removeMatchingRules($searchPattern);
236+
} else {
237+
$this->removeAllRules();
236238
}
237239
}
238240

239241
/**
240242
* Removes rules by property name or search pattern.
241243
*
242-
* @param string|null $searchPattern
243-
* pattern to remove. If null, all rules are removed. If the pattern ends in a dash,
244+
* @param string $searchPattern
245+
* pattern to remove.
246+
* If the pattern ends in a dash,
244247
* all rules starting with the pattern are removed as well as one matching the pattern with the dash
245248
* excluded.
246249
*/
247-
public function removeMatchingRules(?string $searchPattern): void
250+
public function removeMatchingRules(string $searchPattern): void
248251
{
249252
foreach ($this->rules as $propertyName => $rules) {
250-
// Either no search rule is given or the search rule matches the found rule exactly
253+
// Either the search rule matches the found rule exactly
251254
// or the search rule ends in “-” and the found rule starts with the search rule or equals it
252255
// (without the trailing dash).
253256
if (
254-
$searchPattern === null || $propertyName === $searchPattern
257+
$propertyName === $searchPattern
255258
|| (\strrpos($searchPattern, '-') === \strlen($searchPattern) - \strlen('-')
256259
&& (\strpos($propertyName, $searchPattern) === 0
257260
|| $propertyName === \substr($searchPattern, 0, -1)))
@@ -261,6 +264,11 @@ public function removeMatchingRules(?string $searchPattern): void
261264
}
262265
}
263266

267+
public function removeAllRules(): void
268+
{
269+
$this->rules = [];
270+
}
271+
264272
protected function renderRules(OutputFormat $outputFormat): string
265273
{
266274
$result = '';

0 commit comments

Comments
 (0)