Skip to content

Commit

Permalink
always make paths absolute before processing files
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanti committed Sep 6, 2024
1 parent 4a94d2e commit 4a95676
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/FileSystem/FilePathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public function relativePath(string $fileRealPath): string
return $this->relativeFilePathFromDirectory($fileRealPath, getcwd());
}

public function absolutePath(string $fileRealPath): string
{
if ($this->filesystem->isAbsolutePath($fileRealPath)) {
return $fileRealPath;
}

return getcwd() . '/' . $fileRealPath;
}

/**
* Used from
* https://github.com/phpstan/phpstan-src/blob/02425e61aa48f0668b4efb3e73d52ad544048f65/src/File/FileHelper.php#L40, with custom modifications
Expand Down
1 change: 1 addition & 0 deletions src/FileSystem/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function findInDirectoriesAndFiles(array $source, array $suffixes = [], b

// filtering files in files collection
$filteredFilePaths = $this->fileAndDirectoryFilter->filterFiles($filesAndDirectories);
$filteredFilePaths = array_map($this->filePathHelper->absolutePath(...), $filteredFilePaths);
$filteredFilePaths = array_filter(
$filteredFilePaths,
fn (string $filePath): bool => ! $this->pathSkipper->shouldSkip($filePath)
Expand Down
20 changes: 20 additions & 0 deletions tests/FileSystem/FilesFinder/FilesFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ public function test(): void
$this->assertCount(0, $foundFiles);
}

#[DataProvider('testAlwaysReturnsAbsolutePathDataProvider')]
public function testAlwaysReturnsAbsolutePath(string $relativePath): void
{
$absolutePath = str_replace('/', DIRECTORY_SEPARATOR, getcwd() . '/' . $relativePath);
$foundFiles = $this->filesFinder->findInDirectoriesAndFiles([$absolutePath], ['php']);
$this->assertStringStartsWith($absolutePath, $foundFiles[0], 'should return absolute path if absolute is given');

$foundFiles = $this->filesFinder->findInDirectoriesAndFiles([$relativePath], ['php']);
$this->assertStringStartsWith($absolutePath, $foundFiles[0], 'should return absolute path if relative is given');
}

/**
* @return Iterator<array<string>>
*/
public static function testAlwaysReturnsAbsolutePathDataProvider(): Iterator
{
yield 'directory given' => ['tests/FileSystem/FilesFinder/Source/'];
yield 'file given' => ['tests/FileSystem/FilesFinder/Source/SomeFile.php'];
}

public function testWithFollowingBrokenSymlinks(): void
{
SimpleParameterProvider::setParameter(Option::SKIP, [__DIR__ . '/../SourceWithBrokenSymlinks/folder1']);
Expand Down

0 comments on commit 4a95676

Please sign in to comment.