refactor(php): 优化路径前缀移除逻辑以支持路径段匹配

- 修改 PlatformBase.php 中的 removeCommonPrefix 方法
- 使用 rtrim 移除路径分隔符后缀
- 将路径拆分为段进行比较而非逐字符比较
- 使用 implode 重新组合剩余路径段
- 添加新测试用例验证路径段匹配功能
- 修复相邻目录按字符前缀截断的问题
pull/5/head
韩天峰 2 months ago
parent c8372fce04
commit 5bbb50b031
  1. 9
      phpunit/src/Platform/PlatformTest.php
  2. 9
      phpunit/src/PreprocessorTest.php
  3. 10
      src/Php/Platform/PlatformBase.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 子系统选项
*/

@ -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';

@ -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));
}
/**

Loading…
Cancel
Save