fix(compiler): discover native globals with case-insensitive keywords (#124) --skip-tests

master
yavon007 3 weeks ago committed by GitHub
parent b3898c329d
commit f84d20e346
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 7
      phpunit/code/native-class-global-keyword-case/mixed-case.php
  2. 13
      phpunit/code/native-class-global-keyword-case/reader.php
  3. 7
      phpunit/code/native-class-global-keyword-case/uppercase.php
  4. 35
      phpunit/src/NativeClass/NativeClassValidationTest.php
  5. 5
      src/Preprocessor.php

@ -0,0 +1,7 @@
<?php
function initializeCaseSlot(): void
{
GlObAl $caseSlot;
$caseSlot = new NativeCaseValue();
}

@ -0,0 +1,13 @@
<?php
#[Native]
class NativeCaseValue
{
public int $value = 42;
}
function readCaseSlot(): int
{
global $caseSlot;
return $caseSlot->value;
}

@ -0,0 +1,7 @@
<?php
function initializeCaseSlot(): void
{
GLOBAL $caseSlot;
$caseSlot = new NativeCaseValue();
}

@ -89,6 +89,41 @@ final class NativeClassValidationTest extends \BaseTest
self::assertStringNotContainsString('nativeForwardGlobal.attr(', $code);
}
/**
* @dataProvider nativeGlobalKeywordCaseProvider
*/
public function testDiscoversNativeGlobalSlotWithCaseInsensitiveKeyword(string $writer): void
{
global $translator;
$compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH);
$translator = $compiler;
$directory = dirname(__DIR__, 2) . '/code/native-class-global-keyword-case';
$files = [$directory . '/reader.php', $directory . '/' . $writer];
$compiler->discoverNativeClassDeclarations($files);
foreach ($files as $file) {
$compiler->prepareFile($file);
}
$compiler->discoverNativeGlobalObjects($files);
$reader = $compiler->convertFile($files[0]);
$compiler->convertFile($files[1]);
$code = file_get_contents($reader);
self::assertIsString($code);
self::assertStringContainsString(
'php::nativeRequireObject(caseSlot, "NativeCaseValue")->value',
$code,
);
}
public static function nativeGlobalKeywordCaseProvider(): array
{
return [
'uppercase' => ['uppercase.php'],
'mixed case' => ['mixed-case.php'],
];
}
public function testRejectsNativeAttributeOnInterface(): void
{
$this->expectException(\TypePhp\Exception\SyntaxError::class);

@ -223,9 +223,8 @@ class Preprocessor extends CompilerBase
continue;
}
$source = file_get_contents($file);
if (is_string($source)
&& (str_contains($source, 'global') || str_contains($source, '$GLOBALS'))
) {
// PHP keywords are case-insensitive; this also includes $GLOBALS accesses.
if (is_string($source) && stripos($source, 'global') !== false) {
$candidateSources[$file] = $source;
}
}

Loading…
Cancel
Save