phpstorm - Generate hints for Civi::paths()
authorTim Otten <totten@civicrm.org>
Wed, 11 Oct 2023 03:14:13 +0000 (20:14 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 11 Oct 2023 03:14:13 +0000 (20:14 -0700)
tools/extensions/phpstorm/Civi/PhpStorm/PathGenerator.php [new file with mode: 0644]

diff --git a/tools/extensions/phpstorm/Civi/PhpStorm/PathGenerator.php b/tools/extensions/phpstorm/Civi/PhpStorm/PathGenerator.php
new file mode 100644 (file)
index 0000000..9b1c496
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+namespace Civi\PhpStorm;
+
+use Civi\Api4\Entity;
+use Civi\Api4\Route;
+use Civi\Core\Service\AutoService;
+use Civi\Test\Invasive;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+/**
+ * @service civi.phpstorm.path
+ */
+class PathGenerator extends AutoService implements EventSubscriberInterface {
+
+  public static function getSubscribedEvents() {
+    return [
+      'civi.phpstorm.flush' => 'generate',
+    ];
+  }
+
+  public function generate() {
+    $pathVars = array_keys(Invasive::get([\Civi::paths(), 'variableFactory']));
+    $pathVarExprs = [];
+    foreach ($pathVars as $pathVar) {
+      $pathVarExprs[] = "[$pathVar]/.";
+    }
+
+    $builder = new PhpStormMetadata('paths', __CLASS__);
+
+    $builder->registerArgumentsSet('pathVars', ...$pathVars);
+    $builder->addExpectedArguments('\Civi\Core\Paths::getPath()', 0, 'pathVarExprs');
+    $builder->addExpectedArguments('\Civi\Core\Paths::getUrl()', 0, 'pathVarExprs');
+
+    $builder->registerArgumentsSet('pathVarExprs', ...$pathVarExprs);
+    $builder->registerArgumentsSet('pathVarAttrs', 'path', 'url');
+    $builder->addExpectedArguments('\Civi\Core\Paths::hasVariable()', 0, 'pathVars');
+    $builder->addExpectedArguments('\Civi\Core\Paths::getVariable()', 0, 'pathVars');
+    $builder->addExpectedArguments('\Civi\Core\Paths::getVariable()', 1, 'pathVarAttrs');
+
+    $builder->write();
+  }
+
+}