Search code examples
coding-stylephpcs

How to exclude specific rule in a custom phpcs.xml ruleset?


I am looking for a way to automate coding standards and I've decided to use SlevomatCodingStandard.TypeHints.TypeHintDeclaration in our phpcs.xml file. Unfortunately we have faced with an issue with the error reporting:

38 | ERROR | [ ] @var annotation of property | | \VC4A\ConvertUploadsToS3::$missing_local_files | | does not specify type hint for its items. 45 | ERROR | [ ] @var annotation of property | | \VC4A\ConvertUploadsToS3::$missing_s3_files does | | not specify type hint for its items. 52 | ERROR | [ ] @var annotation of property | | \VC4A\ConvertUploadsToS3::$updated_s3_links does | | not specify type hint for its items. 66 | ERROR | [ ] @var annotation of property | | \VC4A\ConvertUploadsToS3::$s3_settings does not | | specify type hint for its items. 112 | ERROR | [ ] Method \VC4A\ConvertUploadsToS3::setup_crons() | | does not have return type hint nor @return | | annotation for its return value. 149 | ERROR | [ ] @return annotation of method | | \VC4A\ConvertUploadsToS3::get_cron_actions() does | | not specify type hint for items of its traversable | | return value. 202 | ERROR | [ ] @return annotation of method | | \VC4A\ConvertUploadsToS3::get_cron_intervals() | | does not specify type hint for items of its | | traversable return value. 266 | ERROR | [ ] @param annotation of method | | \VC4A\ConvertUploadsToS3::error_reporting() does | | not specify type hint for items of its traversable | | parameter $uploads. 334 | ERROR | [ ] @param annotation of method | | \VC4A\ConvertUploadsToS3::update_s3_link() does | | not specify type hint for items of its traversable | | parameter $upload. 336 | ERROR | [x] Method \VC4A\ConvertUploadsToS3::update_s3_link() | | does not have void return type hint.

I want to add strict return types for methods only but for the DocBlocks we are using WordPress-Docs standards.

Is there a way to disable the rest of the errors except the last one ?

What I've tried was this:

<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration"> <exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint"/> <exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.enableEachParameterAndReturnInspection"/> <exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.normalizedTraversableTypeHints"/> <exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.normalizedUsefulAnnotations"/> </rule>

Apparently this does not have any effect. Any suggestions ? Or what could be the proper way to add that specific rule only ?

Or is it possible to do it other way around ? Just add a specific rule in a ruleset without modifying the codebase ?


Solution

  • Apparently, I should have used the constant values instead of class data member names:

    <!-- Strict type return for methods --> <rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration"> <exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification"/> <exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversablePropertyTypeHintSpecification"/> <exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification"/> </rule>

    Source: https://github.com/slevomat/coding-standard/issues/570#issuecomment-448647567