-
-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27179 from totten/master-phpstorm-setting
PhpStorm - Add a corybantic avalanche of type-hints
- Loading branch information
Showing
7 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Civi\PhpStorm; | ||
|
||
use Civi\Core\Service\AutoService; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* @service civi.phpstorm.api3 | ||
*/ | ||
class Api3Generator extends AutoService implements EventSubscriberInterface { | ||
|
||
public static function getSubscribedEvents() { | ||
return [ | ||
'civi.phpstorm.flush' => '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 = \civicrm_api3('entity', 'get', []); | ||
$actions = ['create', 'delete', 'get', 'getactions', 'getcount', 'getfield', 'getfields', 'getlist', 'getoptions', 'getrefcount', 'getsingle', 'getunique', 'getvalue', 'replace', 'validate']; | ||
|
||
$builder = new PhpStormMetadata('api3', __CLASS__); | ||
$builder->registerArgumentsSet('api3Entities', ...$entities['values']); | ||
$builder->registerArgumentsSet('api3Actions', ...$actions); | ||
$builder->addExpectedArguments('\civicrm_api3()', 0, 'api3Entities'); | ||
$builder->addExpectedArguments('\civicrm_api3()', 1, 'api3Actions'); | ||
$builder->write(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
tools/extensions/phpstorm/Civi/PhpStorm/EventGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Civi\PhpStorm; | ||
|
||
use Civi\Core\CiviEventDispatcher; | ||
use Civi\Core\CiviEventDispatcherInterface; | ||
use Civi\Core\CiviEventInspector; | ||
use Civi\Core\Service\AutoService; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* @service civi.phpstorm.event | ||
*/ | ||
class EventGenerator extends AutoService implements EventSubscriberInterface { | ||
|
||
public static function getSubscribedEvents() { | ||
return [ | ||
'civi.phpstorm.flush' => 'generate', | ||
]; | ||
} | ||
|
||
public function generate() { | ||
$inspector = new CiviEventInspector(); | ||
|
||
$entities = \Civi\Api4\Entity::get(FALSE)->addSelect('name')->execute()->column('name'); | ||
$specialEvents = ['hook_civicrm_post', 'hook_civicrm_pre', 'civi.api4.validate']; | ||
foreach ($entities as $entity) { | ||
foreach ($specialEvents as $specialEvent) { | ||
$entityEvents [] = "$specialEvent::$entity"; | ||
} | ||
} | ||
// PHP 7.4 can simplify: | ||
// $entityEvents = array_map(fn($pair) => implode('::', $pair), \CRM_Utils_Array::product([$entities, $specialEvents])); | ||
|
||
|
||
$all = array_merge(array_keys($inspector->getAll()), $entityEvents); | ||
|
||
$builder = new PhpStormMetadata('events', __CLASS__); | ||
$builder->registerArgumentsSet('events', ...$all); | ||
|
||
foreach ([CiviEventDispatcher::class, CiviEventDispatcherInterface::class] as $class) { | ||
foreach (['dispatch', 'addListener', 'removeListener', 'getListeners', 'hasListeners'] as $method) { | ||
$builder->addExpectedArguments(sprintf("\\%s::%s()", $class, $method), 0, 'events'); | ||
} | ||
} | ||
|
||
$builder->write(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
tools/extensions/phpstorm/Civi/PhpStorm/SettingsGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Civi\PhpStorm; | ||
|
||
use Civi\Core\Service\AutoService; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* @service civi.phpstorm.settings | ||
*/ | ||
class SettingsGenerator extends AutoService implements EventSubscriberInterface { | ||
|
||
public static function getSubscribedEvents() { | ||
return ['civi.phpstorm.flush' => 'generate']; | ||
} | ||
|
||
public function generate() { | ||
$metadata = \Civi\Core\SettingsMetadata::getMetadata(); | ||
$methods = ['get', 'getDefault', 'getExplicit', 'getMandatory', 'hasExplicit', 'revert', 'set']; | ||
$builder = new PhpStormMetadata('settings', __CLASS__); | ||
$builder->registerArgumentsSet('settingNames', ...array_keys($metadata)); | ||
foreach ($methods as $method) { | ||
$builder->addExpectedArguments('\Civi\Core\SettingsBag::' . $method . '()', 0, 'settingNames'); | ||
} | ||
$builder->write(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters