File: ide-integration.md

package info (click to toggle)
php-di 7.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,932 kB
  • sloc: php: 10,572; makefile: 42; xml: 17; sh: 10; pascal: 5
file content (61 lines) | stat: -rw-r--r-- 2,467 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
---
layout: documentation
current_menu: ide-integration
---

# IDE integration

Ideally, application code [should not use a DI container directly](best-practices.md): dependency injection should be preferred. However there are situations where calling the container directly might happen:

- when writing root application classes (front controllers, …) or more generally frameworks
- when writing factories
- when maintaining or migrating legacy application
- when writing functional tests
- ...

In those situations, being able to benefit from full IDE features like autocompletion, refactoring support, etc. is very valuable.

## Inline PhpDoc

A basic approach to IDE support is using `@var` tags in docblocks:

```php
/** @var $repository UserRepository */
$repository = $this->container->get(UserRepository::class);

// the IDE can now autocomplete this statement
$repository->
```

This solution is simple and works great when your container is used rarely.

## PhpStorm integration

### Metadata file

[PhpStorm will load metadata from a `.phpstorm.meta.php` file](https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata) if it exists at the root of your project. Here is an example that adds support for PHP-DI as well as [any PSR-11 container](http://www.php-fig.org/psr/psr-11/):

```php
<?php
namespace PHPSTORM_META
{
    override(\Psr\Container\ContainerInterface::get(0), map([
        '' => '@',
    ]));
    override(\DI\Container::get(0), map([
        '' => '@',
    ]));
}
```

That configuration will make PhpStorm assume that anything returned by `->get('...')` is an instance of the first argument. For example `->get('DateTime')` (or `->get(DateTime::class)`) will be recognized to return a `DateTime` object.

This however will not work if your service name is not a class or interface, for example `->get('foo.bar')` will not be understood by PhpStorm.

**Note:** you may need to restart your IDE after adding this file to make sure PhpStorm takes it into account.

### PhpStorm plugin

If you don't fancy writing a `.phpstorm.meta.php` file in each of your projects, you can install the [PHP-DI plugin for PhpStorm](https://github.com/pulyaevskiy/phpstorm-phpdi) created by [Anatoly Pulyaevskiy](https://github.com/pulyaevskiy). The plugin can be installed in PhpStorm by searching for *PHP-DI* in the 3rd party plugin list.

This plugin offers the same benefits as `.phpstorm.meta.php`, and has the same limitations.