Skip to content

Commit 87e9ba5

Browse files
committed
[TASK] Rename methods in RuleContainer
E.g. `addRule()` is changed to `addDeclaration()`, with `addRule()` deprecated and changed to a wrapper method to call on to `addDeclaration()`. `RuleContainer` itself should be renamed to `DeclarationContainer` as a separate change. The tests will also be updated as a separate change.
1 parent f6ddd48 commit 87e9ba5

6 files changed

Lines changed: 177 additions & 72 deletions

File tree

CHANGELOG.md

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

1919
### Changed
2020

21+
- Methods like `setRule()` in `RuleSet` and `DeclarationBlock` have been renamed
22+
to `setDeclaration()`, etc. (#1521)
2123
- `Rule\Rule` class is renamed to `Property\Declaration` (#1508, #1512, #1513)
2224
- `Rule::setRule()` and `getRule()` are replaced with `setPropertyName()` and
2325
`getPropertyName()` (#1506)
@@ -32,6 +34,8 @@ Please also have a look at our
3234

3335
### Deprecated
3436

37+
- Methods like `setRule()` in `RuleSet` and `DeclarationBlock` are deprecated;
38+
there are direct replacements such as `setDeclaration()` (#1521)
3539
- `Rule\Rule` class is deprecated; `Property\Declaration` is a direct
3640
replacement (#1508)
3741
- `Rule::setRule()` and `getRule()` are deprecated and replaced with

src/RuleSet/AtRuleSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function render(OutputFormat $outputFormat): string
6161
$arguments = ' ' . $arguments;
6262
}
6363
$result .= "@{$this->type}$arguments{$formatter->spaceBeforeOpeningBrace()}{";
64-
$result .= $this->renderRules($outputFormat);
64+
$result .= $this->renderDeclarations($outputFormat);
6565
$result .= '}';
6666
return $result;
6767
}

src/RuleSet/DeclarationBlock.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
class DeclarationBlock implements CSSElement, CSSListItem, Positionable, RuleContainer
3636
{
3737
use CommentContainer;
38+
use LegacyRuleContainerMethods;
3839
use Position;
3940

4041
/**
@@ -180,65 +181,65 @@ public function getRuleSet(): RuleSet
180181
}
181182

182183
/**
183-
* @see RuleSet::addRule()
184+
* @see RuleSet::addDeclaration()
184185
*/
185-
public function addRule(Declaration $declarationToAdd, ?Declaration $sibling = null): void
186+
public function addDeclaration(Declaration $declarationToAdd, ?Declaration $sibling = null): void
186187
{
187-
$this->ruleSet->addRule($declarationToAdd, $sibling);
188+
$this->ruleSet->addDeclaration($declarationToAdd, $sibling);
188189
}
189190

190191
/**
191192
* @return array<int<0, max>, Declaration>
192193
*
193-
* @see RuleSet::getRules()
194+
* @see RuleSet::getDeclarations()
194195
*/
195-
public function getRules(?string $searchPattern = null): array
196+
public function getDeclarations(?string $searchPattern = null): array
196197
{
197-
return $this->ruleSet->getRules($searchPattern);
198+
return $this->ruleSet->getDeclarations($searchPattern);
198199
}
199200

200201
/**
201202
* @param array<Declaration> $declarations
202203
*
203-
* @see RuleSet::setRules()
204+
* @see RuleSet::setDeclarations()
204205
*/
205-
public function setRules(array $declarations): void
206+
public function setDeclarations(array $declarations): void
206207
{
207-
$this->ruleSet->setRules($declarations);
208+
$this->ruleSet->setDeclarations($declarations);
208209
}
209210

210211
/**
211212
* @return array<string, Declaration>
212213
*
213-
* @see RuleSet::getRulesAssoc()
214+
* @see RuleSet::getDeclarationsAssociative()
214215
*/
215-
public function getRulesAssoc(?string $searchPattern = null): array
216+
public function getDeclarationsAssociative(?string $searchPattern = null): array
216217
{
217-
return $this->ruleSet->getRulesAssoc($searchPattern);
218+
return $this->ruleSet->getDeclarationsAssociative($searchPattern);
218219
}
219220

220221
/**
221-
* @see RuleSet::removeRule()
222+
* @see RuleSet::removeDeclaration()
222223
*/
223-
public function removeRule(Declaration $declarationToRemove): void
224+
public function removeDeclaration(Declaration $declarationToRemove): void
224225
{
225-
$this->ruleSet->removeRule($declarationToRemove);
226+
$this->ruleSet->removeDeclaration($declarationToRemove);
226227
}
227228

228229
/**
229-
* @see RuleSet::removeMatchingRules()
230+
* @see RuleSet::removeMatchingDeclarations()
230231
*/
231-
public function removeMatchingRules(string $searchPattern): void
232+
public function removeMatchingDeclarations(string $searchPattern): void
232233
{
233-
$this->ruleSet->removeMatchingRules($searchPattern);
234+
$this->ruleSet->removeMatchingDeclarations($searchPattern);
234235
}
235236

236237
/**
237-
* @see RuleSet::removeAllRules()
238+
* @see RuleSet::removeAllDeclarations()
238239
*/
239-
public function removeAllRules(): void
240+
public function removeAllDeclarations(): void
240241
{
241-
$this->ruleSet->removeAllRules();
242+
$this->ruleSet->removeAllDeclarations();
242243
}
243244

244245
/**
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\RuleSet;
6+
7+
use Sabberworm\CSS\Property\Declaration;
8+
9+
/**
10+
* @internal
11+
*/
12+
trait LegacyRuleContainerMethods
13+
{
14+
public function addRule(Declaration $declarationToAdd, ?Declaration $sibling = null): void
15+
{
16+
$this->addDeclaration($declarationToAdd, $sibling);
17+
}
18+
19+
public function removeRule(Declaration $declarationToRemove): void
20+
{
21+
$this->removeDeclaration($declarationToRemove);
22+
}
23+
24+
public function removeMatchingRules(string $searchPattern): void
25+
{
26+
$this->removeMatchingDeclarations($searchPattern);
27+
}
28+
29+
public function removeAllRules(): void
30+
{
31+
$this->removeAllDeclarations();
32+
}
33+
34+
/**
35+
* @param array<Declaration> $declarations
36+
*/
37+
public function setRules(array $declarations): void
38+
{
39+
$this->setDeclarations($declarations);
40+
}
41+
42+
/**
43+
* @return array<int<0, max>, Declaration>
44+
*/
45+
public function getRules(?string $searchPattern = null): array
46+
{
47+
return $this->getDeclarations($searchPattern);
48+
}
49+
50+
/**
51+
* @return array<string, Declaration>
52+
*/
53+
public function getRulesAssoc(?string $searchPattern = null): array
54+
{
55+
return $this->getDeclarationsAssociative($searchPattern);
56+
}
57+
}

src/RuleSet/RuleContainer.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,71 @@
77
use Sabberworm\CSS\Property\Declaration;
88

99
/**
10-
* Represents a CSS item that contains `Rules`, defining the methods to manipulate them.
10+
* Represents a CSS item that contains `Declarations`, defining the methods to manipulate them.
1111
*/
1212
interface RuleContainer
1313
{
14+
public function addDeclaration(Declaration $declarationToAdd, ?Declaration $sibling = null): void;
15+
16+
public function removeDeclaration(Declaration $declarationToRemove): void;
17+
18+
public function removeMatchingDeclarations(string $searchPattern): void;
19+
20+
public function removeAllDeclarations(): void;
21+
22+
/**
23+
* @param array<Declaration> $declarations
24+
*/
25+
public function setDeclarations(array $declarations): void;
26+
27+
/**
28+
* @return array<int<0, max>, Declaration>
29+
*/
30+
public function getDeclarations(?string $searchPattern = null): array;
31+
32+
/**
33+
* @return array<string, Declaration>
34+
*/
35+
public function getDeclarationsAssociative(?string $searchPattern = null): array;
36+
37+
/**
38+
* @deprecated in v9.2, will be removed in v10.0; use `addDeclaration()` instead.
39+
*/
1440
public function addRule(Declaration $declarationToAdd, ?Declaration $sibling = null): void;
1541

42+
/**
43+
* @deprecated in v9.2, will be removed in v10.0; use `removeDeclaration()` instead.
44+
*/
1645
public function removeRule(Declaration $declarationToRemove): void;
1746

47+
/**
48+
* @deprecated in v9.2, will be removed in v10.0; use `removeMatchingDeclarations()` instead.
49+
*/
1850
public function removeMatchingRules(string $searchPattern): void;
1951

52+
/**
53+
* @deprecated in v9.2, will be removed in v10.0; use `removeAllDeclarations()` instead.
54+
*/
2055
public function removeAllRules(): void;
2156

2257
/**
2358
* @param array<Declaration> $declarations
59+
*
60+
* @deprecated in v9.2, will be removed in v10.0; use `setDeclarations()` instead.
2461
*/
2562
public function setRules(array $declarations): void;
2663

2764
/**
2865
* @return array<int<0, max>, Declaration>
66+
*
67+
* @deprecated in v9.2, will be removed in v10.0; use `getDeclarations()` instead.
2968
*/
3069
public function getRules(?string $searchPattern = null): array;
3170

3271
/**
3372
* @return array<string, Declaration>
73+
*
74+
* @deprecated in v9.2, will be removed in v10.0; use `getDeclarationsAssociative()` instead.
3475
*/
3576
public function getRulesAssoc(?string $searchPattern = null): array;
3677
}

0 commit comments

Comments
 (0)