-
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?
| $kind = $node->getAttribute('kind', Scalar\Int_::KIND_DEC); | ||
| if (Scalar\Int_::KIND_DEC === $kind) { | ||
| return (string) $node->value; | ||
| return $node->getAttribute('shouldPrintRawValue') |
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.
Would be interesting to see how much slower this additional calls will be when pretty-printing a reasonable big code file (especially for those not opt-in into the feature)
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.
Any idea how to prevent the slowdown?
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.
how about add something like Kind::RAW_VALUE ?
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.
i used the raw value approach
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.
@staabm Thanks for the review, would you mind having another look at the updated PR? Would be great to have your approval too. Thanks 🙏
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 👍
| */ | ||
| public static function fromString(string $str, array $attributes = [], bool $allowInvalidOctal = false): Int_ { | ||
| if(isset($attributes['kind']) && $attributes['kind'] === self::KIND_RAW_VALUE){ | ||
| return new Int_((int) $str, $attributes); |
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.
I think rawValue attribute need to be added as well here, ensure rawValue is added as well before returned.
| return new Int_((int) $str, $attributes); | |
| $attributes['rawValue'] = $str; | |
| return new Int_((int) $str, $attributes); |
so the value is always there to avoid changed on reprinting later.
if needed, move $attributes['rawValue'] = $str; to early before the if.
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.
$str is not the raw value
if you override the raw value with $str then this new change will do nothing
the goal is to be able to set the raw value yourself and print that instead of $str
$attributes['rawValue'] = $str;
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.
okay 👍 let's see @nikic opinion about this or if there is better way for this :)
|
@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 🙏 |
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 ?