Skip to content

Commit

Permalink
[DX] Make use of FilePathHelper over inner magic of FileSystem in Sma…
Browse files Browse the repository at this point in the history
…rtFileInfo (#2862)
  • Loading branch information
TomasVotruba committed Aug 30, 2022
1 parent bb3ced4 commit b01ea04
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter;
use Rector\Core\FileSystem\FilePathHelper;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;

abstract class AbstractPhpDocInfoPrinterTest extends AbstractTestCase
{
protected FilePathHelper $filePathHelper;

protected PhpDocInfoPrinter $phpDocInfoPrinter;

private PhpDocInfoFactory $phpDocInfoFactory;
Expand All @@ -23,6 +26,7 @@ protected function setUp(): void
{
$this->boot();

$this->filePathHelper = $this->getService(FilePathHelper::class);
$this->phpDocInfoFactory = $this->getService(PhpDocInfoFactory::class);
$this->phpDocInfoPrinter = $this->getService(PhpDocInfoPrinter::class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Rector\Tests\BetterPhpDocParser\PhpDocInfo\PhpDocInfoPrinter\Source\Doctrine\CaseSensitive;
use Rector\Tests\BetterPhpDocParser\PhpDocInfo\PhpDocInfoPrinter\Source\Doctrine\IndexInTable;
use Rector\Tests\BetterPhpDocParser\PhpDocInfo\PhpDocInfoPrinter\Source\Doctrine\Short;
use Symplify\SmartFileSystem\SmartFileInfo;

final class DoctrineTest extends AbstractPhpDocInfoPrinterTest
{
Expand All @@ -23,8 +22,7 @@ public function testClass(string $docFilePath, Node $node): void
$docComment = FileSystem::read($docFilePath);
$phpDocInfo = $this->createPhpDocInfoFromDocCommentAndNode($docComment, $node);

$fileInfo = new SmartFileInfo($docFilePath);
$relativeFilePathFromCwd = $fileInfo->getRelativeFilePathFromCwd();
$relativeFilePathFromCwd = $this->filePathHelper->relativePath($docFilePath);

$printedPhpDocInfo = $this->phpDocInfoPrinter->printFormatPreserving($phpDocInfo);
$this->assertSame($docComment, $printedPhpDocInfo, $relativeFilePathFromCwd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PhpParser\Node\Stmt\Property;
use Rector\Tests\BetterPhpDocParser\PhpDocInfo\PhpDocInfoPrinter\Source\Class_\SomeEntityClass;
use Rector\Tests\BetterPhpDocParser\PhpDocInfo\PhpDocInfoPrinter\Source\TableClass;
use Symplify\SmartFileSystem\SmartFileInfo;

final class MultilineTest extends AbstractPhpDocInfoPrinterTest
{
Expand All @@ -28,11 +27,8 @@ public function test(string $docFilePath, Node $node): void
$docComment = FileSystem::read($docFilePath);
$phpDocInfo = $this->createPhpDocInfoFromDocCommentAndNode($docComment, $node);

$fileInfo = new SmartFileInfo($docFilePath);
$relativeFilePathFromCwd = $fileInfo->getRelativeFilePathFromCwd();

$printedPhpDocInfo = $this->phpDocInfoPrinter->printFormatPreserving($phpDocInfo);
$this->assertSame($docComment, $printedPhpDocInfo, $relativeFilePathFromCwd);
$this->assertSame($docComment, $printedPhpDocInfo);
}

public function provideData(): Iterator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Iterator;
use PhpParser\Node\Stmt\Nop;
use Rector\Core\FileSystem\FilePathHelper;
use Symplify\SmartFileSystem\SmartFileInfo;

final class PhpDocInfoPrinterTest extends AbstractPhpDocInfoPrinterTest
Expand Down Expand Up @@ -56,10 +57,9 @@ private function doComparePrintedFileEquals(SmartFileInfo $inputFileInfo, SmartF
$phpDocInfo = $this->createPhpDocInfoFromDocCommentAndNode($inputFileInfo->getContents(), new Nop());
$printedDocComment = $this->phpDocInfoPrinter->printFormatPreserving($phpDocInfo);

$this->assertSame(
$expectedFileInfo->getContents(),
$printedDocComment,
$inputFileInfo->getRelativeFilePathFromCwd()
);
$filePathHelper = $this->getService(FilePathHelper::class);
$relativeInputFilePath = $filePathHelper->relativePath($inputFileInfo->getRealPath());

$this->assertSame($expectedFileInfo->getContents(), $printedDocComment, $relativeInputFilePath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public function testResolvesClass(SmartFileInfo $file): void
*/
public function provideData(): Iterator
{
$directory = __DIR__ . '/Fixture';
return StaticFixtureFinder::yieldDirectory($directory);
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\FileSystem\FilePathHelper;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\FileSystemRector\Parser\FileInfoParser;
use Rector\Testing\PHPUnit\AbstractTestCase;
Expand All @@ -29,12 +30,14 @@ final class TagValueNodeReprintTest extends AbstractTestCase

private PhpDocInfoFactory $phpDocInfoFactory;

private FilePathHelper $filePathHelper;

protected function setUp(): void
{
$this->boot();

$this->fileInfoParser = $this->getService(FileInfoParser::class);

$this->filePathHelper = $this->getService(FilePathHelper::class);
$this->betterNodeFinder = $this->getService(BetterNodeFinder::class);
$this->phpDocInfoPrinter = $this->getService(PhpDocInfoPrinter::class);
$this->phpDocInfoFactory = $this->getService(PhpDocInfoFactory::class);
Expand Down Expand Up @@ -153,6 +156,7 @@ private function doTestContainsTagValueNodeType(
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$hasByAnnotationClass = $phpDocInfo->hasByAnnotationClass($annotationClass);

$this->assertTrue($hasByAnnotationClass, $smartFileInfo->getRelativeFilePathFromCwd());
$relativeFilePath = $this->filePathHelper->relativePath($smartFileInfo->getRealPath());
$this->assertTrue($hasByAnnotationClass, $relativeFilePath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Rector\ChangesReporting\Annotation\RectorsChangelogResolver;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\FileSystem\FilePathHelper;
use Rector\Core\ValueObject\Reporting\FileDiff;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver\Source\RectorWithChangelog;
Expand All @@ -18,10 +19,13 @@ final class RectorsChangelogResolverTest extends AbstractTestCase

private FileDiff $fileDiff;

private FilePathHelper $filePathHelper;

protected function setUp(): void
{
$this->boot();
$this->rectorsChangelogResolver = $this->getService(RectorsChangelogResolver::class);
$this->filePathHelper = $this->getService(FilePathHelper::class);

$this->fileDiff = $this->createFileDiff();
}
Expand All @@ -39,13 +43,16 @@ public function test(): void
private function createFileDiff(): FileDiff
{
// This is by intention to test the array_unique functionality
$rectorWithLineChanges = [];
$rectorWithLineChanges[] = new RectorWithLineChange(RectorWithChangelog::class, 1);
$rectorWithLineChanges[] = new RectorWithLineChange(RectorWithChangelog::class, 1);
$rectorWithLineChanges[] = new RectorWithLineChange(RectorWithOutChangelog::class, 1);
$rectorWithLineChanges = [
new RectorWithLineChange(RectorWithChangelog::class, 1),
new RectorWithLineChange(RectorWithChangelog::class, 1),
new RectorWithLineChange(RectorWithOutChangelog::class, 1),
];

$smartFileInfo = new SmartFileInfo(__FILE__);

return new FileDiff($smartFileInfo->getRelativeFilePathFromCwd(), 'foo', 'foo', $rectorWithLineChanges);
$relativeFilePath = $this->filePathHelper->relativePath($smartFileInfo->getRealPath());

return new FileDiff($relativeFilePath, 'foo', 'foo', $rectorWithLineChanges);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public function test(SmartFileInfo $smartFileInfo): void
$fileContent = trim($fileContent);

$expectedContent = trim((string) $fileInfoToLocalInputAndExpected->getExpected());

$this->assertSame($fileContent, $expectedContent, $smartFileInfo->getRelativeFilePathFromCwd());
$this->assertSame($fileContent, $expectedContent);

// original nodes are not touched
$originalContent = $this->nodePrinter->print($nodes);
Expand Down
8 changes: 6 additions & 2 deletions packages/ChangesReporting/ValueObjectFactory/ErrorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

use PHPStan\AnalysedCodeException;
use Rector\Core\Error\ExceptionCorrector;
use Rector\Core\FileSystem\FilePathHelper;
use Rector\Core\ValueObject\Error\SystemError;
use Symplify\SmartFileSystem\SmartFileInfo;

final class ErrorFactory
{
public function __construct(
private readonly ExceptionCorrector $exceptionCorrector
private readonly ExceptionCorrector $exceptionCorrector,
private readonly FilePathHelper $filePathHelper
) {
}

Expand All @@ -21,6 +23,8 @@ public function createAutoloadError(
SmartFileInfo $smartFileInfo
): SystemError {
$message = $this->exceptionCorrector->getAutoloadExceptionMessageAndAddLocation($analysedCodeException);
return new SystemError($message, $smartFileInfo->getRelativeFilePathFromCwd());
$relativeFilePath = $this->filePathHelper->relativePath($smartFileInfo->getRealPath());

return new SystemError($message, $relativeFilePath);
}
}
15 changes: 10 additions & 5 deletions packages/NodeNameResolver/Error/InvalidNameNodeReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\FileSystem\FilePathHelper;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
use Symplify\SmartFileSystem\SmartFileInfo;

final class InvalidNameNodeReporter
{
Expand All @@ -22,7 +22,8 @@ final class InvalidNameNodeReporter

public function __construct(
private readonly CurrentFileProvider $currentFileProvider,
private readonly NodePrinterInterface $nodePrinter
private readonly NodePrinterInterface $nodePrinter,
private readonly FilePathHelper $filePathHelper
) {
}

Expand All @@ -35,9 +36,12 @@ public function reportInvalidNodeForName(MethodCall | StaticCall $node): void
if ($file instanceof File) {
$smartFileInfo = $file->getSmartFileInfo();
$message .= PHP_EOL . PHP_EOL;

$relatilveFilePath = $this->filePathHelper->relativePath($smartFileInfo->getRealPath());

$message .= sprintf(
'Caused in "%s" file on line %d on code "%s"',
$smartFileInfo->getRelativeFilePathFromCwd(),
$relatilveFilePath,
$node->getStartLine(),
$this->nodePrinter->print($node)
);
Expand All @@ -49,8 +53,9 @@ public function reportInvalidNodeForName(MethodCall | StaticCall $node): void
if ($rectorBacktrace !== null) {
// issues to find the file in prefixed
if (file_exists($rectorBacktrace[self::FILE])) {
$smartFileInfo = new SmartFileInfo($rectorBacktrace[self::FILE]);
$fileAndLine = $smartFileInfo->getRelativeFilePathFromCwd() . ':' . $rectorBacktrace['line'];
$filePath = $rectorBacktrace[self::FILE];
$relatilveFilePath = $this->filePathHelper->relativePath($filePath);
$fileAndLine = $relatilveFilePath . ':' . $rectorBacktrace['line'];
} else {
$fileAndLine = $rectorBacktrace[self::FILE] . ':' . $rectorBacktrace['line'];
}
Expand Down
37 changes: 19 additions & 18 deletions packages/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,7 @@ protected function setUp(): void
{
// speed up
@ini_set('memory_limit', '-1');

// include local files
if (file_exists(__DIR__ . '/../../../preload.php')) {
if (file_exists(__DIR__ . '/../../../vendor')) {
require_once __DIR__ . '/../../../preload.php';
// test case in rector split package
} elseif (file_exists(__DIR__ . '/../../../../../../vendor')) {
require_once __DIR__ . '/../../../preload-split-package.php';
}
}

if (\file_exists(__DIR__ . '/../../../vendor/scoper-autoload.php')) {
require_once __DIR__ . '/../../../vendor/scoper-autoload.php';
}
$this->includePreloadFilesAndScoperAutoload();

$configFile = $this->provideConfigFilePath();
$this->bootFromConfigFiles([$configFile]);
Expand Down Expand Up @@ -131,6 +118,22 @@ protected function getFixtureTempDirectory(): string
return sys_get_temp_dir() . '/_temp_fixture_easy_testing';
}

private function includePreloadFilesAndScoperAutoload(): void
{
if (file_exists(__DIR__ . '/../../../preload.php')) {
if (file_exists(__DIR__ . '/../../../vendor')) {
require_once __DIR__ . '/../../../preload.php';
// test case in rector split package
} elseif (file_exists(__DIR__ . '/../../../../../../vendor')) {
require_once __DIR__ . '/../../../preload-split-package.php';
}
}

if (\file_exists(__DIR__ . '/../../../vendor/scoper-autoload.php')) {
require_once __DIR__ . '/../../../vendor/scoper-autoload.php';
}
}

private function doTestFileMatchesExpectedContent(
SmartFileInfo $originalFileInfo,
SmartFileInfo $expectedFileInfo,
Expand All @@ -146,10 +149,8 @@ private function doTestFileMatchesExpectedContent(
return;
}

$relativeFilePathFromCwd = $fixtureFileInfo->getRelativeFilePathFromCwd();

try {
$this->assertStringEqualsFile($expectedFileInfo->getRealPath(), $changedContent, $relativeFilePathFromCwd);
$this->assertStringEqualsFile($expectedFileInfo->getRealPath(), $changedContent);
} catch (ExpectationFailedException $expectationFailedException) {
if (! $allowMatches) {
throw $expectationFailedException;
Expand All @@ -162,7 +163,7 @@ private function doTestFileMatchesExpectedContent(
$contents = $this->normalizeNewlines($contents);

// if not exact match, check the regex version (useful for generated hashes/uuids in the code)
$this->assertStringMatchesFormat($contents, $changedContent, $relativeFilePathFromCwd);
$this->assertStringMatchesFormat($contents, $changedContent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public function testUsesFromProperty(SmartFileInfo $file): void
*/
public function provideData(): Iterator
{
$directory = __DIR__ . '/Fixture';
return StaticFixtureFinder::yieldDirectoryExclusively($directory);
return StaticFixtureFinder::yieldDirectoryExclusively(__DIR__ . '/Fixture');
}
}
18 changes: 13 additions & 5 deletions src/FileSystem/FilePathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Core\FileSystem;

use Symfony\Component\Filesystem\Filesystem;
use Webmozart\Assert\Assert;

final class FilePathHelper
{
Expand All @@ -15,14 +16,21 @@ public function __construct(

public function relativePath(string $fileRealPath): string
{
return $this->relativeFilePathFromDirectory($fileRealPath, getcwd());
}

/**
* @api
*/
public function relativeFilePathFromDirectory(string $fileRealPath, string $directory): string
{
Assert::directory($directory);

$normalizedFileRealPath = $this->normalizePath($fileRealPath);

$relativeFilePath = $this->filesystem->makePathRelative(
$normalizedFileRealPath,
(string) realpath(getcwd())
);
$relativeFilePath = $this->filesystem->makePathRelative($normalizedFileRealPath, $directory);

return \rtrim($relativeFilePath, '/');
return rtrim($relativeFilePath, '/');
}

private function normalizePath(string $filePath): string
Expand Down
4 changes: 2 additions & 2 deletions src/NonPhpFile/Rector/RenameClassNonPhpRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function configure(array $configuration): void
*/
private function renameClasses(string $newContent, array $classRenames): string
{
$classRenames = $this->addDoubleSlahed($classRenames);
$classRenames = $this->addDoubleSlashed($classRenames);

foreach ($classRenames as $oldClass => $newClass) {
// the old class is without slashes, it can make mess as similar to a word in the text, so we have to be more strict about it
Expand All @@ -109,7 +109,7 @@ private function renameClasses(string $newContent, array $classRenames): string
* @param array<string, string> $classRenames
* @return array<string, string>
*/
private function addDoubleSlahed(array $classRenames): array
private function addDoubleSlashed(array $classRenames): array
{
foreach ($classRenames as $oldClass => $newClass) {
// to prevent no slash override
Expand Down

0 comments on commit b01ea04

Please sign in to comment.