Note: Please do not discuss the validity of passing information using an array versus an object. The basic premise is that this is a desired piece of functionality.In the current draft I have written a proposal where you can use brackets with a tag to indicate that there is more to that tag's contents than is described by the type alone. I have named this construct an `Inline PHPDoc` because syntactically it is a recursive repetition of a PHPDoc block. When combined with the `array` type it describes the contents of an associative array, or hash, and when combined with an `object` type it describes a class without declared members.
/**In the example above we demonstrate a constructor with (a contrived) example of a summary together with a single options array that is expected to contain two elements: required and label.
* Initializes this class with the given options.
*
* @param array $options {
* This is a description should you wish to add it.
*
* @type boolean $required Whether this element is required
* @type string $label The display name for this element
* }
*/
public function __construct(array $options = array())
{
<...>
}
- this notation
- which tag to use for hashes (for objects @property and @method work nicely)
- how to indicate required or optional members
- whether or not to prefix the member name with a dollar; I introduced this to be consistent with other uses of @var/@type and to distinguish between a type and a member name
I would like to ask for your opinion on
- this notation
- how to indicate required or optional members
- whether or not to prefix the member name with a dollar
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/3dbe1d5f-0965-4c14-8133-60ef231247ce%40googlegroups.com.--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/CAMGK6yjMzxfT0ZshELzYH-YKAJkgT_ZHRk5%3DZwBtTj8XsQF17g%40mail.gmail.com.
- this notation
- which tag to use for hashes (for objects @property and @method work nicely)
- how to indicate required or optional members
- whether or not to prefix the member name with a dollar; I introduced this to be consistent with other uses of @var/@type and to distinguish between a type and a member name
I agree with Alexander that @key might be more fitting, though it could create confusion if this is multi-purpose for arrays and objects.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/e702ee3f-cdbf-43f3-babc-4ce250baa3a5%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/54402A32.6070403%40gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/e702ee3f-cdbf-43f3-babc-4ce250baa3a5%40googlegroups.com.
>>/**
>> *
>> * @param array $map {
>> * @type map
>> * @key string
>> * @value integer
>> *} This would define a hash with >>string keys and integer values
>> */
I then would know it's a hash. 5 lines of comments seem very much for such limited Information.
>>/**
>> *
>> * @param array $map {
>> * @type list
>> * @value integer
>> *} This would define a 0-indexed >>array of values
>> */
Isn't this the same as
@param int
Mike,
would it be possible to nest this notation?
/**
* Initializes this class with the given options.
*
* @param array $options {
* This is a description should you wish to add it.
*
* @type boolean $required Whether this element is required
* @type string $label The display name for this element
* @type array[] $fieldSet {
* @type string $name
* @type string $ baz
* }
* }
*/
public function __construct(array $options = array())
{
<...>
}
How could this definition be reused? (This requirement has been discussed before, I believe. Ultimately discarded because this added another set of headaches. Where to put it? Should namespaces be used? If so, please ignore.)
I like the feature, but I don't quite dig the proposed notation.
IMHO it's not immediately clear about the fact that it describes an associative array instead of a indexed one (even if the latter is not the use case for the notation, that's beside my point). I think the dollar notation and the ambiguity of the @type tag contributes to this feeling. Especially the dollar, I find its semantic not very transparent and potentially confusing.
I would personally prefer the key names to be enclosed with quotes, as they usually appear in the code.
About the tag, since it's actually describing a value, and not a key, using @key or something like that is out of the question. Maybe @value, since "@type string" and "@type integer" happen to be also valid key annotations, and that would be ambiguous.
I don't think any tag is required to describe the key. Either it's a string or it's an integer, but they can all be assumed as strings because numbers will be type casted.Ultimately, what matters is the semantic of the array values.
For the required annotation, I honestly don't know, using the description arbitrarily seems the most readable thing.
Bottom line, I would rather like something like this...
/**
* @param array $options {
* @value boolean 'foo' (REQUIRED) Description lorem ipsum dolor* @value string 'bar' Description lorem ipsum dolor
* }
*/
It comes down very much to personal taste, though.
Anyway thank you, and keep up the good work.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/CAFojS1vOWNVjfyYbn0GVyhoSn080xECUN-LB9s9izs-bcg%3DSxA%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/899fdebd-c037-45da-be3e-40d4e0c7faaf%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/5440B777.9010404%40gmail.com.
I'm inclined toward the `keyText` format, since it is exactly that given Key string that we document via one @var line:Functionally speaking, is there any other One True Identifier for that given member of the array? If we formalize a syntax for this identifier, it seems to me that the actual string that is the member's Key is the proper identifier.
/**
* Short Description
*
* Long Description foo bar baz,
* foosum barsum bazsum,
* fee figh foh fum.
*
* @param array $options {
* Description for $options itself goes here.
*
* @var bool 'required' the value here should be a bool flag
* @var string 'label' the value here should be a string
* }
*/
public function __construct(array $options = array()) {}
Regarding required vs optional members of an array, what do we want to accomplish by building a syntax for it? Does it influence IDE autocomplete behavior in some way? Should the documentation somehow link to something else based on that identifier? I'm not yet convinced this one aspect of info justifies a syntax of its own, since that info can be conveyed in the @var's text description already.
In the case of an inline doc for an object (that presumably is not itself already a declared class, and thus has its own docs elsewhere), I agree that @property is valid for things like \stdClass->prop1, and would be written as "@var bool $prop1 here is description". However, is it even feasible that an undeclared method can exist on \stdClass? Maybe I'm missing something here.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/5440B777.9010404%40gmail.com.
--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/CANsgjnvCxZqVz8E3LGe%2BXQ9ERGEWksKsFWujfQnZ-idUpmDUCA%40mail.gmail.com.
/**
* Initializes this class with the given options.
*
* @param array $options {
* This is a description should you wish to add it.
*
* @item boolean 'required' Whether this element is required
* @item string 'label'? The display name for this element
* @item int:array '42'? Magic numbers array
* }
*/
Thank you all for your input regarding this feature. In order to get a little overview I have compiled this summary of the discussion so far. Please note that this is mixed up with my opinion so this is not a completely objective summary but more a basis for refinement. I have tried to mention which items are my own opinion. Please correct me if I am wrong.
New proposal
-------------------
Before describing the results in the compiled summary I would like to do a new proposal based on this discussion. I am really interested to hear if you have objections to this notation.
```
@var array<string,mixed> $field {
A field definition describing the information on how to render a single input element in a form.
@element string 'label'
@element string 'type'
@element array 'options'? {
@element boolean 'required'? Whether this field must have a value for the form to validate.
@element string 'helpText'? A text that is shown with the field to provide additional information
}
}
```
In the following chapters I summarize what has been discussed so far and what I see as caveats for certain options or just my opinion (since most of this is a matter of taste).
Topics
------
## Documenting key types
During the discussion the topic of documenting the type of a key has surfaced including suggestions for notations. As I see it no notation needs to be introduced for this as the variable name for the value already indicates what type it is and otherwise the Collection notation can be used: `array<keyType,valueType>`.
## Tag name for documenting an element
I would also like to add another that I just thought of:
1. @element
Just as @property and @method are explicit for objects can @element be explicit for arrays. After comparing the semantics of each one my personal favorites are either `@item` or `@element` since one talks about array items or array elements. Though more characters in the end I prefer @element as I think it is the most name.
## Optional members
A variety of suggestions have come by but one thing appears to be dominant: you indicate which items are optional and each element is considered required by default.
The following notations have come by (all other parts are taken from the original proposal; this is only about the optional indication):
1. `@type integer $myInteger optional This is an optional number.`
2. `@type optional integer $myInteger This is an optional number.`
3. `@type integer $myInteger? This is an optional number.`
4. `@type:optional integer $myInteger This is an optional number.`
5. `@typeOptional integer $myInteger This is an optional number.`
My personal favorites are number 3 and 4; number 3 is nicely concise but number 4 is explicit and makes an interesting use of the new specialization notation.
## How to indicate variable name
One of the things that is under discussion is how to represent the key name for a given element in an array, here we also found several variations during the discussions:
1. `@type integer myInteger This is a number.`
2. `@type integer $myInteger This is a number.`
3. `@type integer 'myInteger' This is a number.`
I used to like option 2 but thanks to this topic I have shifted in favor of number 3. I hadn't considered number 3 before and there is one argument in specific that made me interested in this one: keys can have spaces (and other special characters). Option 1 and 2 will not work if a key has spaces in it.
## Array vs. Objects
Another question was whether it would also be possible to document objects this way as sometimes you have anonymous objects (for example json-decoded output).
The proposal that I made supports this specific use-case:
```
@var object $myObject {
This is a summary for this object.
This is a description for this object.
@property integer $myProperty
}
```
You can even use this to augment existing objects in case they do not have the right `@method` tags or otherwise expose magic behaviour that you'd want to capture:
```
@var MyMagicClass $object {
@method string getMagic()
}
```
This last example also shows that the summary and description are entirely option (as with a normal DocBlock). This notation is also an example of 'Inline PHPDoc' where an entire PHPDoc can be nested inside another.
Once again, Mike, thank you for the thorough writeup.
On 10/18/2014 02:58 AM, Mike van Riel wrote:
Thank you all for your input regarding this feature. In order to get a little overview I have compiled this summary of the discussion so far. Please note that this is mixed up with my opinion so this is not a completely objective summary but more a basis for refinement. I have tried to mention which items are my own opinion. Please correct me if I am wrong.
New proposal
-------------------
Before describing the results in the compiled summary I would like to do a new proposal based on this discussion. I am really interested to hear if you have objections to this notation.
```
@var array<string,mixed> $field {
A field definition describing the information on how to render a single input element in a form.
@element string 'label'
@element string 'type'
@element array 'options'? {
@element boolean 'required'? Whether this field must have a value for the form to validate.
@element string 'helpText'? A text that is shown with the field to provide additional information
}
}
```
In the following chapters I summarize what has been discussed so far and what I see as caveats for certain options or just my opinion (since most of this is a matter of taste).
Topics
------
## Documenting key types
During the discussion the topic of documenting the type of a key has surfaced including suggestions for notations. As I see it no notation needs to be introduced for this as the variable name for the value already indicates what type it is and otherwise the Collection notation can be used: `array<keyType,valueType>`.
Since PHP only supports scalars as array keys (you need SplObjectHash if you want object keys on something, IIRC), and all scalars are always coercible to each other, when would you need to document the type of the key? The value, sure, but the key?
## Tag name for documenting an element
*snip*
I would also like to add another that I just thought of:
1. @element
Just as @property and @method are explicit for objects can @element be explicit for arrays. After comparing the semantics of each one my personal favorites are either `@item` or `@element` since one talks about array items or array elements. Though more characters in the end I prefer @element as I think it is the most name.
I don't have a strong feeling here, but both @item and @element would be acceptable on my end.
## Optional members
A variety of suggestions have come by but one thing appears to be dominant: you indicate which items are optional and each element is considered required by default.
The following notations have come by (all other parts are taken from the original proposal; this is only about the optional indication):
1. `@type integer $myInteger optional This is an optional number.`
2. `@type optional integer $myInteger This is an optional number.`
3. `@type integer $myInteger? This is an optional number.`
4. `@type:optional integer $myInteger This is an optional number.`
5. `@typeOptional integer $myInteger This is an optional number.`
My personal favorites are number 3 and 4; number 3 is nicely concise but number 4 is explicit and makes an interesting use of the new specialization notation.
There was one other option proposed I think is worth considering:
@item string 'foo'='bar' Some description here.
That is, if the key is omitted it will behave as if you passed "bar". If you do not omit it, it will do, er, whatever it is you passed. That implies the value is optional since otherwise having a default would be nonsensical. That is the same logic as PHP itself uses for parameter passing. I think the idea has merit.
If not that, then I agree option 3 (@element string 'foo'?) is far and away the best of the other options.
<snip>
<snip>
1. @type
2. @value
3. @key
4. @var
5. @item
## Optional members
1. `@type integer $myInteger optional This is an optional number.`
2. `@type optional integer $myInteger This is an optional number.`
3. `@type integer $myInteger? This is an optional number.`
4. `@type:optional integer $myInteger This is an optional number.`
5. `@typeOptional integer $myInteger This is an optional number.`
My personal favorites are number 3 and 4; number 3 is nicely concise but number 4 is explicit and makes an interesting use of the new specialization notation.
@element boolean $myBool This is my description.
@element:optional integer $myInteger This is my description.
## How to indicate variable name
1. `@type integer myInteger This is a number.`
2. `@type integer $myInteger This is a number.`
3. `@type integer 'myInteger' This is a number.`
# Notation
/**
* @param Service $myService
* @param array $options Additional options
* @param boolean $options[required] Whether this element is required
* @param string $options[label] The display name for this element
*/
compared to (notation of original proposal used for display purposes):
/**
* @param Service $myService
* @param array $options {
* Additional options
* @type boolean $required Whether this element is required
* @type string $label The display name for this element
* }
*/
<snip>
## Optional members
1. `@type integer $myInteger optional This is an optional number.`
2. `@type optional integer $myInteger This is an optional number.`
3. `@type integer $myInteger? This is an optional number.`
4. `@type:optional integer $myInteger This is an optional number.`
5. `@typeOptional integer $myInteger This is an optional number.`
My personal favorites are number 3 and 4; number 3 is nicely concise but number 4 is explicit and makes an interesting use of the new specialization notation.
Hmm. I'm not really a fan of either #3 or #4. I think semantically it's not clear what ? (#3) means at first grok. I think it's important to remember that inline docs need to be understandable in the code as well as in parsed documentation.
Even worse: my first priority lies in that it needs to be understandable in the code first! Tooling is nice and all but when push comes to shove you should be able to rely on a single information source, your repository. (that is why I belief in ReST or to a lesser extent Markdown documentation in your repo!)
Specializing the tag in #4 makes the intent vastly more obvious, but could really start making lines quite long, especially if you're aligning.
Case in point, we're 38 characters in before even get to the description. And that's for a short key name:
@element boolean $myBool This is my description.
@element:optional integer $myInteger This is my description.
I guess my question would be ... what's the current standard for marking regular @param values as optional? Can we mimic that?
## How to indicate variable name
1. `@type integer myInteger This is a number.`
2. `@type integer $myInteger This is a number.`
3. `@type integer 'myInteger' This is a number.`
I think I could get on board with 'myInteger'. I still think the $ would ease parsing but it's definitely a little less ambiguous with quotes..
# Notation
/**
* @param Service $myService
* @param array $options Additional options
* @param boolean $options[required] Whether this element is required
* @param string $options[label] The display name for this element
*/
compared to (notation of original proposal used for display purposes):
/**
* @param Service $myService
* @param array $options {
* Additional options
* @type boolean $required Whether this element is required
* @type string $label The display name for this element
* }
*/
I think I'm biased toward the original proposal, though I'd add a new line after the description. I think from a parsing perspective it'll be a lot more difficult to snag the description followed by the opening brace than it would be to simply have the hash description on a new line just inside the hash. And this applies in nesting situations as well.
Sorry, can you elaborate on your example? A class doesn't support the @var tag. Did you perhaps intend to use the @property tag to dynamically add a new property named $object to the class Foo, that is intended to contain an object of class Foo that in turn has a dynamically defined method bar()?
Right?