diff --git a/phpunit/bootstrap.php b/phpunit/bootstrap.php index c60ea94c..f56426c8 100644 --- a/phpunit/bootstrap.php +++ b/phpunit/bootstrap.php @@ -1,4 +1,25 @@ addFiles([$testFile]); + $compiler->prepare($testFile); + $compiler->convert($testFile); + } catch (TestError $exception) { + $this->assertStringContainsString($expected, $exception->getMessage()); + return; + } + $this->fail(); + } +} \ No newline at end of file diff --git a/phpunit/code/re-assign.php b/phpunit/code/re-assign.php new file mode 100644 index 00000000..b957e4dd --- /dev/null +++ b/phpunit/code/re-assign.php @@ -0,0 +1,6 @@ +exec('Cannot re-assign variable', 're-assign.php'); + } +} diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index f0152798..48f9544b 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1368,10 +1368,18 @@ class CompilerBase extends \PhpAot\Core\Translator return $var . ' = ' . $this->convertExprFromType($type, $expr); } } + } elseif ($this->isVarExpr($right)) { + $rightVar = $this->parseIdentifier($right); + if ($this->hasVar($rightVar) and $this->isTypedObject($rightVar)) { + $type = self::TYPE_OBJECT; + $this->addObject($var, $this->getObjectType($rightVar)); + } } if (!$this->hasVar($var)) { $this->addLocalVar($var, $type); + } else if ($this->getVarType($var) != $type) { + $this->fatalError($left, "Cannot re-assign variable `\${$var}` from " . $this->getVarType($var) . " to " . $type); } } elseif ($this->isPropertyFetch($left) and !$left->getAttribute('nativeProperty')) { return $this->parseAssignPropertyFetch($left, $right);