Skip to content

Commit

Permalink
Add phpstorm completion for OOP hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-teck committed Dec 20, 2024
1 parent 0862ef0 commit cec4e2d
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Command/PhpStormMeta/Hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DrupalCodeGenerator\Command\PhpStormMeta;

use DrupalCodeGenerator\Asset\File;
use DrupalCodeGenerator\Helper\Drupal\HookInfo;
use DrupalCodeGenerator\Helper\Drupal\ModuleInfo;

/**
* Generates PhpStorm meta-data for Drupal hooks.
*/
final class Hooks {

/**
* Constructs the object.
*/
public function __construct(
private readonly HookInfo $hookInfo,
private readonly ModuleInfo $moduleInfo,
) {}

/**
* Generator callback.
*/
public function __invoke(): File {
$hooks = \array_keys($this->hookInfo->getHookTemplates());
$modules = \array_keys($this->moduleInfo->getExtensions());
return File::create('.phpstorm.meta.php/hooks.php')
->template('hooks.php.twig')
->vars(['hooks' => $hooks, 'modules' => $modules]);
}

}
1 change: 1 addition & 0 deletions src/Command/PhpStormMeta/PhpStormMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected function generate(array &$vars, Assets $assets): void {
$assets[] = (new FieldDefinitions($service('entity_type.manager'), $service('plugin.manager.field.field_type')))();
$assets[] = (new Fields($service('entity_type.manager'), $service('entity_field.manager'), $entity_interface))();
$assets[] = (new FileSystem())();
$assets[] = (new Hooks($this->getHelper('hook_info'), $this->getHelper('module_info')))();
$assets[] = (new Miscellaneous())();
$assets[] = (new Permissions($this->getHelper('permission_info')))();
$assets[] = (new Plugins($this->getHelper('service_info')))();
Expand Down
20 changes: 20 additions & 0 deletions templates/_phpstorm-meta/hooks.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PHPSTORM_META {

registerArgumentsSet('hooks',
{% for hook in hooks %}
'{{ hook }}',
{% endfor %}
);
registerArgumentsSet('modules',
{% for module in modules %}
'{{ module }}',
{% endfor %}
);
expectedArguments(\Drupal\Core\Hook\Attribute\Hook::__construct(), 0, argumentsSet('hooks'));
expectedArguments(\Drupal\Core\Hook\Attribute\Hook::__construct(), 2, argumentsSet('modules'));

}
42 changes: 42 additions & 0 deletions tests/functional/Generator/PhpStormMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function testGenerator(): void {
• .phpstorm.meta.php/field_definitions.php
• .phpstorm.meta.php/fields.php
• .phpstorm.meta.php/file_system.php
• .phpstorm.meta.php/hooks.php
• .phpstorm.meta.php/miscellaneous.php
• .phpstorm.meta.php/permissions.php
• .phpstorm.meta.php/plugins.php
Expand Down Expand Up @@ -71,6 +72,7 @@ public function testGenerator(): void {
$this->assertMiscellaneous();
$this->assertRoles();
$this->assertRoutes();
$this->assertHooks();
}

/**
Expand Down Expand Up @@ -715,4 +717,44 @@ private function assertRoutes(): void {
self::assertStringContainsString($arguments, $generated_content);
}

private function assertHooks(): void {
$generated_content = $this->getGeneratedContent('.phpstorm.meta.php/hooks.php');

$hooks = <<< 'PHP'
<?php
declare(strict_types=1);
namespace PHPSTORM_META {
registerArgumentsSet('hooks',
'query_alter',
'query_TAG_alter',
'schema',
'entity_access',
'ENTITY_TYPE_access',
'entity_create_access',
'ENTITY_TYPE_create_access',
'entity_type_build',
PHP;
self::assertStringContainsString($hooks, $generated_content);

$modules = <<< 'PHP'
registerArgumentsSet('modules',
'announcements_feed',
'automated_cron',
'big_pipe',
'block',
'block_content',
'breakpoint',
PHP;
self::assertStringContainsString($modules, $generated_content);

$arguments = <<< 'PHP'
expectedArguments(\Drupal\Core\Hook\Attribute\Hook::__construct(), 0, argumentsSet('hooks'));
expectedArguments(\Drupal\Core\Hook\Attribute\Hook::__construct(), 2, argumentsSet('modules'));
PHP;
self::assertStringContainsString($arguments, $generated_content);
}

}

0 comments on commit cec4e2d

Please sign in to comment.