slevomat / coding-standard

Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs
MIT License
1.39k stars 171 forks source link

Feature request: Forbidden classes #1653

Closed michnovka closed 8 months ago

michnovka commented 8 months ago

We have a sniff for forbidden functions, but I would like to have a sniff for forbidden classes.

More specifically, my usecase is to disallow use of DateTime, and force DateTimeImmutable

Alternatively, we can also make a sniff just for this, given that there are talks about deprecating DateTime altogether in the future.

However the forbidden classes would give much more flexibility, as there may be other features one might want to disallow in a project.

I would argue against fixer sniff for DateTime -> DateTimeImmutable blind replacement, as this would just lead to errors due to the mutability behavior.


I am willing to work on this if there is consensus on the usefulness of such sniff, otherwise I will implement it only as a part of my own standard.

P.S. making the forbidden classes sniff would work nicely if one wants to use an alternative to PHP's native types, like Carbon, as by forbidding both DateTime, DateTimeImmutable and Carbon you would always be forced to use CarbonImmutable which would be the only one allowed.

kukulich commented 8 months ago

https://github.com/slevomat/coding-standard/blob/master/doc/php.md#slevomatcodingstandardphpforbiddenclasses-

github-actions[bot] commented 7 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.