Skip to content

NotContainDocBlockLike matches not related classes #363

Open

Description

@Maxell92

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?

Activity

  1. micheleorselli commented on Feb 26, 2023

    @micheleorselli
    Member

    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?

  2. AlessandroMinoccheri commented on Feb 26, 2023

    @AlessandroMinoccheri
    Member

    Yes, it could be useful to accept wildcards!

  3. fain182 commented on Feb 27, 2023

    @fain182
    Collaborator

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

  4. micheleorselli commented on Feb 27, 2023

    @micheleorselli
    Member

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

  5. AlessandroMinoccheri commented on Feb 28, 2023

    @AlessandroMinoccheri
    Member

    I prefer to add a new NotContainDocBlock rule.

  6. LuigiCardamone commented on Mar 2, 2023

    @LuigiCardamone
    Contributor

    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)

  7. micheleorselli commented on Mar 2, 2023

    @micheleorselli
    Member

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions