Skip to content

Several types of dependency are not detected #307

@LuigiCardamone

Description

@LuigiCardamone

Feature Request

Detect all types of dependency and add an option to detect all dependencies used on custom annotations.

Q A
New Feature yes
RFC no
BC Break yes

Summary

Hi All.

I created an Application level DTO that should depend only from Application and Domain using the rule DependsOnlyOnTheseNamespaces but I notice that several types of dependency from the framework are not detected.
I attach some examples.

Example 1 with custom annotation --> Not detected

use Symfony\Component\Validator\Constraints as Assert;

class ApplicationLevelDto
{
    /**
     * @Assert\NotBlank
     */
    public string|null $foo;
}

Example 2 with @var --> Not detected

use Symfony\Component\Validator\Constraints\NotBlank;

class ApplicationLevelDto
{
    /**
     * @var NotBlank
     */
    public $foo;
}

Example 3 with typed property --> Not detected

use Symfony\Component\Validator\Constraints\NotBlank;

class ApplicationLevelDto
{
    public NotBlank $foo;
}

It works only when I write this:

use Symfony\Component\Validator\Constraints\NotBlank;

class ApplicationLevelDto
{
    public NotBlank $foo;

    public function __construct()
    {
        $this->foo = new NotBlank(); //Violation detected in this line
    }
}

Here my phparkitect.php config file:

return static function (Config $config): void {
    $classSet = ClassSet::fromDir(__DIR__ . '/src/MyProject/AppBundle/Application');

    $rule = Rule::allClasses()
        ->that(new ResideInOneOfTheseNamespaces('MyProject\AppBundle\Application'))
        ->should(new DependsOnlyOnTheseNamespaces(
            'MyProject\AppBundle\Application',
            'MyProject\AppBundle\Domain',
        ))
        ->because('Application should depends only on Application and Domain');

    $config
        ->add($classSet, $rule);
};

Example 1 may be ok since that annotation is just adding extra information and the class may work even without that dependency: maybe could it be an optional check?
Example 2 could be useful for legacy project.
Example 3 seems a bug: what you think?

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions