Skip to content

Commit 9db809b

Browse files
committed
fix(optimizer): handle internal class literals in get_parent_class
1 parent 10ecb94 commit 9db809b

4 files changed

Lines changed: 52 additions & 1 deletion

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* This file is part of TypePHP(AOT).
4+
*
5+
* @link https://www.swoole.com/aot/
6+
* @contact service@swoole.com
7+
*/
8+
9+
function getParentClassInternalLiteral(): void
10+
{
11+
var_dump(get_parent_class('RuntimeException'));
12+
var_dump(get_parent_class('Exception'));
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* This file is part of TypePHP(AOT).
4+
*
5+
* @link https://www.swoole.com/aot/
6+
* @contact service@swoole.com
7+
*/
8+
9+
namespace TypePhp\Tests;
10+
11+
use PHPUnit\Framework\TestCase;
12+
use TypePhp\CompilerTest;
13+
14+
/**
15+
* @internal
16+
* @coversNothing
17+
*/
18+
final class GetParentClassOptimizerTest extends TestCase
19+
{
20+
public function testInternalClassLiteralFallsBackToRuntimeLookup(): void
21+
{
22+
global $translator;
23+
24+
$compiler = CompilerTest::create(TYPEPHP_ROOT_PATH);
25+
$translator = $compiler;
26+
$source = TYPEPHP_ROOT_PATH . '/phpunit/code/get-parent-class-internal-literal.php';
27+
$compiler->addFiles([$source]);
28+
$compiler->prepareFile($source);
29+
$cpp = file_get_contents($compiler->convertFile($source));
30+
31+
self::assertIsString($cpp);
32+
self::assertSame(2, substr_count($cpp, 'php::fn::get_parent_class('));
33+
}
34+
}

src/Optimizer/FuncCallOptimizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ protected function genGetParentClass(string $n, Node\Expr\FuncCall $e, array $c)
965965
);
966966
}
967967
if ($this->isScalarString($arg)) {
968-
$cls = $this->getClass($arg->value);
968+
$cls = $this->getClassDef($arg->value);
969969
if ($cls && $cls->extends) return $this->getLiteralString($cls->extends);
970970
if ($cls && !$cls->extends) return 'false';
971971
}

tests/std/core/006_get_parent_class.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ function main() {
1616
echo get_parent_class(Child::class) === "Base" ? "ok-cls\n" : "fail-cls\n";
1717
echo get_parent_class($noParent) === false ? "ok-obj-false\n" : "fail-obj-false\n";
1818
echo get_parent_class(NoParent::class) === false ? "ok-cls-false\n" : "fail-cls-false\n";
19+
echo get_parent_class('RuntimeException') === "Exception" ? "ok-internal-cls\n" : "fail-internal-cls\n";
20+
echo get_parent_class('Exception') === false ? "ok-internal-cls-false\n" : "fail-internal-cls-false\n";
1921

2022
echo "done\n";
2123
}
@@ -26,4 +28,6 @@ ok-obj
2628
ok-cls
2729
ok-obj-false
2830
ok-cls-false
31+
ok-internal-cls
32+
ok-internal-cls-false
2933
done

0 commit comments

Comments
 (0)