5 * Generate PhpStorm metadata file.
9 * Implements of hook_drush_command().
11 function phpstorm_drush_command() {
14 $items['phpstorm-metadata'] = array(
15 'description' => 'Save the PhpStorm Metadata file to Drupal root.',
16 'core' => array('8+'),
17 'aliases' => array('phpm'),
18 'category' => 'devel',
25 * Implements hook_drush_help_alter().
27 function phpstorm_drush_help_alter(&$command) {
28 if ($command['command'] == 'cache-rebuild') {
29 $command['options']['storm'] = 'Write a new PHPstorm metadata file to Drupal root.';
34 * Implements drush_hook_post_COMMAND().
36 function drush_phpstorm_post_cache_rebuild() {
37 if (drush_get_option('storm')) {
38 drush_invoke_process('@self', 'phpstorm-metadata');
43 * Generate PhpStorm Metadata file.
45 * @see http://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata
47 function drush_phpstorm_metadata() {
48 $container = \Drupal::getContainer();
50 $reflectedClass = new ReflectionClass($container);
54 // Map for all services of the container.
55 // @see \Symfony\Component\DependencyInjection\Container::getServiceIds().
56 foreach ($reflectedClass->getMethods() as $method) {
57 if (preg_match('/^get(.+)Service$/', $method->name, $match)) {
58 $id = strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), strtr($match[1], '_', '.')));
59 $service = \Drupal::service($id);
60 if (is_object($service)) {
61 $map["\\Drupal::service('')"][$id] = '\\' . get_class($service);
66 // Entity Manager - getStorage
67 foreach (\Drupal::entityTypeManager()->getDefinitions() as $type => $definition) {
68 $class = Drupal::entityTypeManager()->getStorage($type);
69 $map["\\Drupal::entityManager()->getStorage('')"][$type] = '\\' . get_class($class);
70 $map["\\Drupal::entityTypeManager()->getStorage('')"][$type] = '\\' . get_class($class);
73 $content = _drush_phpstorm_metadata_phpstorm_metadata_template($map);
74 file_put_contents(DRUPAL_ROOT . '/.phpstorm.meta.php', $content);
77 function _drush_phpstorm_metadata_phpstorm_metadata_template($data) {
80 namespace PHPSTORM_META {
82 /** @noinspection PhpUnusedLocalVariableInspection */
83 /** @noinspection PhpIllegalArrayKeyTypeInspection */
84 $STATIC_METHOD_TYPES = [
87 foreach ($data as $method => $map) {
89 $file .= " {$method} => [\n";
91 foreach ($map as $argument => $class) {
92 $file .= " '{$argument}' instanceof {$class},\n";