|
8 | 8 |
|
9 | 9 | use PHP_CodeSniffer\Files\File; |
10 | 10 | use PHP_CodeSniffer\Sniffs\Sniff; |
| 11 | +use SimpleXMLElement; |
11 | 12 |
|
12 | 13 | /** |
13 | 14 | * Test for obsolete nodes/attributes in the module.xml |
@@ -36,22 +37,52 @@ public function register(): array |
36 | 37 | */ |
37 | 38 | public function process(File $phpcsFile, $stackPtr) |
38 | 39 | { |
39 | | - $xml = simplexml_load_string($phpcsFile->getTokensAsString(0, 999999)); |
| 40 | + $line = $phpcsFile->getTokens()[$stackPtr]['content']; |
| 41 | + if (strpos(trim($line), '<module ') === false) { |
| 42 | + return; |
| 43 | + } |
40 | 44 |
|
41 | | - if ($xml->xpath('/config/module/@version') !== false) { |
42 | | - $phpcsFile->addWarning( |
43 | | - 'The "version" attribute is obsolete. Use "setup_version" instead.', |
44 | | - $stackPtr, |
45 | | - $this->warningCode |
46 | | - ); |
| 45 | + $xml = simplexml_load_string($phpcsFile->getTokensAsString(0, 999999)); |
| 46 | + |
| 47 | + $foundElements = $xml->xpath('/config/module'); |
| 48 | + if ($foundElements === false) { |
| 49 | + return; |
47 | 50 | } |
48 | 51 |
|
49 | | - if ($xml->xpath('/config/module/@active') !== false) { |
50 | | - $phpcsFile->addWarning( |
51 | | - 'The "active" attribute is obsolete. The list of active modules is defined in deployment configuration.', |
52 | | - $stackPtr, |
53 | | - $this->warningCode |
54 | | - ); |
| 52 | + foreach ($foundElements as $element) { |
| 53 | + if (!$this->elementIsCurrentlySniffedLine($element, $stackPtr)) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + if (property_exists($element->attributes(), 'version')) { |
| 58 | + $phpcsFile->addWarning( |
| 59 | + 'The "version" attribute is obsolete. Use "setup_version" instead.', |
| 60 | + $stackPtr, |
| 61 | + $this->warningCode |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + if (property_exists($element->attributes(), 'active')) { |
| 66 | + $phpcsFile->addWarning( |
| 67 | + 'The "active" attribute is obsolete. The list of active modules is defined in deployment configuration.', |
| 68 | + $stackPtr, |
| 69 | + $this->warningCode |
| 70 | + ); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param SimpleXMLElement $element |
| 77 | + * @param int $stackPtr |
| 78 | + * @return bool |
| 79 | + */ |
| 80 | + private function elementIsCurrentlySniffedLine(SimpleXMLElement $element, int $stackPtr): bool |
| 81 | + { |
| 82 | + $node = dom_import_simplexml($element); |
| 83 | + if ($node->getLineNo() === $stackPtr+1) { |
| 84 | + return true; |
55 | 85 | } |
| 86 | + return false; |
56 | 87 | } |
57 | 88 | } |
0 commit comments