-
Notifications
You must be signed in to change notification settings - Fork 160
AC-661: Create phpcs static check for XmlTest #266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
702cd60
AC-661: Create phpcs static check for XmlTest
jcuerdo 76359c2
AC-661: Create phpcs static check for XmlTest
jcuerdo a25e2ca
AC-661: Create phpcs static check for XmlTest
jcuerdo dc49aaa
AC-661: Create phpcs static check for XmlTest
jcuerdo 64512dd
AC-661: Create phpcs static check for XmlTest
jcuerdo df14044
AC-661: Create phpcs static check for XmlTest
jcuerdo 2f4c72e
AC-661: Create phpcs static check for XmlTest
jcuerdo 74c47b2
AC-661: Create phpcs static check for XmlTest
jcuerdo dae1ab3
AC-661: Create phpcs static check for XmlTest
jcuerdo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento2\Sniffs\Legacy; | ||
|
||
use DOMDocument; | ||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
/** | ||
* Test for obsolete nodes/attributes in the widget.xml | ||
*/ | ||
class WidgetXMLSniff implements Sniff | ||
{ | ||
private const ERROR_CODE_OBSOLETE = 'FoundObsoleteNode'; | ||
private const ERROR_CODE_FACTORY = 'FoundFactory'; | ||
private const ERROR_CODE_XML = 'WrongXML'; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function register(): array | ||
{ | ||
return [ | ||
T_INLINE_HTML | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
if ($stackPtr > 0) { | ||
return; | ||
} | ||
|
||
try{ | ||
jcuerdo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); | ||
jcuerdo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (\Exception $e) { | ||
$phpcsFile->addError( | ||
sprintf( | ||
"Couldn't parse contents of '%s', check that they are in valid XML format", | ||
$phpcsFile->getFilename(), | ||
), | ||
$stackPtr, | ||
self::ERROR_CODE_XML | ||
); | ||
return; | ||
} | ||
|
||
$foundElements = $xml->xpath('/widgets/*[@type]'); | ||
|
||
foreach ($foundElements as $element) { | ||
if (!property_exists($element->attributes(), 'type')) { | ||
continue; | ||
} | ||
$type = $element['type']; | ||
if (preg_match('/\//', $type)) { | ||
$phpcsFile->addError( | ||
"Factory name detected: {$type}.", | ||
dom_import_simplexml($element)->getLineNo() - 1, | ||
self::ERROR_CODE_FACTORY | ||
); | ||
} | ||
} | ||
|
||
$foundElements = $xml->xpath('/widgets/*/supported_blocks'); | ||
foreach ($foundElements as $element) { | ||
$phpcsFile->addError( | ||
"Obsolete node: <supported_blocks>. To be replaced with <supported_containers>", | ||
dom_import_simplexml($element)->getLineNo() - 1, | ||
self::ERROR_CODE_OBSOLETE | ||
); | ||
} | ||
|
||
$foundElements = $xml->xpath('/widgets/*/*/*/block_name'); | ||
foreach ($foundElements as $element) { | ||
$phpcsFile->addError( | ||
"Obsolete node: <block_name>. To be replaced with <container_name>", | ||
dom_import_simplexml($element)->getLineNo() - 1, | ||
self::ERROR_CODE_OBSOLETE | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Check if the element passed is in the currently sniffed line | ||
* | ||
* @param SimpleXMLElement $element | ||
* @param int $stackPtr | ||
* @return bool | ||
*/ | ||
private function elementIsCurrentlySniffedLine(SimpleXMLElement $element, int $stackPtr): bool | ||
{ | ||
$node = dom_import_simplexml($element); | ||
|
||
return $node->getLineNo() === $stackPtr + 1; | ||
} | ||
|
||
/** | ||
* Format the incoming XML to avoid tags split into several lines. | ||
* | ||
* @param File $phpcsFile | ||
* @return false|string | ||
*/ | ||
private function getFormattedXML(File $phpcsFile) | ||
{ | ||
$doc = new DomDocument('1.0'); | ||
$doc->formatOutput = true; | ||
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); | ||
return $doc->saveXML(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<widgets> | ||
<widget type="/Deprecated"> | ||
<label translate="true">Catalog New Products List</label> | ||
<description translate="true">List of Products that are set as New</description> | ||
jcuerdo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<supported_blocks>Deprecated | ||
<block> | ||
<block_name>Deprecated</block_name> | ||
</block> | ||
</supported_blocks> | ||
</widget> | ||
</widgets> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
jcuerdo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<widgets> | ||
<widget | ||
</widgets> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento2\Tests\Legacy; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
class WidgetXMLUnitTest extends AbstractSniffUnitTest | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getErrorList($testFile = '') | ||
{ | ||
if ($testFile === 'WidgetXMLUnitTest.1.xml') { | ||
return [ | ||
9 => 1, | ||
12 => 1, | ||
14 => 1, | ||
]; | ||
} | ||
if ($testFile === 'WidgetXMLUnitTest.2.xml') { | ||
return [ | ||
1 => 1 | ||
]; | ||
} | ||
return []; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getWarningList($testFile = '') | ||
{ | ||
return []; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.