Skip to content

[TASK] Allow only Rule to be passed to RuleSet::removeRule() #1255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Please also have a look at our

### Changed

- `RuleSet::removeRule()` now only allows `Rule` as the parameter
(implementing classes are `AtRuleSet` and `DeclarationBlock`);
use `removeMatchingRules()` or `removeAllRules()` for other functions (#1255)
- `RuleSet::getRules()` and `getRulesAssoc()` now only allow `string` or `null`
as the parameter (implementing classes are `AtRuleSet` and `DeclarationBlock`)
(#1253)
Expand Down
27 changes: 8 additions & 19 deletions src/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,28 +206,17 @@ public function getRulesAssoc(?string $searchPattern = null): array

/**
* Removes a `Rule` from this `RuleSet` by identity.
*
* @param Rule|string|null $searchPattern
* `Rule` to remove.
* Passing a `string` or `null` is deprecated in version 8.9.0, and will no longer work from v9.0.
* Use `removeMatchingRules()` or `removeAllRules()` instead.
*/
public function removeRule($searchPattern): void
public function removeRule(Rule $ruleToRemove): void
{
if ($searchPattern instanceof Rule) {
$nameOfPropertyToRemove = $searchPattern->getRule();
if (!isset($this->rules[$nameOfPropertyToRemove])) {
return;
}
foreach ($this->rules[$nameOfPropertyToRemove] as $key => $rule) {
if ($rule === $searchPattern) {
unset($this->rules[$nameOfPropertyToRemove][$key]);
}
$nameOfPropertyToRemove = $ruleToRemove->getRule();
if (!isset($this->rules[$nameOfPropertyToRemove])) {
return;
}
foreach ($this->rules[$nameOfPropertyToRemove] as $key => $rule) {
if ($rule === $ruleToRemove) {
unset($this->rules[$nameOfPropertyToRemove][$key]);
}
} elseif ($searchPattern !== null) {
$this->removeMatchingRules($searchPattern);
} else {
$this->removeAllRules();
}
}

Expand Down