Skip to content

refactor: Use Symfony's Path instead of a custom function #806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/Console/ConsoleScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use Humbug\PhpScoper\Throwable\Exception\ParsingException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Throwable;
use Webmozart\Assert\Assert;
use function array_column;
use function array_keys;
use function array_map;
use function count;
use function Humbug\PhpScoper\get_common_path;
use function preg_match as native_preg_match;
use function Safe\file_get_contents;
use function Safe\fileperms;
Expand Down Expand Up @@ -166,17 +167,22 @@ private static function getFiles(Configuration $config, string $outputDir): arra
$filesWithContent = $config->getFilesWithContents();
$excludedFilesWithContents = $config->getExcludedFilesWithContents();

$commonPath = get_common_path(
[
...array_keys($filesWithContent),
...array_keys($excludedFilesWithContents),
],
$commonDirectoryPath = Path::getLongestCommonBasePath(
...array_map(
static fn (string $path) => Path::getDirectory($path),
array_keys($filesWithContent),
),
...array_map(
static fn (string $path) => Path::getDirectory($path),
array_keys($excludedFilesWithContents),
),
);
Assert::notNull($commonDirectoryPath);

$mapFiles = static fn (array $inputFileTuple) => [
$inputFileTuple[0],
$inputFileTuple[1],
$outputDir.str_replace($commonPath, '', $inputFileTuple[0]),
$outputDir.str_replace($commonDirectoryPath, '', $inputFileTuple[0]),
];

return [
Expand Down
46 changes: 0 additions & 46 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

use Composer\InstalledVersions;
use Iterator;
use function array_pop;
use function count;
use function str_split;
use function str_starts_with;
use function strrpos;
use function substr;

function get_php_scoper_version(): string
Expand All @@ -38,48 +34,6 @@ function get_php_scoper_version(): string
return $prettyVersion.'@'.$shortCommitHash;
}

/**
* @param string[] $paths Absolute paths
*/
function get_common_path(array $paths): string
{
$nbPaths = count($paths);

if (0 === $nbPaths) {
return '';
}

$pathRef = (string) array_pop($paths);

if (1 === $nbPaths) {
$commonPath = $pathRef;
} else {
$commonPath = '';

foreach (str_split($pathRef) as $pos => $char) {
foreach ($paths as $path) {
if (!isset($path[$pos]) || $path[$pos] !== $char) {
break 2;
}
}

$commonPath .= $char;
}
}

foreach (['/', '\\'] as $separator) {
$lastSeparatorPos = strrpos($commonPath, $separator);

if (false !== $lastSeparatorPos) {
$commonPath = rtrim(substr($commonPath, 0, $lastSeparatorPos), $separator);

break;
}
}

return $commonPath;
}

function chain(iterable ...$iterables): Iterator
{
foreach ($iterables as $iterable) {
Expand Down
89 changes: 0 additions & 89 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ public function test_it_gets_the_php_scoper_version(): void
self::assertStringContainsString('@', $version);
}

/**
* @dataProvider providePaths
*/
public function test_get_the_common_path(array $paths, string $expected): void
{
$actual = get_common_path($paths);

self::assertSame($expected, $actual);
}

/**
* @dataProvider provideGenerators
*/
Expand All @@ -60,85 +50,6 @@ public function test_it_can_chain_iterators(array $iterators, array $expected):
self::assertSame($expected, $actual);
}

public static function providePaths(): iterable
{
yield [
[],
'',
];

yield [
[
'/path/to/file',
],
'/path/to',
];

yield [
[
'/path/to/file',
'/path/to/another-file',
],
'/path/to',
];

yield [
[
'/path/to/file',
'/path/to/another-file',
'/path/another-to/another-file',
],
'/path',
];

yield [
[
'/path/to/file',
'/another/path/to/another-file',
],
'',
];

yield [
[
'/file',
],
'',
];

yield [
[
'C:\\path\\to\\file',
],
'C:\\path\\to',
];

yield [
[
'C:\\path\\to\\file',
'C:\\path\\to\\another-file',
],
'C:\\path\\to',
];

yield [
[
'C:\\path\\to\\file',
'C:\\path\\to\\another-file',
'C:\\path\\another-to\\another-file',
],
'C:\\path',
];

yield [
[
'C:\\path\\to\\file',
'D:\\another\\path\\to\\another-file',
],
'',
];
}

public static function provideGenerators(): iterable
{
yield [
Expand Down