phpstorm - Generate hints about civicrm_api4()
authorColeman Watts <coleman@civicrm.org>
Sat, 26 Aug 2023 02:14:23 +0000 (19:14 -0700)
committerTim Otten <totten@civicrm.org>
Sat, 26 Aug 2023 02:20:57 +0000 (19:20 -0700)
tools/extensions/phpstorm/Civi/PhpStorm/Api4Generator.php [new file with mode: 0644]

diff --git a/tools/extensions/phpstorm/Civi/PhpStorm/Api4Generator.php b/tools/extensions/phpstorm/Civi/PhpStorm/Api4Generator.php
new file mode 100644 (file)
index 0000000..62a6f65
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+namespace Civi\PhpStorm;
+
+use Civi\Core\Service\AutoService;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+/**
+ * @service civi.phpstorm.api4
+ */
+class Api4Generator extends AutoService implements EventSubscriberInterface {
+
+  public static function getSubscribedEvents() {
+    return [
+      'civi.phpstorm.flush' => 'generate',
+      'hook_civicrm_post::CustomGroup' => 'generate',
+    ];
+  }
+
+  public function generate() {
+    /*
+     * FIXME: PHPSTORM_META doesn't seem to support compound dynamic arguments
+     * so even if you give it separate lists like
+     * ```
+     * expectedArguments(\civicrm_api4('Contact'), 1, 'a', 'b');
+     * expectedArguments(\civicrm_api4('Case'), 1, 'c', 'd');
+     * ```
+     * It doesn't differentiate them and always offers a,b,c,d for every entity.
+     * If they ever fix that upstream we could fetch a different list of actions per entity,
+     * but for now there's no point.
+     */
+
+    $entities = \Civi\Api4\Entity::get(FALSE)->addSelect('name')->execute()->column('name');
+    $actions = ['get', 'save', 'create', 'update', 'delete', 'replace', 'revert', 'export', 'autocomplete', 'getFields', 'getActions', 'checkAccess'];
+
+    $builder = new PhpStormMetadata('api4', __CLASS__);
+    $builder->registerArgumentsSet('api4Entities', ...$entities);
+    $builder->registerArgumentsSet('api4Actions', ...$actions);
+    $builder->addExpectedArguments('\civicrm_api4()', 0, 'api4Entities');
+    $builder->addExpectedArguments('\civicrm_api4()', 1, 'api4Actions');
+    $builder->write();
+  }
+
+}