Skip to content

Commit cec4e2d

Browse files
committed
Add phpstorm completion for OOP hooks
1 parent 0862ef0 commit cec4e2d

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

src/Command/PhpStormMeta/Hooks.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DrupalCodeGenerator\Command\PhpStormMeta;
6+
7+
use DrupalCodeGenerator\Asset\File;
8+
use DrupalCodeGenerator\Helper\Drupal\HookInfo;
9+
use DrupalCodeGenerator\Helper\Drupal\ModuleInfo;
10+
11+
/**
12+
* Generates PhpStorm meta-data for Drupal hooks.
13+
*/
14+
final class Hooks {
15+
16+
/**
17+
* Constructs the object.
18+
*/
19+
public function __construct(
20+
private readonly HookInfo $hookInfo,
21+
private readonly ModuleInfo $moduleInfo,
22+
) {}
23+
24+
/**
25+
* Generator callback.
26+
*/
27+
public function __invoke(): File {
28+
$hooks = \array_keys($this->hookInfo->getHookTemplates());
29+
$modules = \array_keys($this->moduleInfo->getExtensions());
30+
return File::create('.phpstorm.meta.php/hooks.php')
31+
->template('hooks.php.twig')
32+
->vars(['hooks' => $hooks, 'modules' => $modules]);
33+
}
34+
35+
}

src/Command/PhpStormMeta/PhpStormMeta.php

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ protected function generate(array &$vars, Assets $assets): void {
6868
$assets[] = (new FieldDefinitions($service('entity_type.manager'), $service('plugin.manager.field.field_type')))();
6969
$assets[] = (new Fields($service('entity_type.manager'), $service('entity_field.manager'), $entity_interface))();
7070
$assets[] = (new FileSystem())();
71+
$assets[] = (new Hooks($this->getHelper('hook_info'), $this->getHelper('module_info')))();
7172
$assets[] = (new Miscellaneous())();
7273
$assets[] = (new Permissions($this->getHelper('permission_info')))();
7374
$assets[] = (new Plugins($this->getHelper('service_info')))();
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPSTORM_META {
6+
7+
registerArgumentsSet('hooks',
8+
{% for hook in hooks %}
9+
'{{ hook }}',
10+
{% endfor %}
11+
);
12+
registerArgumentsSet('modules',
13+
{% for module in modules %}
14+
'{{ module }}',
15+
{% endfor %}
16+
);
17+
expectedArguments(\Drupal\Core\Hook\Attribute\Hook::__construct(), 0, argumentsSet('hooks'));
18+
expectedArguments(\Drupal\Core\Hook\Attribute\Hook::__construct(), 2, argumentsSet('modules'));
19+
20+
}

tests/functional/Generator/PhpStormMetaTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function testGenerator(): void {
3838
• .phpstorm.meta.php/field_definitions.php
3939
• .phpstorm.meta.php/fields.php
4040
• .phpstorm.meta.php/file_system.php
41+
• .phpstorm.meta.php/hooks.php
4142
• .phpstorm.meta.php/miscellaneous.php
4243
• .phpstorm.meta.php/permissions.php
4344
• .phpstorm.meta.php/plugins.php
@@ -71,6 +72,7 @@ public function testGenerator(): void {
7172
$this->assertMiscellaneous();
7273
$this->assertRoles();
7374
$this->assertRoutes();
75+
$this->assertHooks();
7476
}
7577

7678
/**
@@ -715,4 +717,44 @@ private function assertRoutes(): void {
715717
self::assertStringContainsString($arguments, $generated_content);
716718
}
717719

720+
private function assertHooks(): void {
721+
$generated_content = $this->getGeneratedContent('.phpstorm.meta.php/hooks.php');
722+
723+
$hooks = <<< 'PHP'
724+
<?php
725+
726+
declare(strict_types=1);
727+
728+
namespace PHPSTORM_META {
729+
730+
registerArgumentsSet('hooks',
731+
'query_alter',
732+
'query_TAG_alter',
733+
'schema',
734+
'entity_access',
735+
'ENTITY_TYPE_access',
736+
'entity_create_access',
737+
'ENTITY_TYPE_create_access',
738+
'entity_type_build',
739+
PHP;
740+
self::assertStringContainsString($hooks, $generated_content);
741+
742+
$modules = <<< 'PHP'
743+
registerArgumentsSet('modules',
744+
'announcements_feed',
745+
'automated_cron',
746+
'big_pipe',
747+
'block',
748+
'block_content',
749+
'breakpoint',
750+
PHP;
751+
self::assertStringContainsString($modules, $generated_content);
752+
753+
$arguments = <<< 'PHP'
754+
expectedArguments(\Drupal\Core\Hook\Attribute\Hook::__construct(), 0, argumentsSet('hooks'));
755+
expectedArguments(\Drupal\Core\Hook\Attribute\Hook::__construct(), 2, argumentsSet('modules'));
756+
PHP;
757+
self::assertStringContainsString($arguments, $generated_content);
758+
}
759+
718760
}

0 commit comments

Comments
 (0)