A
A
Alex_Gerasimov2020-05-17 21:04:07
PHP
Alex_Gerasimov, 2020-05-17 21:04:07

How to jump to a specific implementation when using the Dependency inversion principle?

The principle is to use high-level abstractions instead of concrete implementations.

Example:

interface ProductCreatorInterface
{
  public function create();
}

class ProductCreator implements ProductCreatorInterface
{
  public function create()
  {
    return new Product();
  }
}

class ProductController
{
  private $productCreator;

  public function __construct(ProductCreatorInterface $productCreator)
  {
    $this->productCreator = $productCreator;
  }

  public function createAction()
  {
    $this->productCreator->create();
  }
}


If in the controller on the line "$this->productCreator->create();" go through the IDE to the implementation of the create() method, then the transition will go to the ProductCreatorInterface interface, as it is specified in the type hint in the controller constructor. The same goes for pulling documentation - it will be pulled from the interface, and not from a specific implementation.

Well, in general, it turns out not very convenient - yes, I abstract from a specific implementation and can easily replace them if necessary, but if you need to go and see which specific implementation is used, you first have to go from the code to the interface, and then look for where this interface is used in this moment.

Has anyone had this issue and was it possible to solve it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Flying, 2020-05-17
@Alex_Gerasimov

Since the question is not about code execution, but about the IDE, the specific answer will greatly depend on which IDE is used and even on which framework is used.
In fact, the question boils down to the ability of the IDE (on its own or through plugins) to get information related to the runtime of the application. And this, in turn, depends on whether a particular application (or the framework on which it is built) is able to provide this information in a form that the IDE can understand. If yes, you will be happy to switch to actually used implementations, if not, you will switch to abstract interfaces and look for an implementation yourself. In general, the habit of prescribing types everywhere through annotations starts to help here (in your example, this is not), but, of course, this approach has its drawbacks. you need to make sure that the specified types match the real ones. Here, in turn, a few more good habits related to the process of writing code help, but this way you can get deep into offtopic...
As an example, I will give perhaps PHPStorm in conjunction with the Symfony Support plugin in the script for working with Symfony . Since in Symfony DI container is compiled and, in addition, it generates a machine-readable representation of the container - through the plugin, the IDE gets a lot of information about what is actually used there at runtime, and navigation through the code (as well as a host of other things) becomes strong easier.
But not all IDE + framework sets are so lucky, for example, support for an application on ZF2, which you also have to deal with, does not provide this, you have to look for everything by hand. PHPStorm, of course, helps a lot here, but of course it cannot be compared with the Symfony script.

D
dmitriy, 2020-05-17
@dmitriylanets

specify the specifics in phpdoc and you will be happy

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question