Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions phpunit/code/get-parent-class-internal-literal.php
Original file line number Diff line number Diff line change
@@ -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'));
}
34 changes: 34 additions & 0 deletions phpunit/src/GetParentClassOptimizerTest.php
Original file line number Diff line number Diff line change
@@ -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('));
}
}
2 changes: 1 addition & 1 deletion src/Optimizer/FuncCallOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ protected function genGetParentClass(string $n, Node\Expr\FuncCall $e, array $c)
);
}
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';
}
Expand Down
4 changes: 4 additions & 0 deletions tests/std/core/006_get_parent_class.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand All @@ -26,4 +28,6 @@ ok-obj
ok-cls
ok-obj-false
ok-cls-false
ok-internal-cls
ok-internal-cls-false
done