Skip to content

Commit

Permalink
Remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed Sep 26, 2023
1 parent 880d4f7 commit c688fd3
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,7 @@ public function process(File $phpcsFile, $stackPtr)
// instead, as it is the one we compare with $stackPtr later on.
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
if ($xml === false) {
$phpcsFile->addError(
"Couldn't parse contents of '%s', check that they are in valid XML format",
$stackPtr,
self::ERROR_CODE_CONFIG,
[
$phpcsFile->getFilename(),
]
);
return;
}

$classes = $this->collectClassesInConfig($xml);
Expand Down
8 changes: 0 additions & 8 deletions Magento2/Sniffs/Legacy/LayoutSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,6 @@ public function process(File $phpcsFile, $stackPtr)
$layout = simplexml_load_string($this->getFormattedXML($phpcsFile));

if ($layout === false) {
$phpcsFile->addError(
"Couldn't parse contents of '%s', check that they are in valid XML format",
$stackPtr,
self::ERROR_CODE_XML,
[
$phpcsFile->getFilename(),
]
);
return;
}

Expand Down
8 changes: 0 additions & 8 deletions Magento2/Sniffs/Legacy/ModuleXMLSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ public function process(File $phpcsFile, $stackPtr)
// instead, as it is the one we compare with $stackPtr later on.
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
if ($xml === false) {
$phpcsFile->addError(
"Couldn't parse contents of '%s', check that they are in valid XML format",
$stackPtr,
self::ERROR_CODE,
[
$phpcsFile->getFilename(),
]
);
return;
}

Expand Down
9 changes: 1 addition & 8 deletions Magento2/Sniffs/Legacy/ObsoleteConfigNodesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,7 @@ public function process(File $phpcsFile, $stackPtr)
// instead, as it is the one we compare with $stackPtr later on.
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
if ($xml === false) {
$phpcsFile->addError(
"Couldn't parse contents of '%s', check that they are in valid XML format",
$stackPtr,
self::ERROR_CODE_CONFIG,
[
$phpcsFile->getFilename(),
]
);
return;
}

foreach ($this->getObsoleteNodes() as $xpath => $suggestion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public function process(File $phpcsFile, $stackPtr)
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));

if ($xml === false) {
$this->invalidXML($phpcsFile, $stackPtr);
return;
}

Expand Down
26 changes: 8 additions & 18 deletions Magento2/Sniffs/Legacy/ParseXMLTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,6 @@

trait ParseXMLTrait
{
/**
* Adds an invalid XML error
*
* @param File $phpcsFile
* @param int $stackPtr
*/
private function invalidXML(File $phpcsFile, int $stackPtr): void
{
$phpcsFile->addError(
"Couldn't parse contents of '%s', check that they are in valid XML format.",
$stackPtr,
'WrongXML',
[
$phpcsFile->getFilename(),
]
);
}

/**
* Format the incoming XML to avoid tags split into several lines.
*
Expand All @@ -45,6 +27,14 @@ private function getFormattedXML(File $phpcsFile)
$doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens())));
return $doc->saveXML();
} catch (\Exception $e) {
$phpcsFile->addError(
"Couldn't parse contents of '%s', check that they are in valid XML format.",
0,
'WrongXML',
[
$phpcsFile->getFilename(),
]
);
return false;
}
}
Expand Down
1 change: 0 additions & 1 deletion Magento2/Sniffs/Legacy/WidgetXMLSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function process(File $phpcsFile, $stackPtr)
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));

if ($xml === false) {
$this->invalidXML($phpcsFile, $stackPtr);
return;
}

Expand Down

0 comments on commit c688fd3

Please sign in to comment.