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
22 changes: 22 additions & 0 deletions phpunit/code/class-exists-class-and-enum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* This file is part of TypePHP(AOT).
*
* @link https://www.swoole.com/aot/
* @contact service@swoole.com
*/

class Real
{
}

enum Suit
{
case Hearts;
}

function main(): void
{
var_dump(class_exists('Real'));
var_dump(class_exists('Suit'));
}
16 changes: 16 additions & 0 deletions phpunit/code/class-exists-trait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* This file is part of TypePHP(AOT).
*
* @link https://www.swoole.com/aot/
* @contact service@swoole.com
*/

trait Helper
{
}

function main(): void
{
var_dump(class_exists('Helper'));
}
50 changes: 50 additions & 0 deletions phpunit/src/ClassExistsTraitFoldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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
*/
class ClassExistsTraitFoldTest extends TestCase
{
public function testTraitNameFoldsToFalse(): void
{
$cpp = $this->compileToCpp('class-exists-trait.php');

// The trait is known at compile time, so the call is still folded -
// just to the answer PHP gives, which is false for a trait.
self::assertStringNotContainsString('php::fn::class_exists(', $cpp);
self::assertStringContainsString('= false;', $cpp);
}

public function testClassAndEnumNamesStillFoldToTrue(): void
{
$cpp = $this->compileToCpp('class-exists-class-and-enum.php');

self::assertStringNotContainsString('php::fn::class_exists(', $cpp);
self::assertStringNotContainsString('= false;', $cpp);
}

private function compileToCpp(string $file): string
{
global $translator;

$compiler = CompilerTest::create(TYPEPHP_ROOT_PATH);
$translator = $compiler;
$source = TYPEPHP_ROOT_PATH . '/phpunit/code/' . $file;
$compiler->addFiles([$source]);
$compiler->prepareFile($source);

return file_get_contents($compiler->convertFile($source));
}
}
5 changes: 5 additions & 0 deletions src/Optimizer/FuncCallOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,11 @@ protected function doFoldKnownClass(Node\Expr\FuncCall $expr): string|false
if (!$this->isScalarString($cn) || !$this->hasClass($cn->value)) {
return false;
}
// The class table also carries traits, but a trait is not a class to
// class_exists(): PHP answers false for it and true for an enum.
if ($this->getClassDef($cn->value)?->trait !== null) {
return 'false';
}
return $this->isNativeObjectClass($cn->value) ? 'false' : 'true';
}

Expand Down
8 changes: 8 additions & 0 deletions tests/compiler/stdlib/class_exists.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ function main() {
var_dump(class_exists("NonexistentClass"));
var_dump(class_exists("NonexistentClass", false));

// A trait is not a class. The answer must not depend on whether the name
// is a literal the compiler can resolve at compile time.
var_dump(class_exists("MyTrait"));
$traitName = "MyTrait";
var_dump(class_exists($traitName));

// interface_exists
var_dump(interface_exists("MyInterface"));
var_dump(interface_exists("NonexistentInterface"));
Expand All @@ -38,6 +44,8 @@ bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
bool(false)
bool(false)
Expand Down