refactor(parser): 重构赋值操作符解析逻辑并调整测试文件结构

- 调整 AssignOpTrait.php 中变量解析顺序,优化代码执行流程
- 将 inAssignExpr 上下文切换移到属性写入检查之后
- 移除比较操作符测试文件中的太空船操作符测试用例
- 创建独立的太空船操作符测试文件 spaceship.phpt
- 重新组织测试用例结构以提高可维护性
pull/11/head
韩天峰 2 months ago
parent 1e774af718
commit 8df3e8bfef
  1. 18
      src/Php/Parser/AssignOpTrait.php
  2. 13
      tests/aot/operator/comparison_operators.phpt
  3. 18
      tests/aot/operator/spaceship.phpt

@ -142,20 +142,20 @@ trait AssignOpTrait
return $this->parseAssignToList($left, $right); return $this->parseAssignToList($left, $right);
} }
$oriInAssignExpr = $this->context->inAssignExpr;
$this->context->inAssignExpr = true;
$var = $this->parseIdentifier($left);
$this->context->inAssignExpr = $oriInAssignExpr;
$propertyWriteTarget = $this->preparePropertyWriteTarget($left); $propertyWriteTarget = $this->preparePropertyWriteTarget($left);
if ($var === 'this_') {
$this->fatalError($left, 'Cannot re-assign $this');
}
$finalVarType = $type = $this->detectTypeOfExpr($right); $finalVarType = $type = $this->detectTypeOfExpr($right);
if ($type === self::TYPE_VOID) { if ($type === self::TYPE_VOID) {
$finalVarType = $type = self::TYPE_VAR; $finalVarType = $type = self::TYPE_VAR;
} }
if ($this->isVarExpr($left)) { if ($this->isVarExpr($left)) {
$oriInAssignExpr = $this->context->inAssignExpr;
$this->context->inAssignExpr = true;
$var = $this->parseIdentifier($left);
$this->context->inAssignExpr = $oriInAssignExpr;
if ($var === 'this_') {
$this->fatalError($left, 'Cannot re-assign $this');
}
if ($this->isStdContainer($var)) { if ($this->isStdContainer($var)) {
$copyAssign = $this->parseStdContainerCopyAssign($var, $right); $copyAssign = $this->parseStdContainerCopyAssign($var, $right);
if ($copyAssign !== null) { if ($copyAssign !== null) {
@ -274,6 +274,10 @@ trait AssignOpTrait
$this->assertCanAssignPropertyWrite($propertyWriteTarget, $right); $this->assertCanAssignPropertyWrite($propertyWriteTarget, $right);
} }
$oriInAssignExpr = $this->context->inAssignExpr;
$this->context->inAssignExpr = true;
$var = $this->parseIdentifier($left);
$this->context->inAssignExpr = $oriInAssignExpr;
$rightExpr = $this->parseAssignRightExpr($right); $rightExpr = $this->parseAssignRightExpr($right);
if ($propertyWriteTarget !== null) { if ($propertyWriteTarget !== null) {
$rightExpr = $this->wrapPropertyWriteTypeCheck($propertyWriteTarget, $right, $rightExpr); $rightExpr = $this->wrapPropertyWriteTypeCheck($propertyWriteTarget, $right, $rightExpr);

@ -47,16 +47,6 @@ $int_num = 10;
var_dump($str_num == $int_num); // true (loose comparison) var_dump($str_num == $int_num); // true (loose comparison)
var_dump($str_num === $int_num); // false (strict comparison) var_dump($str_num === $int_num); // false (strict comparison)
// Test spaceship operator (PHP 7+)
$spaceship1 = 5 <=> 10; // -1
var_dump($spaceship1);
$spaceship2 = 10 <=> 10; // 0
var_dump($spaceship2);
$spaceship3 = 15 <=> 10; // 1
var_dump($spaceship3);
// Test comparisons with strings // Test comparisons with strings
$str1 = "apple"; $str1 = "apple";
$str2 = "banana"; $str2 = "banana";
@ -149,9 +139,6 @@ bool(true)
bool(false) bool(false)
bool(true) bool(true)
bool(false) bool(false)
int(-1)
int(0)
int(1)
bool(true) bool(true)
bool(true) bool(true)
bool(false) bool(false)

@ -0,0 +1,18 @@
--TEST--
assign compare
--FILE--
<?php
// Test spaceship operator (PHP 7+)
$spaceship1 = 5 <=> 10; // -1
var_dump($spaceship1);
$spaceship2 = 10 <=> 10; // 0
var_dump($spaceship2);
$spaceship3 = 15 <=> 10; // 1
var_dump($spaceship3);
?>
--EXPECT--
int(-1)
int(0)
int(1)
Loading…
Cancel
Save