arkitect icon indicating copy to clipboard operation
arkitect copied to clipboard

NotContainDocBlockLike matches not related classes

Open Maxell92 opened this issue 3 years ago • 7 comments

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?

Maxell92 avatar Feb 24 '23 11:02 Maxell92

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?

micheleorselli avatar Feb 26 '23 10:02 micheleorselli

Yes, it could be useful to accept wildcards!

AlessandroMinoccheri avatar Feb 26 '23 19:02 AlessandroMinoccheri

@micheleorselli it makes sense... So you would break backward compatiblity and transform NotContainDocBlockLike in an exact match when there are no wildcard?

fain182 avatar Feb 27 '23 10:02 fain182

Or we could add a new NotContainDocBlock and deprecate the usage of NotContainDocBlockLike

micheleorselli avatar Feb 27 '23 12:02 micheleorselli

I prefer to add a new NotContainDocBlock rule.

AlessandroMinoccheri avatar Feb 28 '23 20:02 AlessandroMinoccheri

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)

LuigiCardamone avatar Mar 02 '23 08:03 LuigiCardamone

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

micheleorselli avatar Mar 02 '23 09:03 micheleorselli