Fix implicit flot to int casts

This commit is contained in:
Alexander M. Turek
2021-06-06 00:31:05 +02:00
parent b8026c8907
commit de9cd76d7c
2 changed files with 12 additions and 5 deletions
+2 -2
View File
@@ -349,7 +349,7 @@ function twig_cycle($values, $position)
function twig_random(Environment $env, $values = null, $max = null) function twig_random(Environment $env, $values = null, $max = null)
{ {
if (null === $values) { if (null === $values) {
return null === $max ? mt_rand() : mt_rand(0, $max); return null === $max ? mt_rand() : mt_rand(0, (int) $max);
} }
if (\is_int($values) || \is_float($values)) { if (\is_int($values) || \is_float($values)) {
@@ -366,7 +366,7 @@ function twig_random(Environment $env, $values = null, $max = null)
$max = $max; $max = $max;
} }
return mt_rand($min, $max); return mt_rand((int) $min, (int) $max);
} }
if (\is_string($values)) { if (\is_string($values)) {
+10 -3
View File
@@ -11,6 +11,7 @@ namespace Twig\Tests;
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use PHPUnit\Framework\TestCase;
use Twig\Environment; use Twig\Environment;
use Twig\Error\RuntimeError; use Twig\Error\RuntimeError;
use Twig\Extension\SandboxExtension; use Twig\Extension\SandboxExtension;
@@ -23,7 +24,7 @@ use Twig\Sandbox\SecurityError;
use Twig\Sandbox\SecurityPolicy; use Twig\Sandbox\SecurityPolicy;
use Twig\Template; use Twig\Template;
class TemplateTest extends \PHPUnit\Framework\TestCase class TemplateTest extends TestCase
{ {
public function testDisplayBlocksAcceptTemplateOnlyAsBlocks() public function testDisplayBlocksAcceptTemplateOnlyAsBlocks()
{ {
@@ -238,9 +239,15 @@ class TemplateTest extends \PHPUnit\Framework\TestCase
$this->assertSame('Zero', $array[false]); $this->assertSame('Zero', $array[false]);
$this->assertSame('One', $array[true]); $this->assertSame('One', $array[true]);
$this->assertSame('One', $array[1.5]); if (\PHP_VERSION_ID < 80100) {
// This line will trigger a deprecation warning on PHP 8.1.
$this->assertSame('One', $array[1.5]);
}
$this->assertSame('One', $array['1']); $this->assertSame('One', $array['1']);
$this->assertSame('MinusOne', $array[-1.5]); if (\PHP_VERSION_ID < 80100) {
// This line will trigger a deprecation warning on PHP 8.1.
$this->assertSame('MinusOne', $array[-1.5]);
}
$this->assertSame('FloatButString', $array['1.5']); $this->assertSame('FloatButString', $array['1.5']);
$this->assertSame('IntegerButStringWithLeadingZeros', $array['01']); $this->assertSame('IntegerButStringWithLeadingZeros', $array['01']);
$this->assertSame('EmptyString', $array[null]); $this->assertSame('EmptyString', $array[null]);