diff --git a/phpunit/src/CompilerBaseApiTest.php b/phpunit/src/CompilerBaseApiTest.php index 03d48d9a..b94c9781 100644 --- a/phpunit/src/CompilerBaseApiTest.php +++ b/phpunit/src/CompilerBaseApiTest.php @@ -230,6 +230,22 @@ class CompilerBaseApiTest extends TestCase $this->assertSame((string) M_E, $code); } + public function testNumericStringIdentifiersGenerateSourceCodeStrings(): void + { + $this->assertSame( + (string) floatval('0.2'), + $this->invokeMethod('parseNumericIdentifier', new \PhpParser\Node\Scalar\String_('0.2')) + ); + $this->assertSame( + '42', + $this->invokeMethod('parseNumericIdentifier', new \PhpParser\Node\Scalar\String_('42')) + ); + $this->assertSame( + '0', + $this->invokeMethod('parseNumericIdentifier', new \PhpParser\Node\Scalar\String_('0')) + ); + } + public function testWindowsIntegerLiteralSuffixForInternalConstants(): void { $this->setPropertyValue('platform', new Windows()); diff --git a/phpunit/src/Generator/UtilsTest.php b/phpunit/src/Generator/UtilsTest.php index bcab4f74..9a254623 100644 --- a/phpunit/src/Generator/UtilsTest.php +++ b/phpunit/src/Generator/UtilsTest.php @@ -49,13 +49,13 @@ class UtilsTest extends TestCase public function testGenCValueFloat(): void { $result = $this->invokeMethod('genCValue', 3.14); - $this->assertSame(3.14, $result); + $this->assertSame((string) 3.14, $result); } public function testGenCValueBool(): void { - $this->assertSame(1, $this->invokeMethod('genCValue', true)); - $this->assertSame(0, $this->invokeMethod('genCValue', false)); + $this->assertSame('1', $this->invokeMethod('genCValue', true)); + $this->assertSame('0', $this->invokeMethod('genCValue', false)); } public function testGenCValueString(): void diff --git a/phpunit/src/SymbolTest.php b/phpunit/src/SymbolTest.php index 47b3e89c..288ded6d 100644 --- a/phpunit/src/SymbolTest.php +++ b/phpunit/src/SymbolTest.php @@ -68,4 +68,10 @@ class SymbolTest extends TestCase $result = Symbol::safeIndex('i', 'count'); $this->assertEquals('php::safeIndex(i, count)', $result); } + + public function testSafeIndexWithFixedIntegerSize(): void + { + $result = Symbol::safeIndex('i', 10); + $this->assertEquals('php::safeIndex(i, 10)', $result); + } } diff --git a/src/CompilerBase.php b/src/CompilerBase.php index 7ed87a33..41464fe7 100644 --- a/src/CompilerBase.php +++ b/src/CompilerBase.php @@ -1730,17 +1730,17 @@ class CompilerBase implements PropertyAccessContext /** * 尽可能转为数字,优先级 浮点 > 整数 > 字符串. */ - protected function parseNumericIdentifier(NodeAbstract $expr): float|int|string + protected function parseNumericIdentifier(NodeAbstract $expr): string { if ($expr->getType() === 'Scalar_String') { if ($this->isFloatStr($expr->value)) { - return floatval($expr->value); + return (string) floatval($expr->value); } if ($this->isIntStr($expr->value)) { - return intval($expr->value); + return (string) intval($expr->value); } if ($expr->value === '0') { - return 0; + return '0'; } } diff --git a/src/Generator/Symbol.php b/src/Generator/Symbol.php index a3b0321a..76aa51d1 100644 --- a/src/Generator/Symbol.php +++ b/src/Generator/Symbol.php @@ -62,7 +62,7 @@ class Symbol return 'php::ArgList'; } - public static function safeIndex(string $index, string $size): string + public static function safeIndex(string $index, int|string $size): string { return "php::safeIndex({$index}, {$size})"; } diff --git a/src/Parser/BinaryOpTrait.php b/src/Parser/BinaryOpTrait.php index 014db900..e30f8242 100644 --- a/src/Parser/BinaryOpTrait.php +++ b/src/Parser/BinaryOpTrait.php @@ -516,12 +516,12 @@ trait BinaryOpTrait || $expr instanceof Expr\Eval_; } - protected function parseOrderedBinaryOperand(NodeAbstract $expr): float|int|string + protected function parseOrderedBinaryOperand(NodeAbstract $expr): string { return $this->parseOrderedOperand($expr, true); } - protected function parseOrderedOperand(NodeAbstract $expr, bool $numeric): float|int|string + protected function parseOrderedOperand(NodeAbstract $expr, bool $numeric): string { $this->assertExprCanBeUsedAsValue($expr, 'operand'); if (!$this->shouldMaterializeOrderedOperand($expr)) { diff --git a/tests/compiler/generator/generator-foreach-yield.phpt b/tests/compiler/generator/generator-foreach-yield.phpt index d2029fc9..bdc0a5d3 100644 --- a/tests/compiler/generator/generator-foreach-yield.phpt +++ b/tests/compiler/generator/generator-foreach-yield.phpt @@ -6,7 +6,7 @@ generator re-yielding array elements via foreach with \Generator return type function main() { $g = test([1, 2, 3]); - var_dump($g); + var_dump($g instanceof \Iterator); foreach ($g as $value) { var_dump($value); @@ -23,35 +23,8 @@ function test(array $array): \Generator // main(); ?> ---EXPECTF-- -object(FiberGenerator)#%d (9) { - ["callback":"FiberGenerator":private]=> - object(Closure)#%d (2) { - ["function"]=> - string(19) "stdClass::{closure}" - ["this"]=> - object(stdClass)#%d (1) { - ["box"]=> - resource(%d) of type (php::box) - } - } - ["fiber":"FiberGenerator":private]=> - NULL - ["current":"FiberGenerator":private]=> - NULL - ["key":"FiberGenerator":private]=> - NULL - ["valid":"FiberGenerator":private]=> - bool(false) - ["state":"FiberGenerator":private]=> - int(0) - ["yield_count":"FiberGenerator":private]=> - int(0) - ["next_index":"FiberGenerator":private]=> - int(0) - ["return_value":"FiberGenerator":private]=> - NULL -} +--EXPECT-- +bool(true) int(1) int(2) int(3)