|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package s9e\TextFormatter |
|
5
|
|
|
* @copyright Copyright (c) 2010-2023 The s9e authors |
|
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace s9e\TextFormatter\Configurator\TemplateNormalizations; |
|
9
|
|
|
|
|
10
|
|
|
class OptimizeChooseAttributes extends AbstractChooseOptimization |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* {@inheritdoc} |
|
14
|
|
|
*/ |
|
15
|
|
|
protected array $queries = ['//xsl:choose[xsl:otherwise/xsl:attribute]']; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get a list of all attribute elements common to all branches |
|
19
|
|
|
* |
|
20
|
|
|
* @return array<string, s9e\SweetDOM\Element> |
|
21
|
|
|
*/ |
|
22
|
|
|
protected function getCommonAttributes(): array |
|
23
|
|
|
{ |
|
24
|
|
|
$attributes = []; |
|
25
|
|
|
$branchesAttributes = []; |
|
26
|
|
|
foreach ($this->getBranches() as $branch) |
|
27
|
|
|
{ |
|
28
|
|
|
// Collect the XML for each xsl:attribute as well as a reference to the live node |
|
29
|
|
|
$branchAttributes = []; |
|
30
|
|
|
foreach ($branch->query('xsl:attribute') as $attribute) |
|
31
|
|
|
{ |
|
32
|
|
|
$attrName = $attribute->getAttribute('name'); |
|
33
|
|
|
$attributes[$attrName] = $attribute; |
|
34
|
|
|
$branchAttributes[$attrName] = $attribute->ownerDocument->saveXML($attribute); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$branchesAttributes[] = $branchAttributes; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// Keep only attributes with an alphanumeric, literal name (no dynamic attributes) |
|
41
|
|
|
$attributes = array_filter( |
|
42
|
|
|
$attributes, |
|
43
|
|
|
fn($attrName) => preg_match('(^[-\\w]+$)', $attrName), |
|
44
|
|
|
ARRAY_FILTER_USE_KEY |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
// Keep only the attributes whose XML is identical in all branches, and match their name |
|
48
|
|
|
// to return an actual xsl:attribute element |
|
49
|
|
|
return array_intersect_key($attributes, array_intersect_assoc(...$branchesAttributes)); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function optimizeChoose() |
|
56
|
|
|
{ |
|
57
|
|
|
foreach ($this->getCommonAttributes() as $attrName => $attribute) |
|
58
|
|
|
{ |
|
59
|
|
|
// Move the attribute before the xsl:choose element, then remove all remaining copies |
|
60
|
|
|
$this->choose->before($attribute); |
|
61
|
|
|
foreach ($this->choose->query('*/xsl:attribute[@name = "' . $attrName . '"]') as $xslAttribute) |
|
62
|
|
|
{ |
|
63
|
|
|
$xslAttribute->remove(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.