-
Notifications
You must be signed in to change notification settings - Fork 17
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
The phpdoc array keys format supported by the plugin. #63
Comments
Very nice feature! Does this also allow for specification of a ('virtual') |
Also I'm curious... If I could add non-static PHP code like this, would that in turn also be type inferred? Or would that just be asking for too much? 😉 |
Hi. /**
* @method array getData() = [
* 'name' => 'John',
* 'age' => 27,
* 'children' => ['Tiffany', 'Bob', 'Marta'],
* ]
*/ I'll write back when this is done (in few days maybe). By non-static code you mean something like this? $product = ['name' => 'insurance', 'quantity' => 3, 'price' => '80.00'];
/** @var $sale = ['sellingPrice' => '300.00', 'product' => $product] */
$sale = $this->sellProduct($product); If so, there is no such feature yet either. Though I guess it could be added as well, if this is what you meant... |
2019.02.28.001 - added support for @bravoman, please, confirm that I understood you correctly about the non-static php code before I start implementing it. |
Sweet! Yes, that is what I meant! With regards to the non-static code I meant something like this: /**
* @method getById(int $id) = [
* 'id' => 1,
* 'username' => 'user_name'
* ]
* @method getAll() = [
* 0 => self::getById(1)
* ]
*/
class users_table {
} Edit: it seems this already works if I use 😅 |
I see this even works (assuming database is setup correctly in PHPStorm): /**
* @method getById(int $id) = (new \PDO('mysql:host=localhost;db=my_db_name;', 'random', 'random'))
* ->query('SELECT * FROM domain_comments LIMIT 1')
* ->fetch(\PDO::FETCH_ASSOC)
* @method getAll() = [
* 0 => users_table::getById()
* ]
*/
class users_table {
} You're my new hero ... 🏆 😉 |
Glad you liked it. |
Added support for |
PS: thanks for this plugin, I really miss auto-completion in legacy projects :) |
@AustinGrey, maybe psalm notation is what you are looking for? It's also mostly supported by the plugin: Click to expand code
/**
* @param $person_id
* @param array{
* filter_active: int,
* filter_pokemon: int|string,
* filter_min_date: string,
* filter_max_date: string,
* ignored_pokemon_ownership_ids: array<int>,
* } $options
*/
public function get_owned_pokemons($person_id, $options=[])
{
$options[''];
} I also accidentally found out that phpstorm syntax tree parser chews such writing pretty well: Click to expand code
/**
* @param $person_id
* @param $options = [
* 'filter_active' => (int), // if true, only active pokemon shown
* 'filter_pokemon' => (int) ?: (string), // only shows those pokemons whose name or id appear in the array
* 'filter_min_date' => (string),
* 'filter_max_date' => (string),
* 'ignored_pokemon_ownership_ids' => [(int)], // an array of pokemon_ownership_ids that will not be included
* ]
*/
public function get_owned_pokemons($person_id, $options=[])
{
$options[''];
} |
Oh man this is great! Yeah looks like psalm/phan notation would be the ideal but since there's no comments allowed yet I'll use the casting format you found second until comments come. Thanks! |
@AustinGrey, I just added basic comments support in psalm format in 2019.06.13.001: Click to expand code/**
* @param $person_id
* @param array{
* filter_active: int, // comment 1
* filter_pokemon: int|string,
* filter_min_date: string, // comment 2
* filter_max_date: string,
* ignored_pokemon_ownership_ids: array<int>, // comment 3.1
* // comment 3.2
* } $options
*/
public function provide_psalmKeyComments($person_id, $options=[])
{
$options[''];
return [
[$options, ['filter_active', 'filter_pokemon', 'filter_min_date', 'filter_max_date', 'ignored_pokemon_ownership_ids']],
];
} |
Hey @klesun, in reference to the following:
Just thought I'd double check (and I may be doing something wrong) but does this newer psalm commenting support only work for the @param tag? I tried using it with the @var tag with no luck. I can definitely use the parenthetical type + comment format you discovered above if the new format doesn't work for the @var tag yet. Dope plugin BTW. |
Hi, @miquelbrazil. I'm also opening a ticket to make parser still provide at least some completion even if it fails at some point to make it clear where you have the mistake if any - #99 |
Thanks for the quick reply @klesun So a couple of things I realized in my noobish-ness with trying to get better at commenting:
Nonetheless, my original version looked like the following: You can see the parser accurately detected the current value of the element after the All that said, I reformatted the docBlock to a class-level block and changed the tag to @Property. In this variation, I got the following: Correct current value, but no type detections and no comments. I guess it's safe to say that @Property might not be a supported tag (but when I think about it, why would it need to be since you generally could define the structure of the property within the class anyway). Which led me to my next iteration – psalm notation in the docBlock and comments following each array element: Now we have comments but no type detection. Which still supports my previous theory about psalm notation possibly being unsupported for the @Property tag. So my final attempt was to see how well the tree parser format worked with comments on the @Property tag: Now we have current value, comments and type detection (I realize So I think I will go with the final format, but I figured writing all these observations up might be helpful as you work on the plugin. So sorry it's so long. A lot of this is my desire to get better at writing phpDocumentation (which is how I stumbled upon your plugin in the first place). Thanks so much again for looking at this and being so responsive. |
It's in my TODO list - #94 (comment) (will implement it this weekend I hope)
Yeah, plugin does not support psalm format in the Thanks for the detailed explanation! |
Do you have a hint for hinting the return value of a function/method that is a generator / returning a Generator which generates an array (list) each is an array (list) which would map what I've read from the Psalm syntax like so:
so to say many of them. The exemplary code of the generator would look like:
And here is the usage example:
(simplified example, point in case is here that I tried with I also dropped the Any feedback really appreciated. /E: I could make some progress with a line like this (note the [] at the end):
I can ctrl+click in Phpstorm the method Any idea? |
@ktomk I have a strong feeling that what you are looking for is PSALM's templates syntax:
|
Thanks for this example, it does work as well and the result looks the same: Your Phpstrom extension provides the correct hints, it's just that Phpstorm can't infer the correct type for |
@ktomk note, there is an open issue with typing of Upd.: oh, lol, they're going to finally fix it in |
Awesome, thanks a lot |
most likely b/c there is only the first (0) key in the |
That is the question: How to make it think that there is not only first (0) key but over 9000 keys? :)
Sorry for that, here is the text form of this exaple:
|
hard to answer honestly (for myself) as I don't know and would need to guess (never tried before) which brings me to the idea if not already
perhaps works? would offer the "typed" array as an array (similar to what Phpstorm allows to annotate, albeit it has types as names, not the |
As there weren't three guesses yet, maybe a psalm type [1] ?
1: https://psalm.dev/docs/annotating_code/supported_annotations/#psalm-type |
Hi. @JustACatRu, you probably want: /**
* @var $employees = [
* $index => [
* 'salary' => 800.00,
* 'position' => 'frontend developer',
* 'fullName' => 'Pupkin Vasja',
* ]
* ]
*/
$employees = json_decode('dummy');
$employees[1][''] You can also express that with inline PSALM notation: /**
* @var array<array{
* salary: float,
* position: string,
* fullName: string,
* }> $employees
*/
$employees = json_decode('dummy');
$employees[5]['']
|
Neither... But thanks for trying!
Yep! This one is what I want! Works like a charm. Thanks! PS: Thank you for the great plugin! |
The README briefly mentions an ability to specify array keys in a doc. I'll describe it a bit more detailed here...
So, basically, in the
@param
,@var
,@property
,@method
and@return
phpdoc tags, after the variable name, you can type an=
followed by any valid php expression - it will be parsed by the plugin and used in completion on further references to this variable.Example: (specify that var contains the value returned by a function)
Another example: (specify associative array keys)
Note that these type annotation will also work for argument key completion when you are calling the function:
Another example: (specify return type keys)
(an
=
between@return
and the expression is allowed, but not required)As discussed in Enumeration #50, you can specify "enum" string value options in phpdoc:
As mentioned below, you can specify magic method return type like that:
As described in centralize configure as similar as .phpstorm.meta.php of phpstorm #75, you can now specify function return type and it's argument types in .phpstorm.meta.php
As described in Support PSALM/phan phpdoc notation #77, you can now use psalm/phan
array{key1: int, key2: string}
notation to specify key types (with@template
generics, yay)As described in Implement WordPress-like documentations //
Magic::argType()
#152, you can now use WordPress format notation to specify key typesAs described in Twig type hints don't seem to work with completion for me #165 and Allow to specify php data reference function FQN in twig comment #119, deep-assoc also allows you to reference functions by fully qualified name in Twig templates to use them as type information for vars completion:
Considering we have such php class somewhere in the project:
You can reference it in a twig file with
{# data-source = ... #}
:There is a set of magic function names you can use in doc comments to reference plugin-specific resolution tools:
@param $row = Magic::dbRow('my_table_name')
to tell IDE that$row
is an associative array with keys matching columns in themy_table_name
. For that to work, you need Data Source with your database credentials configured. Note, you can use any expression instead of'my_table_name'
string, like$this->tableName
orstatic::getTableName()
If there are any questions concerning the format, you are welcome to file an issue - I'll answer there, and if it involves adding more formats, will reference your issue here.
The text was updated successfully, but these errors were encountered: