From 5bbb50b031421d368dd8aea7a1ba77ddaf374fa9 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 30 Jun 2026 09:09:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E4=BC=98=E5=8C=96=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E5=89=8D=E7=BC=80=E7=A7=BB=E9=99=A4=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E4=BB=A5=E6=94=AF=E6=8C=81=E8=B7=AF=E5=BE=84=E6=AE=B5=E5=8C=B9?= =?UTF-8?q?=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 PlatformBase.php 中的 removeCommonPrefix 方法 - 使用 rtrim 移除路径分隔符后缀 - 将路径拆分为段进行比较而非逐字符比较 - 使用 implode 重新组合剩余路径段 - 添加新测试用例验证路径段匹配功能 - 修复相邻目录按字符前缀截断的问题 --- phpunit/src/Platform/PlatformTest.php | 9 +++++++++ phpunit/src/PreprocessorTest.php | 9 +++++++++ src/Php/Platform/PlatformBase.php | 10 +++++++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/phpunit/src/Platform/PlatformTest.php b/phpunit/src/Platform/PlatformTest.php index 35e0ec27..589ad117 100644 --- a/phpunit/src/Platform/PlatformTest.php +++ b/phpunit/src/Platform/PlatformTest.php @@ -110,6 +110,15 @@ class PlatformTest extends TestCase $this->assertSame('src/app.php', $linux->removeCommonPrefix('/project', '/project/src/app.php')); } + public function testPlatformPathPrefixRemovalUsesPathSegments(): void + { + $windows = new Windows(); + $linux = new Linux(); + + $this->assertSame('project2\src\app.php', $windows->removeCommonPrefix('C:\project', 'C:/project2/src/app.php')); + $this->assertSame('project2/src/app.php', $linux->removeCommonPrefix('/tmp/project', '/tmp/project2/src/app.php')); + } + /** * 测试 Windows 子系统选项 */ diff --git a/phpunit/src/PreprocessorTest.php b/phpunit/src/PreprocessorTest.php index 2f743a98..3e1f1bc2 100644 --- a/phpunit/src/PreprocessorTest.php +++ b/phpunit/src/PreprocessorTest.php @@ -135,6 +135,15 @@ class PreprocessorTest extends TestCase $this->assertStringStartsWith($this->compiler->getBuildDir(), $result); } + public function testGetCppFileDoesNotTrimSiblingDirectoryByCharacterPrefix(): void + { + $this->setProperty('buildDir', '/tmp/project/build'); + + $result = $this->compiler->getCppFile('/tmp/project2/src/app.php'); + + $this->assertSame('/tmp/project/build/project2/src/app.cc', $result); + } + public function testGetCppFileDotPhpReplaced(): void { $phpFile = '/tmp/test_file.php'; diff --git a/src/Php/Platform/PlatformBase.php b/src/Php/Platform/PlatformBase.php index 272ca47d..fc4e2da6 100644 --- a/src/Php/Platform/PlatformBase.php +++ b/src/Php/Platform/PlatformBase.php @@ -154,18 +154,22 @@ abstract class PlatformBase $long = str_replace('/', '\\', $long); } - $len = min(strlen($short), strlen($long)); + $short = rtrim($short, $separator); + $long = rtrim($long, $separator); + $shortParts = explode($separator, $short); + $longParts = explode($separator, $long); $prefixLen = 0; + $len = min(count($shortParts), count($longParts)); for ($i = 0; $i < $len; $i++) { - if ($short[$i] === $long[$i]) { + if ($shortParts[$i] === $longParts[$i]) { $prefixLen++; } else { break; } } - return ltrim(substr($long, $prefixLen), $separator); + return implode($separator, array_slice($longParts, $prefixLen)); } /**