Skip to content

Commit 6a3fec8

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 b1d0988 commit 6a3fec8

6 files changed

Lines changed: 144 additions & 40 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`
2224
(#1508, #1512, #1513, #1522)
2325
- `Rule::setRule()` and `getRule()` are replaced with `setPropertyName()` and
@@ -33,6 +35,8 @@ Please also have a look at our
3335

3436
### Deprecated
3537

38+
- Methods like `setRule()` in `RuleSet` and `DeclarationBlock` are deprecated;
39+
there are direct replacements such as `setDeclaration()` (#1521)
3640
- `Rule\Rule` class is deprecated; `Property\Declaration` is a direct
3741
replacement (#1508)
3842
- `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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,67 @@
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
}

src/RuleSet/RuleSet.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
* However, unknown `AtRule`s (like `@font-face`) are rule sets as well.
2323
*
2424
* If you want to manipulate a `RuleSet`,
25-
* use the methods `addRule()`, `getRules()`, `removeRule()`, `removeMatchingRules()`, etc.
25+
* use the methods `addDeclaration()`, `getDeclarations()`, `removeDeclaration()`, `removeMatchingDeclarations()`, etc.
2626
*
2727
* Note that `CSSListItem` extends both `Commentable` and `Renderable`, so those interfaces must also be implemented.
2828
*/
2929
class RuleSet implements CSSElement, CSSListItem, Positionable, RuleContainer
3030
{
3131
use CommentContainer;
32+
use LegacyRuleContainerMethods;
3233
use Position;
3334

3435
/**
@@ -88,7 +89,7 @@ public static function parseRuleSet(ParserState $parserState, RuleSet $ruleSet):
8889
$declaration = Declaration::parse($parserState, $commentsBeforeDeclaration);
8990
}
9091
if ($declaration instanceof Declaration) {
91-
$ruleSet->addRule($declaration);
92+
$ruleSet->addDeclaration($declaration);
9293
}
9394
}
9495
$parserState->consume('}');
@@ -99,7 +100,7 @@ public static function parseRuleSet(ParserState $parserState, RuleSet $ruleSet):
99100
* if the last `Declaration` is needed as a basis for setting position, but does not have a valid position,
100101
* which should never happen
101102
*/
102-
public function addRule(Declaration $declarationToAdd, ?Declaration $sibling = null): void
103+
public function addDeclaration(Declaration $declarationToAdd, ?Declaration $sibling = null): void
103104
{
104105
$propertyName = $declarationToAdd->getPropertyName();
105106
if (!isset($this->declarations[$propertyName])) {
@@ -148,14 +149,14 @@ public function addRule(Declaration $declarationToAdd, ?Declaration $sibling = n
148149
if ($declarationToAdd->getLineNumber() === null) {
149150
//this node is added manually, give it the next best line
150151
$columnNumber = $declarationToAdd->getColumnNumber() ?? 0;
151-
$declarations = $this->getRules();
152+
$declarations = $this->getDeclarations();
152153
$declarationsCount = \count($declarations);
153154
if ($declarationsCount > 0) {
154155
$last = $declarations[$declarationsCount - 1];
155156
$lastsLineNumber = $last->getLineNumber();
156157
if (!\is_int($lastsLineNumber)) {
157158
throw new \UnexpectedValueException(
158-
'A Declaration without a line number was found during addRule',
159+
'A Declaration without a line number was found during addDeclaration',
159160
1750718399
160161
);
161162
}
@@ -173,9 +174,9 @@ public function addRule(Declaration $declarationToAdd, ?Declaration $sibling = n
173174
/**
174175
* Returns all declarations matching the given property name
175176
*
176-
* @example $ruleSet->getRules('font') // returns array(0 => $declaration, …) or array().
177+
* @example $ruleSet->getDeclarations('font') // returns array(0 => $declaration, …) or array().
177178
*
178-
* @example $ruleSet->getRules('font-')
179+
* @example $ruleSet->getDeclarations('font-')
179180
* //returns an array of all declarations either beginning with font- or matching font.
180181
*
181182
* @param string|null $searchPattern
@@ -185,7 +186,7 @@ public function addRule(Declaration $declarationToAdd, ?Declaration $sibling = n
185186
*
186187
* @return array<int<0, max>, Declaration>
187188
*/
188-
public function getRules(?string $searchPattern = null): array
189+
public function getDeclarations(?string $searchPattern = null): array
189190
{
190191
$result = [];
191192
foreach ($this->declarations as $propertyName => $declarations) {
@@ -214,11 +215,11 @@ public function getRules(?string $searchPattern = null): array
214215
*
215216
* @param array<Declaration> $declarations
216217
*/
217-
public function setRules(array $declarations): void
218+
public function setDeclarations(array $declarations): void
218219
{
219220
$this->declarations = [];
220221
foreach ($declarations as $declaration) {
221-
$this->addRule($declaration);
222+
$this->addDeclaration($declaration);
222223
}
223224
}
224225

@@ -238,11 +239,11 @@ public function setRules(array $declarations): void
238239
*
239240
* @return array<string, Declaration>
240241
*/
241-
public function getRulesAssoc(?string $searchPattern = null): array
242+
public function getDeclarationsAssociative(?string $searchPattern = null): array
242243
{
243244
/** @var array<string, Declaration> $result */
244245
$result = [];
245-
foreach ($this->getRules($searchPattern) as $declaration) {
246+
foreach ($this->getDeclarations($searchPattern) as $declaration) {
246247
$result[$declaration->getPropertyName()] = $declaration;
247248
}
248249

@@ -252,7 +253,7 @@ public function getRulesAssoc(?string $searchPattern = null): array
252253
/**
253254
* Removes a `Declaration` from this `RuleSet` by identity.
254255
*/
255-
public function removeRule(Declaration $declarationToRemove): void
256+
public function removeDeclaration(Declaration $declarationToRemove): void
256257
{
257258
$nameOfPropertyToRemove = $declarationToRemove->getPropertyName();
258259
if (!isset($this->declarations[$nameOfPropertyToRemove])) {
@@ -274,7 +275,7 @@ public function removeRule(Declaration $declarationToRemove): void
274275
* all declarations starting with the pattern are removed as well as one matching the pattern with the dash
275276
* excluded.
276277
*/
277-
public function removeMatchingRules(string $searchPattern): void
278+
public function removeMatchingDeclarations(string $searchPattern): void
278279
{
279280
foreach ($this->declarations as $propertyName => $declarations) {
280281
// Either the search pattern matches the found declaration's property name exactly
@@ -291,7 +292,7 @@ public function removeMatchingRules(string $searchPattern): void
291292
}
292293
}
293294

294-
public function removeAllRules(): void
295+
public function removeAllDeclarations(): void
295296
{
296297
$this->declarations = [];
297298
}
@@ -301,15 +302,15 @@ public function removeAllRules(): void
301302
*/
302303
public function render(OutputFormat $outputFormat): string
303304
{
304-
return $this->renderRules($outputFormat);
305+
return $this->renderDeclarations($outputFormat);
305306
}
306307

307-
protected function renderRules(OutputFormat $outputFormat): string
308+
protected function renderDeclarations(OutputFormat $outputFormat): string
308309
{
309310
$result = '';
310311
$isFirst = true;
311312
$nextLevelFormat = $outputFormat->nextLevel();
312-
foreach ($this->getRules() as $declaration) {
313+
foreach ($this->getDeclarations() as $declaration) {
313314
$nextLevelFormatter = $nextLevelFormat->getFormatter();
314315
$renderedDeclaration = $nextLevelFormatter->safely(
315316
static function () use ($declaration, $nextLevelFormat): string {

0 commit comments

Comments
 (0)