-
Notifications
You must be signed in to change notification settings - Fork 160
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
Conversation
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$line = $phpcsFile->getTokens()[$stackPtr]['content']; | ||
if (strpos(trim($line), '<module ') === false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to validate the file path as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's already done in the ruleset
return; | ||
} | ||
|
||
$xml = simplexml_load_string($phpcsFile->getTokensAsString(0, 999999)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that possible to load all token until the token content ending with />
?
Also, it would be good to handle possible exceptions for XML operations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getTokenAsString
returns the whole file, as there are no PHP tags inside.
* | ||
* @var string | ||
*/ | ||
protected $warningCode = 'FoundObsoleteAttribute'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to use constant here
/** | ||
* Error violation code. | ||
*/ | ||
const WARNING_CODE = 'FoundObsoleteAttribute'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const WARNING_CODE = 'FoundObsoleteAttribute'; | |
private const WARNING_CODE = 'FoundObsoleteAttribute'; |
I think it might be good to take a look at the ways to verify this case using XSD schema, not necessarily with phpcs |
We have discussed the possible approaches and decided thatt loading XML and verifying specific incorrect nodes/attributes in it is fine. Full XSD validation is still present in dev mode in Magento. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@svera can you please take a look at the review comment and update the branch from develop. Thank you!
Magento2/ruleset.xml
Outdated
@@ -223,6 +223,12 @@ | |||
<severity>8</severity> | |||
<type>warning</type> | |||
</rule> | |||
<rule ref="Magento2.Legacy.ModuleXML"> | |||
<include-pattern>*\/module.xml$</include-pattern> | |||
<include-pattern>*\/ModuleXMLUnitTest.xml$</include-pattern> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@svera please remove the test file form the patterns
@ihor-sviziev is it possible to have 2 test files? I don't think we need to follow the exact structure of the module.xml to test a specific case. |
@sivaschenko sure, PS: TBH i would write one more static check against using multiple sections inside the single module.xml file, but that's another story ;) |
@ihor-sviziev could you please clarify this?
|
@svera, the regular module.xml file contains the only single section. Having tests with more than 1 section in the file isn't correct as it might cause some issues. https://github.com/magento/magento-coding-standard/tree/develop/Magento2/Tests/Classes |
👍 Got it, you're talking about the |
yes, for some reason, github removed it from my comment :) |
@ihor-sviziev done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look good now. Could you also add the case, when the module.xml file is valid and we don't have the warning?
@ihor-sviziev done too |
@magento import pr to magento-commerce/magento-coding-standard |
@svera the pull request successfully imported. |
This sniffer look for occurrences of obsolete attributes
active
andversion
inmodule.xml
, and returns a warning if it find any of those.