NotContainDocBlockLike matches not related classes
Feature Request
| Q | A |
|---|---|
| New Feature | yes |
| RFC | no |
| BC Break | no |
Summary
We want to be sure, no one throws a general Exception in functions:
$rules[] = Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App'))
->should(new NotContainDocBlockLike('@throws Exception'))
->because('we don\'t want simple exceptions - use custom instead');
The rule also matches the case below, which is not useful:
/**
* @throws ExceptionInterface
*/
public function doStuff(): void
{
doSomething();
}
The violation:
Xxx has 1 violations
should not have a doc block that contains @throws Exception because we don't want simple exceptions - use custom instead
Is there any possibility to use a regex?
Hi @Maxell92,
thank you for reporting this 🙇
We could probably make NotContainDocBlockLike accepting wildcards as for the rules that match class name or namespaces. What do you think @fain182 @AlessandroMinoccheri?
Yes, it could be useful to accept wildcards!
@micheleorselli it makes sense...
So you would break backward compatiblity and transform NotContainDocBlockLike in an exact match when there are no wildcard?
Or we could add a new NotContainDocBlock and deprecate the usage of NotContainDocBlockLike
I prefer to add a new NotContainDocBlock rule.
Just brainstorming. Maybe it can be useful to add a specif rule for Exceptions. The rule ->should(new NotContainDocBlockLike('@throws Exception')) does not work well if the same Exception is used with a relative namespace (E.g. @throws Exception) or with the complete namespace (E.g. @throws \Exception)
I think I get what you mean... we should probably look into @throws tags during the analysis and treat them as dependencies
That said, a NotContainDocBlock could still be useful