-
Notifications
You must be signed in to change notification settings - Fork 162
AC-659 Create phpcs static check for ModuleXMLTest #230
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 all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
cab2f92
AC-659: Create phpcs static check for ModuleXMLTest
svera e935df8
AC-659: Create phpcs static check for ModuleXMLTest
svera aa53065
AC-659: Create phpcs static check for ModuleXMLTest
svera 8d23aa4
AC-659: Create phpcs static check for ModuleXMLTest
svera 135630e
AC-659: Create phpcs static check for ModuleXMLTest
svera 85de47c
AC-659: Create phpcs static check for ModuleXMLTest
svera 5f4d45f
AC-659: Create phpcs static check for ModuleXMLTest
svera 7063659
AC-659: Create phpcs static check for ModuleXMLTest
svera c2786fe
AC-659: Create phpcs static check for ModuleXMLTest
svera aca6b41
AC-659: Create phpcs static check for ModuleXMLTest
svera 754e84c
AC-659: Create phpcs static check for ModuleXMLTest
svera 75f43af
AC-659: Create phpcs static check for ModuleXMLTest
svera d0fe150
AC-659: Create phpcs static check for ModuleXMLTest
svera c43f253
Merge branch 'develop' of github.com:magento/magento-coding-standard …
svera 53e9d75
AC-659: Create phpcs static check for ModuleXMLTest
svera 47a0536
AC-659: Create phpcs static check for ModuleXMLTest
svera 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,115 @@ | ||
<?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; | ||
use SimpleXMLElement; | ||
|
||
/** | ||
* Test for obsolete nodes/attributes in the module.xml | ||
*/ | ||
class ModuleXMLSniff implements Sniff | ||
{ | ||
private const WARNING_CODE = 'FoundObsoleteAttribute'; | ||
private const ERROR_CODE = 'WrongXML'; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function register(): array | ||
{ | ||
return [ | ||
T_INLINE_HTML | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$line = $phpcsFile->getTokens()[$stackPtr]['content']; | ||
if (strpos(trim($line), '<module') === false) { | ||
return; | ||
} | ||
|
||
// We need to format the incoming XML to avoid tags split into several lines. In that case, PHP's DOMElement | ||
// returns the position of the closing /> as the position of the tag, and we need the position of <module | ||
// instead, as it is the one we compare with $stackPtr later on. | ||
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); | ||
if ($xml === false) { | ||
$phpcsFile->addError( | ||
sprintf( | ||
"Couldn't parse contents of '%s', check that they are in valid XML format", | ||
$phpcsFile->getFilename(), | ||
), | ||
$stackPtr, | ||
self::ERROR_CODE | ||
); | ||
} | ||
|
||
$foundElements = $xml->xpath('/config/module'); | ||
if ($foundElements === false) { | ||
return; | ||
} | ||
|
||
foreach ($foundElements as $element) { | ||
if (!$this->elementIsCurrentlySniffedLine($element, $stackPtr)) { | ||
continue; | ||
} | ||
|
||
if (property_exists($element->attributes(), 'version')) { | ||
$phpcsFile->addWarning( | ||
'The "version" attribute is obsolete. Use "setup_version" instead.', | ||
$stackPtr, | ||
self::WARNING_CODE | ||
); | ||
} | ||
|
||
if (property_exists($element->attributes(), 'active')) { | ||
$phpcsFile->addWarning( | ||
'The "active" attribute is obsolete. The list of active modules '. | ||
'is defined in deployment configuration.', | ||
$stackPtr, | ||
self::WARNING_CODE | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 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); | ||
if ($node->getLineNo() === $stackPtr+1) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* 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,10 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="Magento_TestModule" active="true" version="1" /> | ||
</config> |
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,14 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module | ||
name="Magento_TestModule2" | ||
active="true" | ||
version="1" | ||
/> | ||
</config> |
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,10 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="Magento_TestModule2" /> | ||
</config> |
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,40 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento2\Tests\Legacy; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
class ModuleXMLUnitTest extends AbstractSniffUnitTest | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getErrorList() | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getWarningList($testFile = '') | ||
{ | ||
if ($testFile === 'ModuleXMLUnitTest.1.xml') { | ||
return [ | ||
9 => 2, | ||
]; | ||
} | ||
if ($testFile === 'ModuleXMLUnitTest.2.xml') { | ||
return [ | ||
9 => 2, | ||
]; | ||
} | ||
if ($testFile === 'ModuleXMLUnitTest.3.xml') { | ||
return []; | ||
} | ||
return []; | ||
} | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.