mamuz / PhpDependencyAnalysis

Static code analysis to find violations in a dependency graph
http://mamuz.github.io/PhpDependencyAnalysis/
MIT License
561 stars 45 forks source link

Access ReferenceValidator at runtime #22

Open FaKleiser opened 8 years ago

FaKleiser commented 8 years ago

Hey, thank you for the great project! I am trying to invoke PhpDA from within another console application and thus not directly using the CLI scripts the project provides. What I would like to do now is to define a more sophisticated ReferenceValidator that requires some configuration at runtime.

Currently, the ReferenceValidator does not support constructor-injection, as the validator is simply instantiated by the plugin loader (which is totally fine for the CLI usage). Also, it is not possible to somehow get the instantiated ReferenceValidator by the API and therefore it is also not possible to use setter-injection.

A simple way to enable either-or is to change the current config implementation as follows:

public function getReferenceValidator()
{
    if (!is_null($this->referenceValidator) && !is_string($this->referenceValidator) && !($this->referenceValidator instanceof ValidatorInterface)) {
        throw new \InvalidArgumentException('Config for referenceValidator must be an string');
    }
    return $this->referenceValidator;
}

The plugin loader would then need to check whether there is already an object coming from the config and then skip instantiation. I guess the proposed approach seems a bit like a workaround, but it should work at least :-).

What do you think? If the proposed approach is fine for you I'd be happy to help with the implementation!