Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,16 +971,18 @@ function twig_compare($a, $b)
if (is_nan($a)) {
return 1;
}
$b = trim($b);
if (!is_numeric($b)) {
return (string) $a <=> $b;
}

return (float) $a <=> $b;
return $a <=> (float) $b;
}
if (\is_float($b) && \is_string($a)) {
if (\is_string($a) && \is_float($b)) {
if (is_nan($b)) {
return 1;
}
$a = trim($a);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this trimming cause issue when entering this !is_numeric($a) and comparing strings ? Shouldn't we compare the original string then ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof: I think you are right and there was already the problem for int <=> string above (see https://3v4l.org/RIUI5). Open a new issue/PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof: See #3352

if (!is_numeric($a)) {
return $a <=> (string) $b;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/Extension/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ public function provideCompareCases()
[-1, 42, 'abc42'],
[-1, 0, 'abc42'],

[0, 42.0, ' 42.0'],
[0, 42.0, '42.0 '],
[-1, 42.0, '42.0abc'],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHP 8 returns 0

@guilliamxavier guilliamxavier May 30, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabpot: But that's unrelated to this PR. Indeed, php/php-src#3886 hasn't been merged (yet), so several existing tests currently return a different result (https://3v4l.org/dX6Q9/rfc#output).

[-1, 42.0, 'abc42.0'],
[-1, 0.0, 'abc42.0'],
Comment thread
guilliamxavier marked this conversation as resolved.

[0, INF, 'INF'],
[0, -INF, '-INF'],
[0, INF, '1e1000'],
Expand Down