fix(optimizer): handle internal class literals in get_parent_class (#44)

master
Echo 1 day ago committed by GitHub
parent 10ecb94470
commit 014d5d55cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 13
      phpunit/code/get-parent-class-internal-literal.php
  2. 34
      phpunit/src/GetParentClassOptimizerTest.php
  3. 2
      src/Optimizer/FuncCallOptimizer.php
  4. 4
      tests/std/core/006_get_parent_class.phpt

@ -0,0 +1,13 @@
<?php
/**
* This file is part of TypePHP(AOT).
*
* @link https://www.swoole.com/aot/
* @contact service@swoole.com
*/
function getParentClassInternalLiteral(): void
{
var_dump(get_parent_class('RuntimeException'));
var_dump(get_parent_class('Exception'));
}

@ -0,0 +1,34 @@
<?php
/**
* This file is part of TypePHP(AOT).
*
* @link https://www.swoole.com/aot/
* @contact service@swoole.com
*/
namespace TypePhp\Tests;
use PHPUnit\Framework\TestCase;
use TypePhp\CompilerTest;
/**
* @internal
* @coversNothing
*/
final class GetParentClassOptimizerTest extends TestCase
{
public function testInternalClassLiteralFallsBackToRuntimeLookup(): void
{
global $translator;
$compiler = CompilerTest::create(TYPEPHP_ROOT_PATH);
$translator = $compiler;
$source = TYPEPHP_ROOT_PATH . '/phpunit/code/get-parent-class-internal-literal.php';
$compiler->addFiles([$source]);
$compiler->prepareFile($source);
$cpp = file_get_contents($compiler->convertFile($source));
self::assertIsString($cpp);
self::assertSame(2, substr_count($cpp, 'php::fn::get_parent_class('));
}
}

@ -965,7 +965,7 @@ trait FuncCallOptimizer
);
}
if ($this->isScalarString($arg)) {
$cls = $this->getClass($arg->value);
$cls = $this->getClassDef($arg->value);
if ($cls && $cls->extends) return $this->getLiteralString($cls->extends);
if ($cls && !$cls->extends) return 'false';
}

@ -16,6 +16,8 @@ function main() {
echo get_parent_class(Child::class) === "Base" ? "ok-cls\n" : "fail-cls\n";
echo get_parent_class($noParent) === false ? "ok-obj-false\n" : "fail-obj-false\n";
echo get_parent_class(NoParent::class) === false ? "ok-cls-false\n" : "fail-cls-false\n";
echo get_parent_class('RuntimeException') === "Exception" ? "ok-internal-cls\n" : "fail-internal-cls\n";
echo get_parent_class('Exception') === false ? "ok-internal-cls-false\n" : "fail-internal-cls-false\n";
echo "done\n";
}
@ -26,4 +28,6 @@ ok-obj
ok-cls
ok-obj-false
ok-cls-false
ok-internal-cls
ok-internal-cls-false
done

Loading…
Cancel
Save