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
Usefulness of attributes #312
Comments
Attributes date back from sometime around the start of this project. At that time nodes extended Do you know whether PhpStorm has some kind of mechanism to extend the properties of a class in a separate declaration? The |
Awesome, this will make code so much less verbose. The best thing is that this can totally happen in a backwards-compatible manner,
Sorry, what do you mean by that? I'm not working with PhpStorm but I imagine something like the example I gave in the OP should work |
Draft implementation: https://github.com/nikic/PHP-Parser/compare/attributes Note that this keeps the backing
Right, of course you aren't ^^ I was hoping for some magic to make this work without having to explicitly declare the node to be of a different type. |
An IDE should be able to read |
The initial construction is https://github.com/nikic/PHP-Parser/blob/master/lib/PhpParser/NodeAbstract.php#L15. This would require copying all the attributes into the dynamic object properties. If you're considering this from a performance angle, this is expensive for the common case where attributes are not actually used much. From an implementation perspective another problematic part is https://github.com/nikic/PHP-Parser/blob/master/lib/PhpParser/NodeAbstract.php#L105. This would need to gather the object properties, while excluding subnodes and declared properties that are not subnodes -- this comes down to using reflection, which I'm not keen on.
The IDE can read Of course you are right that magic get/set is slow. This certainly does not make sense if the goal is to improve performance -- is it? Is attribute fetching a bottleneck for you? |
Hm, valid points. Cannot confirm that it is a performance bottleneck for me, but it is verbose for sure. I mostly see the benefit in cleaner code. But if the implementation is too hard of course we can leave it. |
@nikic I'm a bit lost in the source code, could you give me a link where the nodes are instantiated with |
@felixfbecker This happens in generated code. Something like |
Btw, another potential complication with using dynamic properties is that you can no longer have a subnode and attribute with the same name, which is currently supported. (Personally not particularly concerned about that.) |
Ah, that explains why I couldn't fin it. Well the easy solution would be to just do foreach ($attributes as $attr => $val) {
$this->$attr = $val;
} in $node->attribute1 = value1;
$node->attribute2 = value2; For me, this makes a lot of sense. They become actual attributes of the node.
return array_diff_key(get_object_vars($this), array_flip($this->getSubNodeNames())); Even if As it is targeted for a major release people will have to do some changes to their code anyway and converting to the new style is a simple regexp search/replace.
Thought about that too, which is why it's safer to do in a major version. But can you really imagine having a function node with an additional property |
@nikic I profiled my language server because I need to improve performance and it turns out |
What percentage of the time is it though? |
Don't have the profile open atm but it was about 4% |
What is this issue actually about? I've noticed one note about autocomplete of And use like this: $node->getAttribute(Attributes::TYPES); Having constants makes it very hard to make a typo, compared to string naming. Also I prefer current state using explicit methods than hiding it behind magic. I have very bad experience with that due to assumptions that other packages don't have to make. It was good and fast idea on first site though, so I understand your need. I suggest either closing this or resolving it. Depending on your real current needs for this. |
I think this one can be closed too, as:
|
Given that dynamic properties are deprecated now, I think it's safe to close this. |
I was wondering what was the design decision behind adding an associative array of "attributes" to each node instead of simply patching attributes on the node directly. Calling
getAttribute('myAttribute')
everytime is a lot of boilerplate and has the performance overhead of a function call every time.Just like JavaScript, PHP has excellent support for decorating objects with additional attributes without any warning or performance impact - it's even used by native
json_decode
orPDO::FETCH_OBJECT
.If I see remember correctly,
$namespacedNode
actually is also patched this way by theNodeResolver
and not a defined property. I have to work with custom attributes a lot and always callinggetAttribute()
with the string name feels like working with just a raw array instead of an object.Besides less code and better performance, it would have benefits for static tooling:
With attributes, you could give static tooling a hint:
The text was updated successfully, but these errors were encountered: