Skip to content

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 16 commits into from
Sep 2, 2021

Conversation

svera
Copy link
Contributor

@svera svera commented Aug 24, 2021

This sniffer look for occurrences of obsolete attributes active and version in module.xml, and returns a warning if it find any of those.

@svera svera changed the title Ac 659 AC-659 Aug 24, 2021
public function process(File $phpcsFile, $stackPtr)
{
$line = $phpcsFile->getTokens()[$stackPtr]['content'];
if (strpos(trim($line), '<module ') === false) {
Copy link
Member

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

Copy link
Contributor Author

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));
Copy link
Member

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

Copy link
Contributor Author

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';
Copy link
Member

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

@svera svera changed the title AC-659 AC-659 Create phpcs static check for ModuleXMLTest Aug 24, 2021
/**
* Error violation code.
*/
const WARNING_CODE = 'FoundObsoleteAttribute';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const WARNING_CODE = 'FoundObsoleteAttribute';
private const WARNING_CODE = 'FoundObsoleteAttribute';

@sivaschenko
Copy link
Member

I think it might be good to take a look at the ways to verify this case using XSD schema, not necessarily with phpcs

@sivaschenko
Copy link
Member

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.

Copy link
Member

@sivaschenko sivaschenko left a 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!

@@ -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>
Copy link
Member

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

@svera svera marked this pull request as ready for review September 1, 2021 07:25
@sivaschenko
Copy link
Member

@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.

@ihor-sviziev
Copy link
Collaborator

ihor-sviziev commented Sep 1, 2021

@sivaschenko sure,
Here is an example:
https://github.com/magento/magento-coding-standard/blob/develop/Magento2/Tests/Classes/DiscouragedDependenciesUnitTest.php
https://github.com/magento/magento-coding-standard/tree/develop/Magento2/Tests/Classes

image

PS: TBH i would write one more static check against using multiple sections inside the single module.xml file, but that's another story ;)

@svera
Copy link
Contributor Author

svera commented Sep 2, 2021

@ihor-sviziev could you please clarify this?

@svera, it should have two on more module nodes. Please extract it to a separate xml file

@ihor-sviziev
Copy link
Collaborator

@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.
You can take this test as an example, it uses multiple files :
https://github.com/magento/magento-coding-standard/blob/develop/Magento2/Tests/Classes/DiscouragedDependenciesUnitTest.php

https://github.com/magento/magento-coding-standard/tree/develop/Magento2/Tests/Classes
image

@svera
Copy link
Contributor Author

svera commented Sep 2, 2021

@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.
You can take this test as an example, it uses multiple files :
https://github.com/magento/magento-coding-standard/blob/develop/Magento2/Tests/Classes/DiscouragedDependenciesUnitTest.php

https://github.com/magento/magento-coding-standard/tree/develop/Magento2/Tests/Classes
image

👍 Got it, you're talking about the <module> tag, am I wrong?

@ihor-sviziev
Copy link
Collaborator

yes, for some reason, github removed it from my comment :)

@svera
Copy link
Contributor Author

svera commented Sep 2, 2021

@ihor-sviziev done

Copy link
Collaborator

@ihor-sviziev ihor-sviziev left a 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?

@svera
Copy link
Contributor Author

svera commented Sep 2, 2021

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

@svera
Copy link
Contributor Author

svera commented Sep 2, 2021

@magento import pr to magento-commerce/magento-coding-standard

@magento-engcom-team
Copy link
Contributor

@svera the pull request successfully imported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants