-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Print int custom raw value #1127
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
base: master
Are you sure you want to change the base?
Print int custom raw value #1127
Conversation
|
There is a version of the printer for which the node formatting is preserved (within reason). Maybe that could be done there automatically rather than hiding it behind an undocumented flag. |
Thanks for replying! For example, in a Cash context where amounts are in cents:
|
It's true, when using the preserving formatter, this is kept. But that's the point. We want to change existing integers and format them using underscores. That means that we don't want to preserve, want a complete new representation. Therefore, the preserving formatter does not have a role. $node = Int_::fromString('12_34');
// object(PhpParser\Node\Scalar\Int_)#2 (2) {
// ["attributes":protected]=>
// array(2) {
// ["rawValue"]=>
// string(5) "12_34"
// ["kind"]=>
// int(10)
// }
// ["value"]=>
// int(1234)
// }
var_dump($node);
echo new Standard()->prettyPrintExpr($node); // returns 1234 while we expect 12_34So there is no way with the Standard printer, and plain AST, to write it to format |
|
@samsonasik What do you think of this? I think it makes a lot of sense. Would be great if you could do a review as well. |
samsonasik
left a comment
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.
This should apply on float values as well?
the float method does not reach to the Meaning, |
samsonasik
left a comment
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.
Looks good to me 👍
I think this can be starter, another scalar values can follow this if this merged 👍
|
@nikic I know you are probably very busy, but if you have time, would you mind giving this a review? This will help us a lot in Rector. Thanks 🙏 |
|
Hi @nikic , if you find some time, a review on this would be greatly appreciated. Thank you! |
|
What I considered doing here when the raw value was originally introduced was to always use it for printing if the rawValue matches the value (to gracefully handle the case where the value is modified but rawValue is stale). But at this point that would be a non-trivial breaking change, so the opt-in is probably better. I'm not sure handling this as a separate "kind" is best though. Ultimately, the "raw value" is still going to be one of bin/oct/dec/hex. Whether to use the raw value is only a pretty printing choice. So I think I'd prefer this to be a separate option, like you had originally. (There is also the variant of making this a global pretty printer option instead of something per-node.) |
da04165 to
4670e8f
Compare
Hi @nikic thanks for replying i went back to the original approach |
nikic
left a comment
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.
Can you please rebase over e481026 and then document the attribute there?
62fec10 to
465940a
Compare
done |
nikic
left a comment
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.
LGTM
There is a use case where via rector we convert an integer from this format
1050to10_50which represents for example$10.50This was supported by rector but after this pr that is not supported anymore.
If you create a new
Int_(10_50)the printer will print it as1050So i created this pr to add it here if that makes sense
If not, could you please let me know if there is an alternative solution to separate with
_integer values ?