New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PhpStorm - Add a corybantic avalanche of type-hints #27179
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7043f4e
phpstorm - Enable 'scan-classes'
totten 6d39767
Use `hook_managed` to implement `civi.phpstorm.flush`
totten 056ff76
(NFC) phpstorm.php
totten 3c30e72
PhpStormMetadata - Define registerArgumentsSet() and addExpectedArgum…
totten e20befb
phpstorm - Generate hints about CiviCRM settings
totten a8d3377
phpstorm - Generate hints about civicrm_api4()
colemanw c9380d7
phpstorm - Generate hints about civicrm_api3()
totten f816f7f
phpstorm - Generate hints about Civi::dispatcher()
totten c2053df
phpstorm - Generate event hints for "hook_civicrm_pre::Entity" (and s…
totten File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you've defined a reusable set I guess it wouldn't hurt to add
civicrm_api
as well (it's basically v3-only).I like how I've been busy ripping out APIv3 examples at the same time you were adding this. Yin an Yang 🙃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@colemanw lol. Yeah... I like to the think that the net-change is positive (total size of removed doc-examples exceeds extra size of these hints).
It is easy to add
civicrm_api()
, and it's an acceptable cost. But I suppose #27180 gives a rationale for why we might not care to?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Things that make you go "meh"