Skip to content

Commit

Permalink
Introduce SystemAgnosticSimpleRelativePathHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 11, 2024
1 parent 0e53118 commit 908f705
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
26 changes: 26 additions & 0 deletions src/File/SystemAgnosticSimpleRelativePathHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace PHPStan\File;

use function str_starts_with;
use function strlen;
use function substr;

class SystemAgnosticSimpleRelativePathHelper implements RelativePathHelper
{

public function __construct(private FileHelper $fileHelper)
{
}

public function getRelativePath(string $filename): string
{
$cwd = $this->fileHelper->getWorkingDirectory();
if ($cwd !== '' && str_starts_with($filename, $cwd)) {
return substr($filename, strlen($cwd) + 1);
}

return $filename;
}

}
5 changes: 2 additions & 3 deletions src/Testing/TypeInferenceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\ParameterOutTypeExtensionProvider;
use PHPStan\File\FileHelper;
use PHPStan\File\RelativePathHelper;
use PHPStan\File\SystemAgnosticSimpleRelativePathHelper;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
use PHPStan\PhpDoc\StubPhpDocProvider;
Expand Down Expand Up @@ -146,8 +146,7 @@ public static function gatherAssertTypes(string $file): array
{
$fileHelper = self::getContainer()->getByType(FileHelper::class);

/** @var RelativePathHelper $relativePathHelper */
$relativePathHelper = self::getContainer()->getService('simpleRelativePathHelper');
$relativePathHelper = new SystemAgnosticSimpleRelativePathHelper($fileHelper);

$file = $fileHelper->normalizePath($file);

Expand Down

0 comments on commit 908f705

Please sign in to comment.