Skip to content

Commit 0119435

Browse files
authored
[TASK] Deconflate getAllValues() parameters (#1241)
The `$element` parameter was overloaded with a dual purpose. A second separate parameter has been added for rule filtering, which is not actually mutually exclusive with CSS subtree selection. Since `getAllValues()` is part of the public API, the method now supports being called with the old or new signatures, with the old signature being deprecated. Once the deprecation has been included in the 8.x release branch, the messiness of supporting the previous API can be removed. Part of #994. Also relates to #1230.
1 parent d9137fd commit 0119435

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed

CHANGELOG.md

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

2828
### Changed
2929

30+
- Parameters for `getAllValues()` are deconflated, so it now takes three (all
31+
optional), allowing `$element` and `$ruleSearchPattern` to be specified
32+
separately (#1241)
3033
- Implement `Positionable` in the following CSS item classes:
3134
`Comment`, `CSSList`, `SourceException`, `Charset`, `CSSNamespace`, `Import`,
3235
`Rule`, `DeclarationBlock`, `RuleSet`, `CSSFunction`, `Value` (#1225)
@@ -43,6 +46,11 @@ Please also have a look at our
4346

4447
### Deprecated
4548

49+
- Passing a string as the first argument to `getAllValues()` is deprecated;
50+
the search pattern should now be passed as the second argument (#1241)
51+
- Passing a Boolean as the second argument to `getAllValues()` is deprecated;
52+
the flag for searching in function arguments should now be passed as the third
53+
argument (#1241)
4654
- `getLineNo()` is deprecated in these classes (use `getLineNumber()` instead):
4755
`Comment`, `CSSList`, `SourceException`, `Charset`, `CSSNamespace`, `Import`,
4856
`Rule`, `DeclarationBlock`, `RuleSet`, `CSSFunction`, `Value` (#1225, #1233)

src/CSSList/CSSBlockList.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,44 @@ public function getAllRuleSets(): array
6464
/**
6565
* Returns all `Value` objects found recursively in `Rule`s in the tree.
6666
*
67-
* @param CSSElement|string $element
68-
* the `CSSList` or `RuleSet` to start the search from (defaults to the whole document).
69-
* If a string is given, it is used as rule name filter.
67+
* @param CSSElement|string|null $element
68+
* This is the `CSSList` or `RuleSet` to start the search from (defaults to the whole document).
69+
* If a string is given, it is used as a rule name filter.
70+
* Passing a string for this parameter is deprecated in version 8.9.0, and will not work from v9.0;
71+
* use the following parameter to pass a rule name filter instead.
72+
* @param string|bool|null $ruleSearchPatternOrSearchInFunctionArguments
73+
* This allows filtering rules by property name
74+
* (e.g. if "color" is passed, only `Value`s from `color` properties will be returned,
75+
* or if "font-" is provided, `Value`s from all font rules, like `font-size`, and including `font` itself,
76+
* will be returned).
77+
* If a Boolean is provided, it is treated as the `$searchInFunctionArguments` argument.
78+
* Passing a Boolean for this parameter is deprecated in version 8.9.0, and will not work from v9.0;
79+
* use the `$searchInFunctionArguments` parameter instead.
7080
* @param bool $searchInFunctionArguments whether to also return Value objects used as Function arguments.
7181
*
7282
* @return array<int, Value>
7383
*
7484
* @see RuleSet->getRules()
7585
*/
76-
public function getAllValues($element = null, bool $searchInFunctionArguments = false): array
77-
{
78-
$searchString = null;
86+
public function getAllValues(
87+
$element = null,
88+
$ruleSearchPatternOrSearchInFunctionArguments = null,
89+
bool $searchInFunctionArguments = false
90+
): array {
91+
if (\is_bool($ruleSearchPatternOrSearchInFunctionArguments)) {
92+
$searchInFunctionArguments = $ruleSearchPatternOrSearchInFunctionArguments;
93+
$searchString = null;
94+
} else {
95+
$searchString = $ruleSearchPatternOrSearchInFunctionArguments;
96+
}
97+
7998
if ($element === null) {
8099
$element = $this;
81100
} elseif (\is_string($element)) {
82101
$searchString = $element;
83102
$element = $this;
84103
}
104+
85105
$result = [];
86106
$this->allValues($element, $result, $searchString, $searchInFunctionArguments);
87107
return $result;

tests/Unit/CSSList/CSSBlockListTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,30 @@ public function getAllValuesWithSearchStringProvidedReturnsOnlyValuesFromMatchin
430430
self::assertSame([$value1], $result);
431431
}
432432

433+
/**
434+
* @test
435+
*/
436+
public function getAllValuesWithSearchStringProvidedInNewMethodSignatureReturnsOnlyValuesFromMatchingRules(): void
437+
{
438+
$subject = new ConcreteCSSBlockList();
439+
440+
$value1 = new CSSString('Superfont');
441+
$value2 = new CSSString('aquamarine');
442+
443+
$declarationBlock = new DeclarationBlock();
444+
$rule1 = new Rule('font-family');
445+
$rule1->setValue($value1);
446+
$declarationBlock->addRule($rule1);
447+
$rule2 = new Rule('color');
448+
$rule2->setValue($value2);
449+
$declarationBlock->addRule($rule2);
450+
$subject->setContents([$declarationBlock]);
451+
452+
$result = $subject->getAllValues(null, 'font-');
453+
454+
self::assertSame([$value1], $result);
455+
}
456+
433457
/**
434458
* @test
435459
*/
@@ -471,4 +495,25 @@ public function getAllValuesWithSearchInFunctionArgumentsReturnsValuesInFunction
471495

472496
self::assertSame([$value1, $value2], $result);
473497
}
498+
499+
/**
500+
* @test
501+
*/
502+
public function getAllValuesWithSearchInFunctionArgumentsInNewMethodSignatureReturnsValuesInFunctionArguments(): void
503+
{
504+
$subject = new ConcreteCSSBlockList();
505+
506+
$value1 = new Size(10, 'px');
507+
$value2 = new Size(2, '%');
508+
509+
$declarationBlock = new DeclarationBlock();
510+
$rule = new Rule('margin');
511+
$rule->setValue(new CSSFunction('max', [$value1, $value2]));
512+
$declarationBlock->addRule($rule);
513+
$subject->setContents([$declarationBlock]);
514+
515+
$result = $subject->getAllValues(null, null, true);
516+
517+
self::assertSame([$value1, $value2], $result);
518+
}
474519
}

0 commit comments

Comments
 (0)