-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
Inline element definitions #7
Comments
Would a definitions block elsewhere make more sense? This currently suggests multiple returns of |
My original thought on this was to give the definitions block scope. So the following business rules would be in use:
I would like to emphasize that I am not sold on using the |
Just for reference, I posted some notes on this subject here. |
I agree - why not simply use regular type-names? In languages where you can define function signatures as types, they are usually just regular type-identifiers like any other type. A type is a type - whether it's a concrete type, interface, trait, pseudo-type, or even type-alias, there is no good reason why the name of a type should reflect the kind of type. The kind (or "type of type") belongs in the type definition, not in the type name. |
The main problem I see with this proposal is that it is a global state construct. Similar to the
Basically for these reasons, the I understand the use-case, but I wonder whether there are sufficient real world situations in the wild that would significantly benefit from a native solution like this, and whether they outbalance aforementioned maintenance problems The inline suggestion in #19 made more sense to me, because it avoids the problem of global state in documentation. Lastly, since the topic starter is about an anonymous/stdClass object, #47 on documenting collections appears to be closely related. FWIW, the tag name |
As I see it a definition should always be related to a block element and not in the global space. This will automatically namespace it to prevent/reduce conflicts and provide a clear and definite location where to find the definition |
Allright, I have had time to think this through and would like to propose the following: Using inline statements you can
I would really love feedback, be it positive or negative on this suggestion |
My main issue with this, is the idea that definitions are names in a separate namespace, as defined by item 3 above. Why aren't they just type-names, in the same namespace as other types? The type namespace already contains things that aren't strictly classes, such as pseudo-types - why the In my opinion, type-definitions should be namespaced, but the defined type, like any other type, should belong to the namespace, not to a class/interface/trait - they should be declared at the file-level, most likely, in the namespace of the given file. I propose the following alternative to item 3: Define a re-usable type definition within the current namespace of a file, by using a file-level doc-block:
and invoke it with:
This is considerably simpler (to understand, and likely to implement) than permitting some new concept like "type definition" which can be nested inside all kinds of other elements - nesting in this way doesn't make much sense, because you end up effectively using e.g. the parent class-name or interface-name as merely a namespace; the definition is an independent element with an independent fully-qualified name, with a new syntax (the I also would recommend Note that assigning properties and methods to both existing and non-existing objects (as per item 2 above) is generally termed as "ambient declarations" - these are widely used in e.g. TypeScript, and the reasons for allowing these apply equally to PHP and JavaScript, so I'm glad to see you thought of these! The concept of type-definitions can be greatly simplified though - a type is a type, let's treat them as such, rather than inventing new syntax and more complex concepts? |
Perhaps I am too afraid of FQCN conflicts. On the other hand it might actually be a strength because you could replace the Regarding type-definition vs. typedef; I have done a bit more googling and also found the typedef tag in JSDoc which does a similar thing. So despite that I dislike such abbreviations I agree that it is a recognized term and would be better suited. |
In addition; perhaps we should even go further with this and allow
|
There is no reason to fear conflicts - just use namespaces, just as you would for any other type. No conflicts will occur then. (anymore than conflicts will occur between any other user-defined types.)
This is what I mean by refactoring - a type-name like "Foo\Bar#Baz" cannot refactor into a concrete type implementation. (because the
That is what I'm suggesting - but I'm suggesting also, that if you have this feature, there is actually no practical reason to define types nested inside other types, because the name of a class/interface/trait will not be treated like a type-name at all, it will be treated as a name-space. (unlike e.g. C#, PHP does not support nested types, which is why you don't have the |
Also, +1 for the two examples you quoted. What problem do you see the introduction of nested types solving? |
@mindplay-dk it was not necessarily intended as a nested type, moreover as preventing a conflict if there were a class with the same FQCN as the typedef |
Intended or not, the way it was proposed, the defined type is nested inside a real type, hence the
Understood - but as explained, that's what namespaces are for. You don't need to invent new syntax, or a new concept/idea, or a new name-space for that new concept/idea/type. Whether you have a We already have namespaces as the defined means of avoiding ambiguity and collisions in PHP - using class/trait/interface names as a kind of "extra" namespace for defined types is trying to address a problem that is already addressed by namespaces. It's no better or worse - it's just not necessary. If you did want an extra name for some reason, in case you're really worried about ambiguity in some case, you can just add another namespace... that is to say, |
I tend to disagree with your assessment but discussing that further is much better suited over a beer or other beverage as I think we agree on the direction :) I am now writing the first draft of an |
I wish I could meet you for a beer - we're probably a world apart ;-) |
Denmark - Holland ain't that far away; just a bit too far to drive for just a beer :) You are not attending conferences? |
I do attend conferences, but probably not in the very near future - my situation right now is a bit complicated... I just arrived back in Denmark a couple of months ago, after living in the US for the past 6 years - currently living with my parents, trying to get my life back on track, find a job, a place to live and so on... divorce... |
Ouch, those are difficult times indeed. Take care of yourself and I wish you strength in getting back on track |
Thanks man :-) |
@mindplay-dk @ashnazg and others, I have written a draft for the new |
Array keys can contain arbitrary bytes, so it should include a way to specify variable names beyond the allowed variable name characters. The same applies to object members. An empty name should also be possible, although it is hard to access on objects ( And I’d like to document the leaf level of deeply nested structures (well, your |
[Cache] Adding InvalidArgumentException
Two thoughts:
|
Still opened since 2013? No way to simply solve this? |
@teolaz I'm pretty sure this PSR is dead. |
I intend to resurrect it this spring, after phpdoc3 is done. |
Wouldn't it make more sense to resurrect before you finish the new phpdoc version, so you can make the new phpdoc version match the spec? |
Indeed... a better description of my plan is to get the CI of everything underlying phpdoc3 in place... then, while the others work on completing their own phpdoc3 stuff, I'll switch to the PSR5 WG effort. I had realized that I wouldn't have the time available to do both at the same time, so opted for the CI first. Once I get past the current blocker on that front (can't test new ReflectionDocBlock because PHPUnit requires Prophecy requires older ReflectionDocBlock), I'll start reading up on how to get the WG created. |
For annotating arrays and possibly objects, supporting inline I like the approach mentioned in https://github.com/vimeo/psalm/wiki/Typing-in-Psalm#makeshift-structs for these reasons
/** @return array<int, array{name: string, type: string, active: bool}> */
function getToolsData() : array {
return [
['name' => 'Psalm', 'type' => 'tool', 'active' => true],
['name' => 'PhpParser', 'type' => 'tool', 'active' => true]
];
} Other thoughts:
|
IMHO it could be useful to in general be able to define/document the structure of data, not just array keys. |
Phan's array shapes syntax is reasonable and I like it, but the disadvantage is that this format can not contain documentation for array properties. |
Improve ContainerInterface::has() specification
* Added a short summary and "why bother" * Fixes * Fixes * Fixes
If this needs to be pursued further, please bring this up as a new thread on the FIG mailing list for discussion -- https://groups.google.com/forum/#!forum/php-fig |
Suppose I have a webservice that returns the following JSON:
In my code this would (simplistically speaking) be expressed by json_decoding the string and probably returning it in a method. The problem
here is that the caller only knows that an stdClass will return but nothing in the documentation makes clear what it represents or what
members it has.
As such I propose that we add the capability to define meta-elements in DocBlocks to be able to return information on these stdClasses.
For example:
as is visible; the concept of Inline DocBlocks is reused here. The details of this new syntax still need to
be written and defined, the tag name and method to invoke a definition are not final and should still
be considered
The text was updated successfully, but these errors were encountered: