Skip to content

Commit 880d4f7

Browse files
committed
Extract common code to new trait
1 parent 6c03b60 commit 880d4f7

9 files changed

+67
-190
lines changed

Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php

+2-19
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
class ClassReferencesInConfigurationFilesSniff implements Sniff
1818
{
19+
use ParseXMLTrait;
20+
1921
private const ERROR_MESSAGE_CONFIG = 'Incorrect format of PHP class reference';
2022
private const ERROR_CODE_CONFIG = 'IncorrectClassReference';
2123
private const ERROR_MESSAGE_MODULE = 'Incorrect format of module reference';
@@ -104,25 +106,6 @@ private function assertNonFactoryNameModule(File $phpcsFile, array $classes)
104106
}
105107
}
106108

107-
/**
108-
* Format the incoming XML to avoid tags split into several lines.
109-
*
110-
* @param File $phpcsFile
111-
*
112-
* @return false|string
113-
*/
114-
private function getFormattedXML(File $phpcsFile)
115-
{
116-
try {
117-
$doc = new DomDocument('1.0');
118-
$doc->formatOutput = true;
119-
$doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens())));
120-
return $doc->saveXML();
121-
} catch (\Exception $e) {
122-
return false;
123-
}
124-
}
125-
126109
/**
127110
* Parse an XML for references to PHP class names in selected tags or attributes
128111
*

Magento2/Sniffs/Legacy/LayoutSniff.php

+2-19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
class LayoutSniff implements Sniff
2121
{
22+
use ParseXMLTrait;
23+
2224
private const ERROR_CODE_XML = 'WrongXML';
2325
private const ERROR_CODE_OBSOLETE_BLOCK = 'ObsoleteBlock';
2426
private const ERROR_CODE_OBSOLETE_CLASS = 'ObsoleteClass';
@@ -263,25 +265,6 @@ private function testObsoleteReferences(SimpleXMLElement $layout, File $phpcsFil
263265
}
264266
}
265267

266-
/**
267-
* Format the incoming XML to avoid tags split into several lines.
268-
*
269-
* @param File $phpcsFile
270-
*
271-
* @return false|string
272-
*/
273-
private function getFormattedXML(File $phpcsFile)
274-
{
275-
try {
276-
$doc = new DomDocument('1.0');
277-
$doc->formatOutput = true;
278-
$doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens())));
279-
return $doc->saveXML();
280-
} catch (\Exception $e) {
281-
return false;
282-
}
283-
}
284-
285268
/**
286269
* Check that the output attribute has the right value
287270
*

Magento2/Sniffs/Legacy/ModuleXMLSniff.php

+2-19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
class ModuleXMLSniff implements Sniff
1919
{
20+
use ParseXMLTrait;
21+
2022
private const WARNING_CODE = 'FoundObsoleteAttribute';
2123
private const ERROR_CODE = 'WrongXML';
2224

@@ -78,23 +80,4 @@ public function process(File $phpcsFile, $stackPtr)
7880
}
7981
}
8082
}
81-
82-
/**
83-
* Format the incoming XML to avoid tags split into several lines.
84-
*
85-
* @param File $phpcsFile
86-
*
87-
* @return false|string
88-
*/
89-
private function getFormattedXML(File $phpcsFile)
90-
{
91-
try {
92-
$doc = new DomDocument('1.0');
93-
$doc->formatOutput = true;
94-
$doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens())));
95-
return $doc->saveXML();
96-
} catch (\Exception $e) {
97-
return false;
98-
}
99-
}
10083
}

Magento2/Sniffs/Legacy/ObsoleteAclSniff.php

+2-19
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
class ObsoleteAclSniff implements Sniff
1818
{
19+
use ParseXMLTrait;
20+
1921
private const WARNING_OBSOLETE_ACL_STRUCTURE = 'ObsoleteAclStructure';
2022

2123
/**
@@ -47,23 +49,4 @@ public function process(File $phpcsFile, $stackPtr)
4749
);
4850
}
4951
}
50-
51-
/**
52-
* Format the incoming XML to avoid tags split into several lines.
53-
*
54-
* @param File $phpcsFile
55-
*
56-
* @return false|string
57-
*/
58-
private function getFormattedXML(File $phpcsFile)
59-
{
60-
try {
61-
$doc = new DomDocument('1.0');
62-
$doc->formatOutput = true;
63-
$doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens())));
64-
return $doc->saveXML();
65-
} catch (\Exception $e) {
66-
return false;
67-
}
68-
}
6952
}

Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php

+2-19
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
class ObsoleteConfigNodesSniff implements Sniff
1515
{
16+
use ParseXMLTrait;
17+
1618
private const ERROR_MESSAGE_CONFIG = "Nodes identified by XPath '%s' are obsolete. %s";
1719
private const ERROR_CODE_CONFIG = 'ObsoleteNodeInConfig';
1820

@@ -70,25 +72,6 @@ public function process(File $phpcsFile, $stackPtr)
7072
}
7173
}
7274

73-
/**
74-
* Format the incoming XML to avoid tags split into several lines.
75-
*
76-
* @param File $phpcsFile
77-
*
78-
* @return false|string
79-
*/
80-
private function getFormattedXML(File $phpcsFile)
81-
{
82-
try {
83-
$doc = new DomDocument('1.0');
84-
$doc->formatOutput = true;
85-
$doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens())));
86-
return $doc->saveXML();
87-
} catch (\Exception $e) {
88-
return false;
89-
}
90-
}
91-
9275
/**
9376
* Get a list of obsolete nodes in the format <class_name> => <replacement>
9477
*

Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php

+2-19
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
class ObsoleteMenuSniff implements Sniff
1818
{
19+
use ParseXMLTrait;
20+
1921
private const WARNING_OBSOLETE_MENU_STRUCTURE = 'ObsoleteMenuStructure';
2022

2123
/**
@@ -52,23 +54,4 @@ public function process(File $phpcsFile, $stackPtr)
5254
);
5355
}
5456
}
55-
56-
/**
57-
* Format the incoming XML to avoid tags split into several lines.
58-
*
59-
* @param File $phpcsFile
60-
*
61-
* @return false|string
62-
*/
63-
private function getFormattedXML(File $phpcsFile)
64-
{
65-
try {
66-
$doc = new DomDocument('1.0');
67-
$doc->formatOutput = true;
68-
$doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens())));
69-
return $doc->saveXML();
70-
} catch (\Exception $e) {
71-
return false;
72-
}
73-
}
7457
}

Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php

+2-38
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
class ObsoleteSystemConfigurationSniff implements Sniff
1515
{
16-
private const ERROR_CODE_XML = 'WrongXML';
16+
use ParseXMLTrait;
17+
1718
private const WARNING_CODE_OBSOLETE = 'FoundObsoleteSystemConfiguration';
1819

1920
/**
@@ -56,41 +57,4 @@ public function process(File $phpcsFile, $stackPtr)
5657
);
5758
}
5859
}
59-
60-
/**
61-
* Adds an invalid XML error
62-
*
63-
* @param File $phpcsFile
64-
* @param int $stackPtr
65-
*/
66-
private function invalidXML(File $phpcsFile, int $stackPtr): void
67-
{
68-
$phpcsFile->addError(
69-
"Couldn't parse contents of '%s', check that they are in valid XML format.",
70-
$stackPtr,
71-
self::ERROR_CODE_XML,
72-
[
73-
$phpcsFile->getFilename(),
74-
]
75-
);
76-
}
77-
78-
/**
79-
* Format the incoming XML to avoid tags split into several lines.
80-
*
81-
* @param File $phpcsFile
82-
*
83-
* @return false|string
84-
*/
85-
private function getFormattedXML(File $phpcsFile)
86-
{
87-
try {
88-
$doc = new DomDocument('1.0');
89-
$doc->formatOutput = true;
90-
$doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens())));
91-
return $doc->saveXML();
92-
} catch (\Exception $e) {
93-
return false;
94-
}
95-
}
9660
}
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
namespace Magento2\Sniffs\Legacy;
9+
10+
use DOMDocument;
11+
use PHP_CodeSniffer\Files\File;
12+
13+
trait ParseXMLTrait
14+
{
15+
/**
16+
* Adds an invalid XML error
17+
*
18+
* @param File $phpcsFile
19+
* @param int $stackPtr
20+
*/
21+
private function invalidXML(File $phpcsFile, int $stackPtr): void
22+
{
23+
$phpcsFile->addError(
24+
"Couldn't parse contents of '%s', check that they are in valid XML format.",
25+
$stackPtr,
26+
'WrongXML',
27+
[
28+
$phpcsFile->getFilename(),
29+
]
30+
);
31+
}
32+
33+
/**
34+
* Format the incoming XML to avoid tags split into several lines.
35+
*
36+
* @param File $phpcsFile
37+
*
38+
* @return false|string
39+
*/
40+
private function getFormattedXML(File $phpcsFile)
41+
{
42+
try {
43+
$doc = new DomDocument('1.0');
44+
$doc->formatOutput = true;
45+
$doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens())));
46+
return $doc->saveXML();
47+
} catch (\Exception $e) {
48+
return false;
49+
}
50+
}
51+
}

Magento2/Sniffs/Legacy/WidgetXMLSniff.php

+2-38
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
*/
1717
class WidgetXMLSniff implements Sniff
1818
{
19+
use ParseXMLTrait;
20+
1921
private const ERROR_CODE_OBSOLETE_SUPPORTED_BLOCKS = 'FoundObsoleteNodeSupportedBlocks';
2022
private const ERROR_CODE_OBSOLETE_BLOCK_NAME = 'FoundObsoleteNodeBlockName';
2123
private const ERROR_CODE_FACTORY = 'FoundFactory';
22-
private const ERROR_CODE_XML = 'WrongXML';
2324

2425
/**
2526
* @inheritdoc
@@ -82,41 +83,4 @@ public function process(File $phpcsFile, $stackPtr)
8283
);
8384
}
8485
}
85-
86-
/**
87-
* Adds an invalid XML error
88-
*
89-
* @param File $phpcsFile
90-
* @param int $stackPtr
91-
*/
92-
protected function invalidXML(File $phpcsFile, int $stackPtr): void
93-
{
94-
$phpcsFile->addError(
95-
"Couldn't parse contents of '%s', check that they are in valid XML format",
96-
$stackPtr,
97-
self::ERROR_CODE_XML,
98-
[
99-
$phpcsFile->getFilename(),
100-
]
101-
);
102-
}
103-
104-
/**
105-
* Format the incoming XML to avoid tags split into several lines.
106-
*
107-
* @param File $phpcsFile
108-
*
109-
* @return false|string
110-
*/
111-
private function getFormattedXML(File $phpcsFile)
112-
{
113-
try {
114-
$doc = new DomDocument('1.0');
115-
$doc->formatOutput = true;
116-
$doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens())));
117-
return $doc->saveXML();
118-
} catch (\Exception $e) {
119-
return false;
120-
}
121-
}
12286
}

0 commit comments

Comments
 (0)