diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 11fdaf94..2f56dfdd 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -1,9 +1,9 @@ analyze( $paths, - $includePhpUnit ? ROOT_PATH . '/phpunit/src' : null, - $includePhpUnit ? ROOT_PATH . '/phpunit/code' : null, + $includePhpUnit ? TYPEPHP_ROOT_PATH . '/phpunit/src' : null, + $includePhpUnit ? TYPEPHP_ROOT_PATH . '/phpunit/code' : null, ); } catch (Throwable $error) { fwrite(STDERR, 'Coverage analysis failed: ' . $error->getMessage() . PHP_EOL); @@ -91,7 +91,7 @@ $rendered = match ($format) { if ($output === null) { echo $rendered; } else { - $outputPath = isAbsolutePath($output) ? $output : ROOT_PATH . DIRECTORY_SEPARATOR . $output; + $outputPath = isAbsolutePath($output) ? $output : TYPEPHP_ROOT_PATH . DIRECTORY_SEPARATOR . $output; $directory = dirname($outputPath); if (!is_dir($directory) && !mkdir($directory, 0777, true) && !is_dir($directory)) { fwrite(STDERR, 'Unable to create output directory: ' . $directory . PHP_EOL); @@ -101,7 +101,7 @@ if ($output === null) { fwrite(STDERR, 'Unable to write report: ' . $outputPath . PHP_EOL); exit(1); } - echo 'Wrote ', $format, ' coverage report: ', relativePath(ROOT_PATH, $outputPath), PHP_EOL; + echo 'Wrote ', $format, ' coverage report: ', relativePath(TYPEPHP_ROOT_PATH, $outputPath), PHP_EOL; } if ($strict && ($report['parse_errors'] !== [] || $report['unresolved_phpunit_fixtures'] !== [])) { diff --git a/bin/bootstrap.php b/bin/bootstrap.php index d412fdb3..8d897dda 100644 --- a/bin/bootstrap.php +++ b/bin/bootstrap.php @@ -1,6 +1,17 @@ =8.4 <8.6", - "nikic/php-parser": "5.6.1", + "composer-runtime-api": "^2.2", + "nikic/php-parser": "^5.6", "league/climate": "^3.10", "marcj/topsort": "^2.0", "symfony/var-dumper": "^8.0", "symfony/yaml": "^8.0", "swoole/phpx": "~2.6.4", - "ajaxray/ansikit": "^0.3.1" + "ajaxray/ansikit": "^0.3", + "ext-dom": "*" }, "require-dev": { "phpunit/phpunit": "^10.4", diff --git a/composer.lock b/composer.lock index c450098a..c8ff010e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3262f7344d1d75ad17fdbf19f9f8896d", + "content-hash": "2d4aeff65ded28c1339be3c4f55f55eb", "packages": [ { "name": "ajaxray/ansikit", @@ -4925,7 +4925,8 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=8.4 <8.6" + "php": ">=8.4 <8.6", + "composer-runtime-api": "^2.2" }, "platform-dev": {}, "plugin-api-version": "2.9.0" diff --git a/phpunit/BashCompletionTest.php b/phpunit/BashCompletionTest.php index f852ffdf..550cf758 100644 --- a/phpunit/BashCompletionTest.php +++ b/phpunit/BashCompletionTest.php @@ -12,7 +12,7 @@ final class BashCompletionTest extends TestCase { self::assertSame( BashCompletion::render(), - file_get_contents(ROOT_PATH . '/completions/tpc.bash'), + file_get_contents(TYPEPHP_ROOT_PATH . '/completions/tpc.bash'), ); } @@ -46,7 +46,7 @@ final class BashCompletionTest extends TestCase public function testBashScriptSyntaxAndRepresentativeCompletions(): void { - $script = ROOT_PATH . '/completions/tpc.bash'; + $script = TYPEPHP_ROOT_PATH . '/completions/tpc.bash'; self::assertSame('', $this->runBash("bash -n " . escapeshellarg($script))); self::assertStringNotContainsString('mapfile', file_get_contents($script)); self::assertSame( diff --git a/phpunit/BinaryEntrypointTest.php b/phpunit/BinaryEntrypointTest.php new file mode 100644 index 00000000..2d2ade82 --- /dev/null +++ b/phpunit/BinaryEntrypointTest.php @@ -0,0 +1,188 @@ +runCompiler(TYPEPHP_ROOT_PATH . '/bin/tpc.php', $temporaryRoot); + + self::assertSame(0, $result['status'], $result['stderr']); + self::assertStringContainsString('TypePHP Compiler (AOT)', $result['stdout']); + } finally { + $this->removeDirectory($temporaryRoot); + } + } + + public function testComposerVendorEntrypointUsesProjectAutoloader(): void + { + $temporaryRoot = sys_get_temp_dir() . '/typephp-composer-bin-' . bin2hex(random_bytes(6)); + $packageRoot = $temporaryRoot . '/vendor/swoole/typephp'; + + try { + $proxy = $this->createComposerEntrypoint($temporaryRoot, $packageRoot); + self::assertDirectoryDoesNotExist($packageRoot . '/vendor'); + + $result = $this->runCompiler($proxy, $temporaryRoot); + + self::assertSame(0, $result['status'], $result['stderr']); + self::assertStringContainsString('TypePHP Compiler (AOT)', $result['stdout']); + } finally { + $this->removeDirectory($temporaryRoot); + } + } + + public function testComposerVendorEntrypointDryCompilesProject(): void + { + if (!$this->hasPhpxLibrary()) { + self::markTestSkipped('A built PHPX library is required for the Zend PHP compiler integration test'); + } + + $temporaryRoot = sys_get_temp_dir() . '/typephp-composer-project-' . bin2hex(random_bytes(6)); + $packageRoot = $temporaryRoot . '/vendor/swoole/typephp'; + + try { + $proxy = $this->createComposerEntrypoint($temporaryRoot, $packageRoot); + self::assertNotFalse(file_put_contents( + $temporaryRoot . '/main.php', + "runCompiler( + $proxy, + $temporaryRoot, + ['project.yml', '--dry', '--no-color'], + ); + + self::assertSame(0, $result['status'], $result['stderr']); + self::assertStringContainsString('Dry run completed:', $result['stdout']); + } finally { + $this->removeDirectory($temporaryRoot); + } + } + + /** @return array{status: int, stdout: string, stderr: string} */ + private function runCompiler( + string $entrypoint, + string $workingDirectory, + array $arguments = ['--version', '--no-color'], + ): array { + $process = proc_open([PHP_BINARY, $entrypoint, ...$arguments], [ + 0 => ['pipe', 'r'], + 1 => ['pipe', 'w'], + 2 => ['pipe', 'w'], + ], $pipes, $workingDirectory, null, ['suppress_errors' => true]); + self::assertIsResource($process); + fclose($pipes[0]); + $stdout = stream_get_contents($pipes[1]); + fclose($pipes[1]); + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[2]); + + return [ + 'status' => proc_close($process), + 'stdout' => (string) $stdout, + 'stderr' => (string) $stderr, + ]; + } + + private function createComposerEntrypoint(string $temporaryRoot, string $packageRoot): string + { + $this->copyPackageEntrypoint($packageRoot); + $proxy = $temporaryRoot . '/vendor/bin/tpc.php'; + $proxySource = sprintf( + "isDir() ? rmdir($item->getPathname()) : unlink($item->getPathname()); + } + rmdir($directory); + } +} diff --git a/phpunit/bootstrap.php b/phpunit/bootstrap.php index ba6e7839..1eeae725 100644 --- a/phpunit/bootstrap.php +++ b/phpunit/bootstrap.php @@ -13,7 +13,7 @@ class BaseTest extends TestCase protected function compile(string $file): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/code/' . $file; $compiler->addFiles([$testFile]); diff --git a/phpunit/src/ArrayDefTest.php b/phpunit/src/ArrayDefTest.php index 50f60aca..6165871a 100644 --- a/phpunit/src/ArrayDefTest.php +++ b/phpunit/src/ArrayDefTest.php @@ -23,9 +23,9 @@ final class ArrayDefTest extends \BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/array-def-inclusive-upper-bound.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/array-def-inclusive-upper-bound.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $generated = $compiler->convertFile($source); diff --git a/phpunit/src/Backend/BackendTest.php b/phpunit/src/Backend/BackendTest.php index 8e69deab..882cc304 100644 --- a/phpunit/src/Backend/BackendTest.php +++ b/phpunit/src/Backend/BackendTest.php @@ -252,7 +252,7 @@ class BackendTest extends TestCase $dir = $this->createTemporaryDirectory('backend_link_failure'); $target = $dir . '/app'; $backend = new Gcc(new Linux(), 'false'); - $compiler = new class(ROOT_PATH, $target, $backend) extends CompilerTest { + $compiler = new class(TYPEPHP_ROOT_PATH, $target, $backend) extends CompilerTest { public function __construct( string $rootPath, private readonly string $testTarget, diff --git a/phpunit/src/BigNumericValidationTest.php b/phpunit/src/BigNumericValidationTest.php index 95841307..3615c8f4 100644 --- a/phpunit/src/BigNumericValidationTest.php +++ b/phpunit/src/BigNumericValidationTest.php @@ -5,7 +5,7 @@ class BigNumericValidationTest extends \BaseTest public function testDecimalIntegerOperandDoesNotConvertThroughString(): void { global $translator; - $compiler = \TypePhp\CompilerTest::create(ROOT_PATH); + $compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/big-numeric/decimal-int-operand.php'; $compiler->addFiles([$testFile]); diff --git a/phpunit/src/BranchPredictionTest.php b/phpunit/src/BranchPredictionTest.php index 011b28d2..ea692de9 100644 --- a/phpunit/src/BranchPredictionTest.php +++ b/phpunit/src/BranchPredictionTest.php @@ -8,7 +8,7 @@ final class BranchPredictionTest extends TestCase public function testGeneratesExpectedAndUnexpectedMacros(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $file = __DIR__ . '/../code/branch-prediction.php'; $compiler->addFiles([$file]); diff --git a/phpunit/src/ClassConstantCodegenTest.php b/phpunit/src/ClassConstantCodegenTest.php index 12529427..93762017 100644 --- a/phpunit/src/ClassConstantCodegenTest.php +++ b/phpunit/src/ClassConstantCodegenTest.php @@ -20,9 +20,9 @@ final class ClassConstantCodegenTest extends \BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/class-constant-codegen.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/class-constant-codegen.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $generated = $compiler->convertFile($source); diff --git a/phpunit/src/ClassMethodOverrideTest.php b/phpunit/src/ClassMethodOverrideTest.php index a0d55261..ccf75ce2 100644 --- a/phpunit/src/ClassMethodOverrideTest.php +++ b/phpunit/src/ClassMethodOverrideTest.php @@ -6,7 +6,7 @@ class ClassMethodOverrideTest extends \BaseTest { public function testChildBeforeParentOverride(): void { - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $testFile = __DIR__ . '/../code/class-method-override-order.php'; $compiler->addFiles([$testFile]); $compiler->prepareFile($testFile); @@ -28,7 +28,7 @@ class ClassMethodOverrideTest extends \BaseTest public function testNamespacedOverrideKeysUseFullClassName(): void { - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $testFile = __DIR__ . '/../code/class-method-override-namespace.php'; $compiler->addFiles([$testFile]); $compiler->prepareFile($testFile); diff --git a/phpunit/src/ClassTest.php b/phpunit/src/ClassTest.php index 5ddef047..ac99d631 100644 --- a/phpunit/src/ClassTest.php +++ b/phpunit/src/ClassTest.php @@ -256,7 +256,7 @@ class ClassTest extends \BaseTest public function testPrinterConvertsNonStringFieldsAndArrayablePreservesValues(): void { global $translator; - $compiler = \TypePhp\CompilerTest::create(ROOT_PATH); + $compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/printer-arrayable-field-types.php'; $compiler->addFiles([$testFile]); @@ -298,7 +298,7 @@ class ClassTest extends \BaseTest public function testNotNullWarnsForExplicitlyNullableParameters(): void { global $translator; - $compiler = \TypePhp\CompilerTest::create(ROOT_PATH); + $compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $reporter = new class implements \TypePhp\Diagnostics\DiagnosticReporter { /** @var list */ @@ -433,7 +433,7 @@ class ClassTest extends \BaseTest public function testMethodsForUsesKeywordClassHierarchyAndObjectFallbackPriority(): void { global $translator; - $compiler = \TypePhp\CompilerTest::create(ROOT_PATH); + $compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/methods-for-inheritance.php'; $compiler->addFiles([$testFile]); @@ -656,7 +656,7 @@ class ClassTest extends \BaseTest public function testConstructorCallsParentConstructorWithoutRequiredArguments(): void { global $translator; - $compiler = \TypePhp\CompilerTest::create(ROOT_PATH); + $compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/constructor-parent-optional.php'; $compiler->addFiles([$testFile]); @@ -782,7 +782,7 @@ class ClassTest extends \BaseTest public function testSelfCanBePartOfUnionType() { global $translator; - $compiler = \TypePhp\CompilerTest::create(ROOT_PATH); + $compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/union_type_self_allowed.php'; $compiler->addFiles([$testFile]); @@ -794,7 +794,7 @@ class ClassTest extends \BaseTest public function testParentCanBePartOfUnionType() { global $translator; - $compiler = \TypePhp\CompilerTest::create(ROOT_PATH); + $compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/union_type_parent_allowed.php'; $compiler->addFiles([$testFile]); diff --git a/phpunit/src/CloneWithCodegenTest.php b/phpunit/src/CloneWithCodegenTest.php index b8955f02..74b2cc1d 100644 --- a/phpunit/src/CloneWithCodegenTest.php +++ b/phpunit/src/CloneWithCodegenTest.php @@ -38,9 +38,9 @@ final class CloneWithCodegenTest extends BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/clone-with-codegen.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/clone-with-codegen.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $generated = $compiler->convertFile($source); diff --git a/phpunit/src/ClosureTest.php b/phpunit/src/ClosureTest.php index 58ce6214..da013322 100644 --- a/phpunit/src/ClosureTest.php +++ b/phpunit/src/ClosureTest.php @@ -8,8 +8,8 @@ class ClosureTest extends \BaseTest { global $translator; - $testFile = ROOT_PATH . '/phpunit/code/closure/use-reference-capture.php'; - $compiler = CompilerTest::create(ROOT_PATH); + $testFile = TYPEPHP_ROOT_PATH . '/phpunit/code/closure/use-reference-capture.php'; + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $compiler->addFiles([$testFile]); $compiler->prepareFile($testFile); diff --git a/phpunit/src/CompactTest.php b/phpunit/src/CompactTest.php index 91d720ed..6b93b33e 100644 --- a/phpunit/src/CompactTest.php +++ b/phpunit/src/CompactTest.php @@ -16,7 +16,7 @@ class CompactTest extends \BaseTest public function testCompactThisInsideClass(): void { $testFile = __DIR__ . '/../code/compact-this-inside-class.php'; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $compiler->addFiles([$testFile]); $compiler->prepareFile($testFile); $compiler->convertFile($testFile); diff --git a/phpunit/src/CompileTimeAttributeRegistryTest.php b/phpunit/src/CompileTimeAttributeRegistryTest.php index 414b47d2..68dccb17 100644 --- a/phpunit/src/CompileTimeAttributeRegistryTest.php +++ b/phpunit/src/CompileTimeAttributeRegistryTest.php @@ -38,7 +38,7 @@ final class CompileTimeAttributeRegistryTest extends TestCase public function testRegistryMatchesPublicCompileTimeAttributeDeclarations(): void { - $source = file_get_contents(ROOT_PATH . '/src/polyfills.php'); + $source = file_get_contents(TYPEPHP_ROOT_PATH . '/src/polyfills.php'); $this->assertNotFalse($source); preg_match_all( '/#\[Attribute\([^\]]+\)\]\s+final readonly class ([A-Za-z_][A-Za-z0-9_]*)/', diff --git a/phpunit/src/CompilerBaseApiTest.php b/phpunit/src/CompilerBaseApiTest.php index 221963e0..d4c9bcce 100644 --- a/phpunit/src/CompilerBaseApiTest.php +++ b/phpunit/src/CompilerBaseApiTest.php @@ -415,14 +415,17 @@ PHP); $internalConstants = $this->getPropertyValue('internalConstants'); $this->assertArrayHasKey('PHP_VERSION', $internalConstants); $this->assertArrayNotHasKey('ROOT_PATH', $internalConstants); + $this->assertArrayNotHasKey('TYPEPHP_ROOT_PATH', $internalConstants); - $code = $this->invokeMethod( - 'parseConstFetch', - new \PhpParser\Node\Expr\ConstFetch(new \PhpParser\Node\Name('ROOT_PATH')) - ); + foreach (['ROOT_PATH', 'TYPEPHP_ROOT_PATH'] as $constantName) { + $code = $this->invokeMethod( + 'parseConstFetch', + new \PhpParser\Node\Expr\ConstFetch(new \PhpParser\Node\Name($constantName)) + ); - $this->assertStringStartsWith('php::constant(', $code); - $this->assertStringNotContainsString(ROOT_PATH, $code); + $this->assertStringStartsWith('php::constant(', $code); + $this->assertStringNotContainsString(TYPEPHP_ROOT_PATH, $code); + } } public function testUnqualifiedRuntimeConstantUsesNamespaceFallback(): void @@ -1214,11 +1217,11 @@ YAML); { global $translator; foreach ([CompilerBase::BUILD_MODE_BIN, CompilerBase::BUILD_MODE_LIB, CompilerBase::BUILD_MODE_EXT] as $mode) { - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $compiler->setBuildMode($mode); - $testFile = ROOT_PATH . '/phpunit/code/compiler_api/extension_clean_maps.php'; + $testFile = TYPEPHP_ROOT_PATH . '/phpunit/code/compiler_api/extension_clean_maps.php'; $compiler->addFiles([$testFile]); $compiler->prepareFile($testFile); $compiler->convertFile($testFile); @@ -1245,14 +1248,14 @@ YAML); public function testGeneratedRuntimeSymbolsUseProjectNamespace(): void { global $translator; - $testFile = ROOT_PATH . '/phpunit/code/compiler_api/extension_clean_maps.php'; + $testFile = TYPEPHP_ROOT_PATH . '/phpunit/code/compiler_api/extension_clean_maps.php'; foreach ([ CompilerBase::BUILD_MODE_EXT => 'isolated_ext', CompilerBase::BUILD_MODE_BIN => 'isolated_bin', CompilerBase::BUILD_MODE_LIB => 'isolated_lib', ] as $mode => $target) { - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $compiler->setBuildMode($mode); $compiler->setTargetName($target); @@ -1303,11 +1306,11 @@ YAML); public function testPersistentSymbolCachesAreLazyAndZtsSafe(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $compiler->setBuildMode(CompilerBase::BUILD_MODE_EXT); - $testFile = ROOT_PATH . '/phpunit/code/compiler_api/persistent_symbol_cache.php'; + $testFile = TYPEPHP_ROOT_PATH . '/phpunit/code/compiler_api/persistent_symbol_cache.php'; $compiler->addFiles([$testFile]); $compiler->prepareFile($testFile); $compiler->convertFile($testFile); @@ -1354,7 +1357,7 @@ YAML); global $translator; $translator = $this->compiler; - $testFile = ROOT_PATH . '/phpunit/code/arrayable.php'; + $testFile = TYPEPHP_ROOT_PATH . '/phpunit/code/arrayable.php'; $this->compiler->addFiles([$testFile]); $this->compiler->prepareFile($testFile); $cppFile = $this->compiler->convertFile($testFile); @@ -1373,7 +1376,7 @@ YAML); $this->setPropertyValue('buildMode', CompilerBase::BUILD_MODE_LIB); $this->compiler->setTargetName('prime2'); - $testFile = ROOT_PATH . '/phpunit/code/compiler_api/default_argument_abi.stub.php'; + $testFile = TYPEPHP_ROOT_PATH . '/phpunit/code/compiler_api/default_argument_abi.stub.php'; $this->compiler->addFiles([$testFile]); $this->compiler->prepareFile($testFile); $this->compiler->convertFile($testFile); @@ -1436,7 +1439,7 @@ YAML); $this->setPropertyValue('buildMode', CompilerBase::BUILD_MODE_LIB); $this->compiler->setTargetName('prime2'); - $testFile = ROOT_PATH . '/phpunit/code/compiler_api/prime2.stub.php'; + $testFile = TYPEPHP_ROOT_PATH . '/phpunit/code/compiler_api/prime2.stub.php'; $this->compiler->addFiles([$testFile]); $this->compiler->prepareFile($testFile); $this->compiler->convertFile($testFile); @@ -1467,9 +1470,9 @@ YAML); $this->compiler->setTargetName('prime2'); $files = [ - ROOT_PATH . '/phpunit/code/compiler_api/library_import_php.php', - ROOT_PATH . '/phpunit/code/compiler_api/library_import_native.stub.php', - ROOT_PATH . '/phpunit/code/compiler_api/library_import_global.php', + TYPEPHP_ROOT_PATH . '/phpunit/code/compiler_api/library_import_php.php', + TYPEPHP_ROOT_PATH . '/phpunit/code/compiler_api/library_import_native.stub.php', + TYPEPHP_ROOT_PATH . '/phpunit/code/compiler_api/library_import_global.php', ]; $this->compiler->addFiles($files); $cppFiles = []; @@ -1728,7 +1731,7 @@ YAML); $this->compiler->setTargetName('namespace_rules'); $stubFile = $this->compiler->genLibraryImportStub([ - ROOT_PATH . '/phpunit/code/compiler_api/library_no_export_namespace.php', + TYPEPHP_ROOT_PATH . '/phpunit/code/compiler_api/library_no_export_namespace.php', ]); $stub = file_get_contents($stubFile); diff --git a/phpunit/src/CompilerPhaseTest.php b/phpunit/src/CompilerPhaseTest.php index e75a338c..56afe6fe 100644 --- a/phpunit/src/CompilerPhaseTest.php +++ b/phpunit/src/CompilerPhaseTest.php @@ -34,7 +34,7 @@ class CompilerPhaseTest extends \PHPUnit\Framework\TestCase { public function testPropertyAccessResolverCannotBeUsedOutsideConvertPhase(): void { - $compiler = CompilerPhaseProbe::createProbe(ROOT_PATH); + $compiler = CompilerPhaseProbe::createProbe(TYPEPHP_ROOT_PATH); $this->expectException(TestError::class); $this->expectExceptionMessage('PropertyAccessResolver can only be used during convert phase'); @@ -44,7 +44,7 @@ class CompilerPhaseTest extends \PHPUnit\Framework\TestCase public function testPropertyAccessResolverCannotBeUsedDuringPreparePhase(): void { - $compiler = CompilerPhaseProbe::createProbe(ROOT_PATH); + $compiler = CompilerPhaseProbe::createProbe(TYPEPHP_ROOT_PATH); $compiler->enterPreparePhase(); $this->expectException(TestError::class); @@ -55,7 +55,7 @@ class CompilerPhaseTest extends \PHPUnit\Framework\TestCase public function testPropertyAccessResolverCanBeUsedDuringConvertPhase(): void { - $compiler = CompilerPhaseProbe::createProbe(ROOT_PATH); + $compiler = CompilerPhaseProbe::createProbe(TYPEPHP_ROOT_PATH); $compiler->enterConvertPhase(); $this->assertTrue($compiler->probePropertyAccessResolver()); diff --git a/phpunit/src/ConstantArithmeticOverflowTest.php b/phpunit/src/ConstantArithmeticOverflowTest.php index a4b7b846..eb931530 100644 --- a/phpunit/src/ConstantArithmeticOverflowTest.php +++ b/phpunit/src/ConstantArithmeticOverflowTest.php @@ -72,7 +72,7 @@ class ConstantArithmeticOverflowTest extends TestCase private function compileWithReporter(): object { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $reporter = new class implements DiagnosticReporter { /** @var list */ @@ -101,7 +101,7 @@ class ConstantArithmeticOverflowTest extends TestCase private function compileNativeFile(string $file): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $compiler->setDiagnosticReporter(new class implements DiagnosticReporter { public function fatal(string $message): never diff --git a/phpunit/src/CountGlobalsTest.php b/phpunit/src/CountGlobalsTest.php index 548b736a..3e9c8487 100644 --- a/phpunit/src/CountGlobalsTest.php +++ b/phpunit/src/CountGlobalsTest.php @@ -23,7 +23,7 @@ class CountGlobalsTest extends \BaseTest { // $GLOBALS['key'] 数组访问形式应正常通过编译 $testFile = __DIR__ . '/../code/globals-array-access.php'; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $compiler->addFiles([$testFile]); $compiler->prepareFile($testFile); $compiler->convertFile($testFile); diff --git a/phpunit/src/DuplicateTest.php b/phpunit/src/DuplicateTest.php index b8326222..ea389d8d 100644 --- a/phpunit/src/DuplicateTest.php +++ b/phpunit/src/DuplicateTest.php @@ -10,7 +10,7 @@ class DuplicateTest extends TestCase private function exec(string $expected, string $file): void { try { - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $testFile = __DIR__ . '/../code/' . $file; $compiler->addFiles([$testFile]); $compiler->prepareFile($testFile); diff --git a/phpunit/src/ForeachIndentTest.php b/phpunit/src/ForeachIndentTest.php index f9b67157..60d1a19c 100644 --- a/phpunit/src/ForeachIndentTest.php +++ b/phpunit/src/ForeachIndentTest.php @@ -10,9 +10,9 @@ class ForeachIndentTest extends TestCase public function testObjectForeachRestoresCompilerIndentation(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $file = ROOT_PATH . '/phpunit/code/object-foreach-indent.php'; + $file = TYPEPHP_ROOT_PATH . '/phpunit/code/object-foreach-indent.php'; $compiler->addFiles([$file]); $compiler->prepareFile($file); $compiler->convertFile($file); diff --git a/phpunit/src/FunctionTest.php b/phpunit/src/FunctionTest.php index 3447c0d1..85f03046 100644 --- a/phpunit/src/FunctionTest.php +++ b/phpunit/src/FunctionTest.php @@ -40,7 +40,7 @@ class FunctionTest extends \BaseTest ): void { global $translator; - $compiler = \TypePhp\CompilerTest::create(ROOT_PATH); + $compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/' . $filename; $compiler->addFiles([$testFile]); diff --git a/phpunit/src/GenStubVersionFlagsTest.php b/phpunit/src/GenStubVersionFlagsTest.php index 658fbeb5..cf868195 100644 --- a/phpunit/src/GenStubVersionFlagsTest.php +++ b/phpunit/src/GenStubVersionFlagsTest.php @@ -1,11 +1,15 @@ setValue($compiler, true); } - $source = ROOT_PATH . '/phpunit/code/generated-code-comments.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/generated-code-comments.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $generated = $compiler->convertFile($source); @@ -28,9 +28,9 @@ final class GeneratedCodeCommentTest extends BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/' . $file; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/' . $file; $compiler->addFiles([$source]); $compiler->prepareFile($source); $generated = $compiler->convertFile($source); diff --git a/phpunit/src/GeneratedCodeIndentationTest.php b/phpunit/src/GeneratedCodeIndentationTest.php index 06a3ad59..f665c003 100644 --- a/phpunit/src/GeneratedCodeIndentationTest.php +++ b/phpunit/src/GeneratedCodeIndentationTest.php @@ -7,9 +7,9 @@ class GeneratedCodeIndentationTest extends \PHPUnit\Framework\TestCase public function testNestedStatementsAndZendWrappersAreConsistentlyIndented(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/generated-code-indentation.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/generated-code-indentation.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $cppFile = $compiler->convertFile($source); diff --git a/phpunit/src/Generator/FiberGeneratorTest.php b/phpunit/src/Generator/FiberGeneratorTest.php index e446c596..2efdbb5c 100644 --- a/phpunit/src/Generator/FiberGeneratorTest.php +++ b/phpunit/src/Generator/FiberGeneratorTest.php @@ -10,7 +10,7 @@ class FiberGeneratorTest extends TestCase { public function testWasiTargetRejectsGeneratorDuringPreparation(): void { - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $reflection = new \ReflectionClass($compiler); $reflection->getProperty('targetPlatform')->setValue($compiler, 'wasm32-wasip2'); $file = __DIR__ . '/../../code/generator-conversion-error.php'; @@ -23,7 +23,7 @@ class FiberGeneratorTest extends TestCase public function testCompilerStateIsRestoredAfterGeneratorConversionError(): void { - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $file = __DIR__ . '/../../code/generator-conversion-error.php'; $compiler->addFiles([$file]); $compiler->prepareFile($file); diff --git a/phpunit/src/HotPathCodegenTest.php b/phpunit/src/HotPathCodegenTest.php index 80217461..cc43372d 100644 --- a/phpunit/src/HotPathCodegenTest.php +++ b/phpunit/src/HotPathCodegenTest.php @@ -61,9 +61,9 @@ final class HotPathCodegenTest extends \BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/hot-path-codegen.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/hot-path-codegen.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $generated = $compiler->convertFile($source); diff --git a/phpunit/src/InheritanceErrorTest.php b/phpunit/src/InheritanceErrorTest.php index 3cd9bc9b..323856be 100644 --- a/phpunit/src/InheritanceErrorTest.php +++ b/phpunit/src/InheritanceErrorTest.php @@ -9,7 +9,7 @@ class InheritanceErrorTest extends TestCase private function exec(string $expected, string $file): void { try { - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $testFile = __DIR__ . '/../code/' . $file; $compiler->addFiles([$testFile]); $compiler->prepareFile($testFile); @@ -24,7 +24,7 @@ class InheritanceErrorTest extends TestCase private function assertCompiles(string $file): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/' . $file; $compiler->addFiles([$testFile]); @@ -357,7 +357,7 @@ class InheritanceErrorTest extends TestCase public function testInterfaceArrayConstantInitializesRuntimeValue() { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/interface_array_constant.php'; $compiler->addFiles([$testFile]); @@ -371,7 +371,7 @@ class InheritanceErrorTest extends TestCase public function testInterfaceArrayConstantPropagatesToImplementingClass() { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/interface_array_constant_implements.php'; $compiler->addFiles([$testFile]); diff --git a/phpunit/src/InterfacePropertyHookTest.php b/phpunit/src/InterfacePropertyHookTest.php index ba5dfe6a..c5239f7f 100644 --- a/phpunit/src/InterfacePropertyHookTest.php +++ b/phpunit/src/InterfacePropertyHookTest.php @@ -9,7 +9,7 @@ final class InterfacePropertyHookTest extends TestCase private function compileFixture(string $file): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $path = __DIR__ . '/../code/' . $file; $compiler->addFiles([$path]); diff --git a/phpunit/src/LocalVariableInitializerTest.php b/phpunit/src/LocalVariableInitializerTest.php index a406354e..843fa7c4 100644 --- a/phpunit/src/LocalVariableInitializerTest.php +++ b/phpunit/src/LocalVariableInitializerTest.php @@ -8,9 +8,9 @@ final class LocalVariableInitializerTest extends \BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/local-literal-declaration-initializer.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/local-literal-declaration-initializer.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $generated = $compiler->convertFile($source); @@ -40,9 +40,9 @@ final class LocalVariableInitializerTest extends \BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/local-literal-declaration-initializer-native.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/local-literal-declaration-initializer-native.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $generated = $compiler->convertFile($source); @@ -61,9 +61,9 @@ final class LocalVariableInitializerTest extends \BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/local-constant-declaration-initializer.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/local-constant-declaration-initializer.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $generated = $compiler->convertFile($source); @@ -94,9 +94,9 @@ final class LocalVariableInitializerTest extends \BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/local-class-constant-declaration-initializer.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/local-class-constant-declaration-initializer.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $generated = $compiler->convertFile($source); diff --git a/phpunit/src/LoopOptimizerTest.php b/phpunit/src/LoopOptimizerTest.php index f5ad5d0c..22134b3d 100644 --- a/phpunit/src/LoopOptimizerTest.php +++ b/phpunit/src/LoopOptimizerTest.php @@ -9,7 +9,7 @@ class LoopOptimizerTest extends \BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/' . $file; $compiler->addFiles([$testFile]); @@ -17,7 +17,7 @@ class LoopOptimizerTest extends \BaseTest $compiler->convertFile($testFile); $this->addToAssertionCount(1); - return ROOT_PATH . '/build/phpunit/code/' . preg_replace('/\.php$/', '.cc', $file); + return TYPEPHP_ROOT_PATH . '/build/phpunit/code/' . preg_replace('/\.php$/', '.cc', $file); } public function testForBoundInternalConstantIsFolded(): void diff --git a/phpunit/src/MultiReturnTest.php b/phpunit/src/MultiReturnTest.php index 47c37144..7e4e2f3b 100644 --- a/phpunit/src/MultiReturnTest.php +++ b/phpunit/src/MultiReturnTest.php @@ -8,7 +8,7 @@ final class MultiReturnTest extends TestCase public function testGeneratesTupleFastPathAndArrayCompatibilityAdapter(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $file = __DIR__ . '/../code/multi-return-tuple.php'; $compiler->addFiles([$file]); diff --git a/phpunit/src/NativeClass/NativeClassValidationTest.php b/phpunit/src/NativeClass/NativeClassValidationTest.php index 6f50b4d9..36d5d695 100644 --- a/phpunit/src/NativeClass/NativeClassValidationTest.php +++ b/phpunit/src/NativeClass/NativeClassValidationTest.php @@ -10,7 +10,7 @@ final class NativeClassValidationTest extends \BaseTest { global $translator; - $compiler = \TypePhp\CompilerTest::create(ROOT_PATH); + $compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $directory = dirname(__DIR__, 2) . '/code/native-class-forward'; $files = [$directory . '/a.php', $directory . '/b.php']; @@ -29,7 +29,7 @@ final class NativeClassValidationTest extends \BaseTest { global $translator; - $compiler = \TypePhp\CompilerTest::create(ROOT_PATH); + $compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $directory = dirname(__DIR__, 2) . '/code/native-class-global-forward'; $files = [$directory . '/a.php', $directory . '/b.php']; diff --git a/phpunit/src/NativePropertyTest.php b/phpunit/src/NativePropertyTest.php index 59eab583..f82ee73b 100644 --- a/phpunit/src/NativePropertyTest.php +++ b/phpunit/src/NativePropertyTest.php @@ -9,7 +9,7 @@ class NativePropertyTest extends \BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/' . $file; $compiler->addFiles([$testFile]); @@ -17,7 +17,7 @@ class NativePropertyTest extends \BaseTest $compiler->convertFile($testFile); $this->addToAssertionCount(1); - return ROOT_PATH . '/build/phpunit/code/' . basename($file, '.php') . '.cc'; + return TYPEPHP_ROOT_PATH . '/build/phpunit/code/' . basename($file, '.php') . '.cc'; } public function testFindNativePropertyUsesFullClassNameAcrossBranches(): void diff --git a/phpunit/src/NativeScalarBinaryOperandTest.php b/phpunit/src/NativeScalarBinaryOperandTest.php index 8dfb83cf..7e92e7bc 100644 --- a/phpunit/src/NativeScalarBinaryOperandTest.php +++ b/phpunit/src/NativeScalarBinaryOperandTest.php @@ -8,9 +8,9 @@ final class NativeScalarBinaryOperandTest extends \BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/native-scalar-binary-operands.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/native-scalar-binary-operands.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $generated = $compiler->convertFile($source); @@ -25,9 +25,9 @@ final class NativeScalarBinaryOperandTest extends \BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/dynamic-scalar-binary-operands.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/dynamic-scalar-binary-operands.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $generated = $compiler->convertFile($source); diff --git a/phpunit/src/NewObjectCodegenTest.php b/phpunit/src/NewObjectCodegenTest.php index afe43231..1435c2f2 100644 --- a/phpunit/src/NewObjectCodegenTest.php +++ b/phpunit/src/NewObjectCodegenTest.php @@ -129,9 +129,9 @@ final class NewObjectCodegenTest extends \BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/new-object-codegen.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/new-object-codegen.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $generated = $compiler->convertFile($source); diff --git a/phpunit/src/OperatorTest.php b/phpunit/src/OperatorTest.php index c175f591..3afa9d9f 100644 --- a/phpunit/src/OperatorTest.php +++ b/phpunit/src/OperatorTest.php @@ -5,7 +5,7 @@ class OperatorTest extends \BaseTest public function testBooleanLiteralStrictComparisonUsesNativeBoolOperands(): void { global $translator; - $compiler = \TypePhp\CompilerTest::create(ROOT_PATH); + $compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/bool-literal-identical.php'; $compiler->addFiles([$testFile]); @@ -25,7 +25,7 @@ class OperatorTest extends \BaseTest public function testAssignedValueNotIdenticalToNullIsParenthesized(): void { global $translator; - $compiler = \TypePhp\CompilerTest::create(ROOT_PATH); + $compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/assign-not-identical-null.php'; $compiler->addFiles([$testFile]); @@ -42,7 +42,7 @@ class OperatorTest extends \BaseTest public function testDynamicBoolCallInLogicalExpressionIsConvertedToNativeBool(): void { global $translator; - $compiler = \TypePhp\CompilerTest::create(ROOT_PATH); + $compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/bool-dynamic-call-logical.php'; $compiler->addFiles([$testFile]); diff --git a/phpunit/src/ParallelCompileTest.php b/phpunit/src/ParallelCompileTest.php index be9c4cdb..69149347 100644 --- a/phpunit/src/ParallelCompileTest.php +++ b/phpunit/src/ParallelCompileTest.php @@ -98,7 +98,7 @@ class ScriptedWaitCompiler extends CompilerTest public function __construct(private array $waitResults, private array $forkResults = []) { - parent::__construct(ROOT_PATH); + parent::__construct(TYPEPHP_ROOT_PATH); $this->noProgress = true; } diff --git a/phpunit/src/Python/PythonModuleTest.php b/phpunit/src/Python/PythonModuleTest.php index 0d71624f..4680763d 100644 --- a/phpunit/src/Python/PythonModuleTest.php +++ b/phpunit/src/Python/PythonModuleTest.php @@ -12,9 +12,9 @@ final class PythonModuleTest extends TestCase public function testStaticallyResolvedPythonCallsUseNativeBridge(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/module-access.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/module-access.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $cpp = file_get_contents($compiler->convertFile($source)); @@ -30,9 +30,9 @@ final class PythonModuleTest extends TestCase public function testDynamicPythonMethodNameStillUsesZendDispatch(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/dynamic-method.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/dynamic-method.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $cpp = file_get_contents($compiler->convertFile($source)); @@ -44,9 +44,9 @@ final class PythonModuleTest extends TestCase public function testUsedModuleGeneratesLazyNativeBindingWithoutPhpyLinkage(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/module-access.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/module-access.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $cppFile = $compiler->convertFile($source); @@ -69,9 +69,9 @@ final class PythonModuleTest extends TestCase public function testUnusedPythonUseDoesNotGenerateModuleRuntimeState(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/unused-module.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/unused-module.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $compiler->convertFile($source); @@ -94,9 +94,9 @@ final class PythonModuleTest extends TestCase public function testFullyQualifiedModuleAttributeUsesNamespaceConstantSyntax(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/fully-qualified-module-constant.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/fully-qualified-module-constant.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $cpp = file_get_contents($compiler->convertFile($source)); @@ -132,9 +132,9 @@ final class PythonModuleTest extends TestCase public function testNestedModuleUsesPythonDottedImportName(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/nested-module.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/nested-module.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $compiler->convertFile($source); @@ -147,9 +147,9 @@ final class PythonModuleTest extends TestCase public function testPhpFunctionAndConstantImportsResolvePythonSymbols(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/imported-symbols.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/imported-symbols.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $cpp = file_get_contents($compiler->convertFile($source)); @@ -166,9 +166,9 @@ final class PythonModuleTest extends TestCase public function testFullyQualifiedModuleAccessDoesNotRequireUseDeclaration(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/fully-qualified-module.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/fully-qualified-module.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $cpp = file_get_contents($compiler->convertFile($source)); @@ -187,9 +187,9 @@ final class PythonModuleTest extends TestCase public function testRelativePythonNameInsideNamespaceRemainsPhpName(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/relative-module-name.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/relative-module-name.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $cpp = file_get_contents($compiler->convertFile($source)); @@ -204,11 +204,11 @@ final class PythonModuleTest extends TestCase public function testSameModuleAcrossFilesUsesOneRuntimeSlot(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $sources = [ - ROOT_PATH . '/phpunit/code/python/module-access.php', - ROOT_PATH . '/phpunit/code/python/duplicate-module.php', + TYPEPHP_ROOT_PATH . '/phpunit/code/python/module-access.php', + TYPEPHP_ROOT_PATH . '/phpunit/code/python/duplicate-module.php', ]; $compiler->addFiles($sources); foreach ($sources as $source) { @@ -223,9 +223,9 @@ final class PythonModuleTest extends TestCase public function testPythonBuiltinsUseBuiltinsModuleAndPreserveKnownObjectTypes(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/builtins.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/builtins.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $cpp = file_get_contents($compiler->convertFile($source)); @@ -258,9 +258,9 @@ final class PythonModuleTest extends TestCase public function testNestedPythonNameUsesModuleCallableSyntax(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/invalid-builtin-path.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/invalid-builtin-path.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $cpp = file_get_contents($compiler->convertFile($source)); @@ -274,9 +274,9 @@ final class PythonModuleTest extends TestCase public function testConstructorOnlyProgramConfiguresObjectPreservingRuntimeLazily(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/constructor-only.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/constructor-only.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $cpp = file_get_contents($compiler->convertFile($source)); @@ -290,9 +290,9 @@ final class PythonModuleTest extends TestCase public function testPythonOperatorsLowerToTheOperatorModule(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/operators.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/operators.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $cpp = file_get_contents($compiler->convertFile($source)); @@ -309,9 +309,9 @@ final class PythonModuleTest extends TestCase public function testPythonObjectProtocolUsesNativeCallsAndZendHandlersWhereAppropriate(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/object-protocol.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/object-protocol.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $cpp = file_get_contents($compiler->convertFile($source)); @@ -331,9 +331,9 @@ final class PythonModuleTest extends TestCase public function testPyObjectConversionMethodsUseTheNativeBridge(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/object-conversion-methods.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/object-conversion-methods.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $cpp = file_get_contents($compiler->convertFile($source)); @@ -351,9 +351,9 @@ final class PythonModuleTest extends TestCase private function compileFixture(string $file): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/python/' . $file; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/python/' . $file; $compiler->addFiles([$source]); $compiler->prepareFile($source); $compiler->convertFile($source); diff --git a/phpunit/src/PythonTools/PythonToolsCommandTest.php b/phpunit/src/PythonTools/PythonToolsCommandTest.php index a10d4850..42efe9aa 100644 --- a/phpunit/src/PythonTools/PythonToolsCommandTest.php +++ b/phpunit/src/PythonTools/PythonToolsCommandTest.php @@ -130,7 +130,7 @@ final class PythonToolsCommandTest extends TestCase */ private function runTpc(array $arguments): array { - $command = array_merge([PHP_BINARY, ROOT_PATH . '/bin/tpc.php'], $arguments); + $command = array_merge([PHP_BINARY, TYPEPHP_ROOT_PATH . '/bin/tpc.php'], $arguments); $process = proc_open($command, [ 0 => ['pipe', 'r'], 1 => ['pipe', 'w'], diff --git a/phpunit/src/ScopedCallContextTest.php b/phpunit/src/ScopedCallContextTest.php index ab20ffee..b0775afe 100644 --- a/phpunit/src/ScopedCallContextTest.php +++ b/phpunit/src/ScopedCallContextTest.php @@ -5,7 +5,7 @@ class ScopedCallContextTest extends \BaseTest public function testMethodCreatesOneReusableCallableScope(): void { global $translator; - $compiler = \TypePhp\CompilerTest::create(ROOT_PATH); + $compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $testFile = __DIR__ . '/../code/scoped-call-context-reuse.php'; $compiler->addFiles([$testFile]); diff --git a/phpunit/src/ShiftBoundaryTest.php b/phpunit/src/ShiftBoundaryTest.php index a906f13e..8bed1e8b 100644 --- a/phpunit/src/ShiftBoundaryTest.php +++ b/phpunit/src/ShiftBoundaryTest.php @@ -118,7 +118,7 @@ class ShiftBoundaryTest extends TestCase private function compileWithReporter(): object { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $reporter = new class implements DiagnosticReporter { /** @var list */ @@ -147,7 +147,7 @@ class ShiftBoundaryTest extends TestCase private function compileNativeWithReporter(string $file): CompilerTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $compiler->setDiagnosticReporter(new class implements DiagnosticReporter { public function fatal(string $message): never diff --git a/phpunit/src/StringConcatAssignTest.php b/phpunit/src/StringConcatAssignTest.php index 2d54f5bd..2ecdc84a 100644 --- a/phpunit/src/StringConcatAssignTest.php +++ b/phpunit/src/StringConcatAssignTest.php @@ -8,9 +8,9 @@ final class StringConcatAssignTest extends \BaseTest { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; - $source = ROOT_PATH . '/phpunit/code/string-concat-assign.php'; + $source = TYPEPHP_ROOT_PATH . '/phpunit/code/string-concat-assign.php'; $compiler->addFiles([$source]); $compiler->prepareFile($source); $generated = $compiler->convertFile($source); diff --git a/phpunit/src/Testing/TestCoverageAnalyzerTest.php b/phpunit/src/Testing/TestCoverageAnalyzerTest.php index 7d9599c2..e65604f7 100644 --- a/phpunit/src/Testing/TestCoverageAnalyzerTest.php +++ b/phpunit/src/Testing/TestCoverageAnalyzerTest.php @@ -92,7 +92,7 @@ final class CoverageFixtureTest extends \PHPUnit\Framework\TestCase } PHP); - $analyzer = new TestCoverageAnalyzer(ROOT_PATH, ['8.4', '8.5']); + $analyzer = new TestCoverageAnalyzer(TYPEPHP_ROOT_PATH, ['8.4', '8.5']); $report = $analyzer->analyze( [$this->testRoot . '/phpt'], $this->testRoot . '/phpunit-src', diff --git a/phpunit/src/TypeCheckGeneratorTest.php b/phpunit/src/TypeCheckGeneratorTest.php index 57716f75..90c6c5c3 100644 --- a/phpunit/src/TypeCheckGeneratorTest.php +++ b/phpunit/src/TypeCheckGeneratorTest.php @@ -24,7 +24,7 @@ class TypeCheckGeneratorTest extends \PHPUnit\Framework\TestCase public function testMethodTypeCheckErrorUsesClassQualifiedCallableName(): void { - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $classDef = new ClassDef('Demo', 0, 'Foo\\Bar'); $functionDef = new FunctionDef('run', 'php::Var', 'Foo\\Bar'); $argInfo = new ArgInfo(); @@ -52,7 +52,7 @@ class TypeCheckGeneratorTest extends \PHPUnit\Framework\TestCase public function testFunctionTypeCheckErrorUsesFunctionQualifiedCallableName(): void { - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $functionDef = new FunctionDef('run', 'php::Var', 'Foo\\Bar'); $argInfo = new ArgInfo(); $argInfo->name = 'value'; @@ -79,7 +79,7 @@ class TypeCheckGeneratorTest extends \PHPUnit\Framework\TestCase public function testStrictScalarChecksKeepTheFastCheckAndDelegateColdErrors(): void { - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $functionDef = new FunctionDef('run', 'php::Int', 'Foo\\Bar'); $argInfo = new ArgInfo(); $argInfo->name = 'value'; @@ -106,7 +106,7 @@ class TypeCheckGeneratorTest extends \PHPUnit\Framework\TestCase public function testDynamicStrictScalarArgumentsUseInlinePhpxConversions(): void { - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $this->setProtectedProperty($compiler, 'noLiteralStrings', true); foreach ([ diff --git a/phpunit/src/WasiUnsupportedSyntaxTest.php b/phpunit/src/WasiUnsupportedSyntaxTest.php index cea5e6be..6103b0c6 100644 --- a/phpunit/src/WasiUnsupportedSyntaxTest.php +++ b/phpunit/src/WasiUnsupportedSyntaxTest.php @@ -11,7 +11,7 @@ class WasiUnsupportedSyntaxTest extends TestCase /** @dataProvider unsupportedConversionSyntaxProvider */ public function testUnsupportedSyntaxFailsDuringWasiConversion(string $file, string $message): void { - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); (new \ReflectionClass($compiler))->getProperty('targetPlatform')->setValue($compiler, 'wasm32-wasip2'); $source = __DIR__ . '/../code/' . $file; $compiler->addFiles([$source]); diff --git a/phpunit/src/WrapperArgumentMoveTest.php b/phpunit/src/WrapperArgumentMoveTest.php index faf8ab31..51a371d7 100644 --- a/phpunit/src/WrapperArgumentMoveTest.php +++ b/phpunit/src/WrapperArgumentMoveTest.php @@ -8,7 +8,7 @@ final class WrapperArgumentMoveTest extends TestCase public function testWrapperConsumesOnlyDeadByValueWrappers(): void { global $translator; - $compiler = CompilerTest::create(ROOT_PATH); + $compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); $translator = $compiler; $file = __DIR__ . '/../code/wrapper-argument-move.php'; $compiler->addFiles([$file]); diff --git a/src/CompilerBase.php b/src/CompilerBase.php index 0e05a3d8..f73ce292 100644 --- a/src/CompilerBase.php +++ b/src/CompilerBase.php @@ -30,6 +30,7 @@ use TypePhp\Entity\MethodDef; use TypePhp\Entity\PropertyDef; use TypePhp\Exception\DynamicCall; use TypePhp\Exception\Redo; +use TypePhp\Exception\Unsupported; use TypePhp\Generator\AnonClassGenerator; use TypePhp\Generator\CallArgumentGenerator; use TypePhp\Generator\ClosureGenerator; @@ -575,6 +576,22 @@ class CompilerBase implements PropertyAccessContext return $this->lang; } + protected function unsupportedSyntax(NodeAbstract $node): never + { + $message = 'Error: Unsupported ' . $this->getLang() . ' Syntax,'; + $message .= ' Line: ' . $this->getLine($node) . ', Type: ' . $this->getType($node) . PHP_EOL; + if ($this->mode === 'cli') { + if (defined('TYPEPHP_DEBUG') && TYPEPHP_DEBUG) { + var_dump($node); + debug_print_backtrace(); + } + } else { + header('Content-Type: application/json'); + echo json_encode($node, JSON_PRETTY_PRINT); + } + throw new Unsupported($message); + } + protected function getIndent(): string { return str_repeat($this->indentStr, $this->indentLevel); @@ -934,7 +951,7 @@ class CompilerBase implements PropertyAccessContext case 'Expr_YieldFrom': return $this->parseYieldFromExpr($expr); default: - abort($expr); + $this->unsupportedSyntax($expr); break; } return ''; @@ -1501,7 +1518,7 @@ class CompilerBase implements PropertyAccessContext case 'Scalar_String': return $expr->hasAttribute('noLiteralString') ? $this->getInlineString($expr->value) : $this->getLiteralString($expr->value); default: - abort($expr); + $this->unsupportedSyntax($expr); break; } return ''; @@ -1873,7 +1890,7 @@ class CompilerBase implements PropertyAccessContext $this->fatalError($v, 'Cannot declare function in function'); break; default: - abort($v); + $this->unsupportedSyntax($v); break; } $lines = array_merge($lines, $this->context->beforeStmtLines); diff --git a/src/Parser/AssignOpTrait.php b/src/Parser/AssignOpTrait.php index da14e9d7..4305914a 100644 --- a/src/Parser/AssignOpTrait.php +++ b/src/Parser/AssignOpTrait.php @@ -289,7 +289,7 @@ trait AssignOpTrait $code .= $this->getIndent() . "{$var} = {$tmpVar}.item({$key});" . PHP_EOL; } } else { - abort($item); + $this->unsupportedSyntax($item); } } $this->indentLevel--; diff --git a/src/Parser/ConstantExpressionTrait.php b/src/Parser/ConstantExpressionTrait.php index 3515fbb9..28b0b6a5 100644 --- a/src/Parser/ConstantExpressionTrait.php +++ b/src/Parser/ConstantExpressionTrait.php @@ -25,7 +25,7 @@ trait ConstantExpressionTrait } if ($expr->name->getType() != 'Name' and !($expr->name instanceof Node\Name\FullyQualified)) { - abort($expr); + $this->unsupportedSyntax($expr); } $name = $this->parseIdentifier($expr->name); $name = ltrim($name, '\\'); @@ -175,7 +175,7 @@ trait ConstantExpressionTrait case 'Scalar_MagicConst_Method': return '"' . $this->escapeString($class) . '::' . $this->escapeString($this->method) . '"'; default: - abort($expr); + $this->unsupportedSyntax($expr); break; } } diff --git a/src/Parser/ForeachTrait.php b/src/Parser/ForeachTrait.php index 4180ada1..54f5478d 100644 --- a/src/Parser/ForeachTrait.php +++ b/src/Parser/ForeachTrait.php @@ -88,7 +88,7 @@ trait ForeachTrait } $array = $this->parseIdentifier($node->valueVar->var); if (!$this->hasVar($array) or $node->valueVar->dim === null) { - abort($node->valueVar); + $this->unsupportedSyntax($node->valueVar); } $dim = $this->parseIdentifier($node->valueVar->dim); return $this->getIndent() . "{$array}.offsetSet({$dim}, {$valueExpr});"; diff --git a/src/Preprocessor.php b/src/Preprocessor.php index 1926cd62..ead80b15 100644 --- a/src/Preprocessor.php +++ b/src/Preprocessor.php @@ -1327,7 +1327,7 @@ class Preprocessor extends CompilerBase $this->foundStrayCode($v); break; default: - abort($v); + $this->unsupportedSyntax($v); break; } } diff --git a/src/Translator.php b/src/Translator.php index 46553ad0..0b156288 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -56,6 +56,7 @@ use PhpParser\NodeAbstract; use PhpParser\NodeTraverser; use PhpParser\NodeVisitor\NameResolver; use PhpParser\NodeVisitor\CloningVisitor; +use function TypePhp\StubGenerator\generateStubFile; class Translator extends Preprocessor { @@ -2825,7 +2826,7 @@ CODE; case 'Stmt_Nop': break; default: - abort($v); + $this->unsupportedSyntax($v); break; } } @@ -3012,7 +3013,7 @@ CODE; case 'Stmt_Nop': break; default: - abort($v2); + $this->unsupportedSyntax($v2); break; } } @@ -3630,7 +3631,7 @@ CODE; $this->parseTraitUse($v, $methodCodes); break; default: - abort($v); + $this->unsupportedSyntax($v); break; } } diff --git a/src/compiler.php b/src/compiler.php index 9df24f03..d8f66778 100644 --- a/src/compiler.php +++ b/src/compiler.php @@ -12,8 +12,18 @@ function main(int $argc, array $argv): void // memory. The default CLI limit (commonly 128M) is too small for larger builds. ini_set('memory_limit', '-1'); - if (!defined('ROOT_PATH')) { - define("ROOT_PATH", getcwd()); + if (!defined('TYPEPHP_ROOT_PATH')) { + define('TYPEPHP_ROOT_PATH', getcwd()); + } + if (!defined('TYPEPHP_DEBUG')) { + define('TYPEPHP_DEBUG', true); + } + + // The Zend PHP entrypoint loads the consumer project's Composer autoloader + // in bin/bootstrap.php. The AOT compiler starts here directly and therefore + // must load the dependencies packaged alongside tpc itself. + if (!defined('TYPEPHP_PHP_SCRIPT_ENTRY')) { + require_once TYPEPHP_ROOT_PATH . '/vendor/autoload.php'; } $completionStatus = CompletionCommand::execute($argv); @@ -43,10 +53,9 @@ function main(int $argc, array $argv): void return; } - require_once ROOT_PATH . '/vendor/autoload.php'; global $translator; - $translator = new Translator(ROOT_PATH); + $translator = new Translator(TYPEPHP_ROOT_PATH); $translator->setIndent(' '); // 扫描所有 PHP 文件,预处理 $files = $translator->prepare($translator->parseArgv($argv)); @@ -173,7 +182,7 @@ function compileWasmProgram(array $argv): void $input, $buildDir, $workingDirectory, - ROOT_PATH . DIRECTORY_SEPARATOR . 'build', + TYPEPHP_ROOT_PATH . DIRECTORY_SEPARATOR . 'build', $profile, ); } catch (RuntimeException $exception) { @@ -240,7 +249,7 @@ function compileWasmProgram(array $argv): void } try { - $phpxDir = PhpxLocator::resolve(ROOT_PATH); + $phpxDir = PhpxLocator::resolve(TYPEPHP_ROOT_PATH); } catch (RuntimeException $exception) { fwrite(STDERR, "Unable to locate PHPX: {$exception->getMessage()}\n"); exit(1); diff --git a/src/functions.php b/src/functions.php deleted file mode 100644 index feb63940..00000000 --- a/src/functions.php +++ /dev/null @@ -1,54 +0,0 @@ -getLang(); - $msg = 'Error: Unsupported ' . $lang . ' Syntax,'; - $msg .= ' Line: ' . $translator->getLine($v) . ', Type: ' . $translator->getType($v) . PHP_EOL; - if ($translator->mode == 'cli') { - if (DEBUG) { - var_dump($v); - debug_print_backtrace(); - } - } else { - header('Content-Type: application/json'); - echo json_encode($v, JSON_PRETTY_PRINT); - } - throw new Unsupported($msg); -} - -function if_empty_debug($if_expr, $v) -{ - if (empty($if_expr)) { - abort($v); - } -} - -function if_not_empty_debug($if_expr, $v) -{ - if (!empty($if_expr)) { - abort($v); - } -} - -function debug() -{ - foreach (func_get_args() as $arg) { - var_dump($arg); - } - debug_print_backtrace(); - exit; -} diff --git a/src/gen_stub.php b/src/gen_stub.php index c39335b6..c5dca06b 100755 --- a/src/gen_stub.php +++ b/src/gen_stub.php @@ -1,6 +1,33 @@ getPathName(); - if (substr($pathName, -9) === '.stub.php') { + if (str_ends_with($pathName, '.stub.php')) { $pathNames[] = $pathName; } } @@ -544,7 +571,7 @@ class StubType { public static function fromNode(Node $node): StubType { if ($node instanceof Node\UnionType || $node instanceof Node\IntersectionType) { - $nestedTypeObjects = array_map(['StubType', 'fromNode'], $node->types); + $nestedTypeObjects = array_map([self::class, 'fromNode'], $node->types); $types = []; foreach ($nestedTypeObjects as $typeObject) { array_push($types, ...$typeObject->types); @@ -1961,9 +1988,9 @@ class FuncInfo { /* We create an attribute for xmlns, as libxml otherwise force it to be the first one */ //$refentry->setAttribute("xmlns", "http://docbook.org/ns/docbook"); $namespace = $doc->createAttribute('xmlns'); - $namespace->value = "http://docbook.org/ns/docbook"; + $namespace->value = "https://docbook.org/ns/docbook"; $refentry->setAttributeNode($namespace); - $refentry->setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink"); + $refentry->setAttribute("xmlns:xlink", "https://www.w3.org/1999/xlink"); $refentry->appendChild(new DOMText("\n ")); /* Creation of */ @@ -2571,7 +2598,12 @@ class EvaluatedValue } if ($expr instanceof Expr\ClassConstFetch) { - $className = resolveClassConstFetchClassName($expr, ClassInfo::$currentClass); + // TypePHP registers anonymous classes through eval(). Keep + // file-level function calls independent of the eval scope. + $className = \TypePhp\StubGenerator\resolveClassConstFetchClassName( + $expr, + ClassInfo::$currentClass, + ); $originatingConstName = new ClassConstName( new Name(ltrim($className, '\\')), $expr->name->toString()