Skip to content

Commit 1c09337

Browse files
authored
refactor: Use Symfony's Path instead of a custom function (#806)
1 parent bdba84e commit 1c09337

File tree

3 files changed

+13
-142
lines changed

3 files changed

+13
-142
lines changed

src/Console/ConsoleScoper.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
2424
use Humbug\PhpScoper\Throwable\Exception\ParsingException;
2525
use Symfony\Component\Filesystem\Filesystem;
26+
use Symfony\Component\Filesystem\Path;
2627
use Throwable;
28+
use Webmozart\Assert\Assert;
2729
use function array_column;
2830
use function array_keys;
2931
use function array_map;
3032
use function count;
31-
use function Humbug\PhpScoper\get_common_path;
3233
use function preg_match as native_preg_match;
3334
use function Safe\file_get_contents;
3435
use function Safe\fileperms;
@@ -166,17 +167,22 @@ private static function getFiles(Configuration $config, string $outputDir): arra
166167
$filesWithContent = $config->getFilesWithContents();
167168
$excludedFilesWithContents = $config->getExcludedFilesWithContents();
168169

169-
$commonPath = get_common_path(
170-
[
171-
...array_keys($filesWithContent),
172-
...array_keys($excludedFilesWithContents),
173-
],
170+
$commonDirectoryPath = Path::getLongestCommonBasePath(
171+
...array_map(
172+
static fn (string $path) => Path::getDirectory($path),
173+
array_keys($filesWithContent),
174+
),
175+
...array_map(
176+
static fn (string $path) => Path::getDirectory($path),
177+
array_keys($excludedFilesWithContents),
178+
),
174179
);
180+
Assert::notNull($commonDirectoryPath);
175181

176182
$mapFiles = static fn (array $inputFileTuple) => [
177183
$inputFileTuple[0],
178184
$inputFileTuple[1],
179-
$outputDir.str_replace($commonPath, '', $inputFileTuple[0]),
185+
$outputDir.str_replace($commonDirectoryPath, '', $inputFileTuple[0]),
180186
];
181187

182188
return [

src/functions.php

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616

1717
use Composer\InstalledVersions;
1818
use Iterator;
19-
use function array_pop;
20-
use function count;
21-
use function str_split;
2219
use function str_starts_with;
23-
use function strrpos;
2420
use function substr;
2521

2622
function get_php_scoper_version(): string
@@ -38,48 +34,6 @@ function get_php_scoper_version(): string
3834
return $prettyVersion.'@'.$shortCommitHash;
3935
}
4036

41-
/**
42-
* @param string[] $paths Absolute paths
43-
*/
44-
function get_common_path(array $paths): string
45-
{
46-
$nbPaths = count($paths);
47-
48-
if (0 === $nbPaths) {
49-
return '';
50-
}
51-
52-
$pathRef = (string) array_pop($paths);
53-
54-
if (1 === $nbPaths) {
55-
$commonPath = $pathRef;
56-
} else {
57-
$commonPath = '';
58-
59-
foreach (str_split($pathRef) as $pos => $char) {
60-
foreach ($paths as $path) {
61-
if (!isset($path[$pos]) || $path[$pos] !== $char) {
62-
break 2;
63-
}
64-
}
65-
66-
$commonPath .= $char;
67-
}
68-
}
69-
70-
foreach (['/', '\\'] as $separator) {
71-
$lastSeparatorPos = strrpos($commonPath, $separator);
72-
73-
if (false !== $lastSeparatorPos) {
74-
$commonPath = rtrim(substr($commonPath, 0, $lastSeparatorPos), $separator);
75-
76-
break;
77-
}
78-
}
79-
80-
return $commonPath;
81-
}
82-
8337
function chain(iterable ...$iterables): Iterator
8438
{
8539
foreach ($iterables as $iterable) {

tests/FunctionsTest.php

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,6 @@ public function test_it_gets_the_php_scoper_version(): void
4040
self::assertStringContainsString('@', $version);
4141
}
4242

43-
/**
44-
* @dataProvider providePaths
45-
*/
46-
public function test_get_the_common_path(array $paths, string $expected): void
47-
{
48-
$actual = get_common_path($paths);
49-
50-
self::assertSame($expected, $actual);
51-
}
52-
5343
/**
5444
* @dataProvider provideGenerators
5545
*/
@@ -60,85 +50,6 @@ public function test_it_can_chain_iterators(array $iterators, array $expected):
6050
self::assertSame($expected, $actual);
6151
}
6252

63-
public static function providePaths(): iterable
64-
{
65-
yield [
66-
[],
67-
'',
68-
];
69-
70-
yield [
71-
[
72-
'/path/to/file',
73-
],
74-
'/path/to',
75-
];
76-
77-
yield [
78-
[
79-
'/path/to/file',
80-
'/path/to/another-file',
81-
],
82-
'/path/to',
83-
];
84-
85-
yield [
86-
[
87-
'/path/to/file',
88-
'/path/to/another-file',
89-
'/path/another-to/another-file',
90-
],
91-
'/path',
92-
];
93-
94-
yield [
95-
[
96-
'/path/to/file',
97-
'/another/path/to/another-file',
98-
],
99-
'',
100-
];
101-
102-
yield [
103-
[
104-
'/file',
105-
],
106-
'',
107-
];
108-
109-
yield [
110-
[
111-
'C:\\path\\to\\file',
112-
],
113-
'C:\\path\\to',
114-
];
115-
116-
yield [
117-
[
118-
'C:\\path\\to\\file',
119-
'C:\\path\\to\\another-file',
120-
],
121-
'C:\\path\\to',
122-
];
123-
124-
yield [
125-
[
126-
'C:\\path\\to\\file',
127-
'C:\\path\\to\\another-file',
128-
'C:\\path\\another-to\\another-file',
129-
],
130-
'C:\\path',
131-
];
132-
133-
yield [
134-
[
135-
'C:\\path\\to\\file',
136-
'D:\\another\\path\\to\\another-file',
137-
],
138-
'',
139-
];
140-
}
141-
14253
public static function provideGenerators(): iterable
14354
{
14455
yield [

0 commit comments

Comments
 (0)