Skip to content

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

Open
@TravisCarden

Description

@TravisCarden

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions