Skip to content
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

Arbitrary error messages for SlevomatCodingStandard.PHP.ForbiddenClasses.forbiddenClasses #1618

Open
TravisCarden opened this issue Sep 7, 2023 · 0 comments

Comments

@TravisCarden
Copy link

I would like to use arbitrary error messages for SlevomatCodingStandard.PHP.ForbiddenClasses.forbiddenClasses, because I want to tell a developer what to do instead, but it's not necessarily a "fixable" alternative. For example, say I have a class \SomeClass. It has no alternative, it's just supposed to be instantiated via a factory, e.g., \SomeClassFactory::create(). If I try to provide that detail, it breaks the fixer, which creates invalid PHP.

No message

With no message, the error is confusing an un-actionable:

<element key="SomeClass" value="null"/>
# The problem:
new SomeClass('argument');
$ php vendor/bin/phpcs
------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------
 10 | ERROR | Usage of \SomeClass class is forbidden.
------------------------------------------------------

Arbitrary message

But a helpful, arbitrary message confuses the fixer, which assumes the value is a valid class:

<element key="SomeClass" value="I'm trying to be helpful by recommending SomeClassFactory::create()"/>
# The problem:
new SomeClass('argument');
$ php vendor/bin/phpcs

--------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------------------------------------------------
 10 | ERROR | [x] Usage of \SomeClass class is forbidden, use \I'm trying to be helpful by recommending SomeClassFactory::create() instead.
--------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------------------------------------------------------------

$ php vendor/bin/phpcbf
...
# "Fixed":
new I'm trying to be helpful by recommending SomeClassFactory::create()('argument');

Possible solution

If I could just disable the fixer for that one class element, that would solve the problem for me.

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

No branches or pull requests

1 participant