Updated Rector to commit b01ea044294135d9ae9546939d66b8181af50940

b01ea04429 [DX] Make use of FilePathHelper over inner magic of FileSystem in SmartFileInfo (#2862)
This commit is contained in:
Tomas Votruba 2022-08-30 15:03:28 +00:00
parent f251f578f6
commit eb540c7f2a
9 changed files with 66 additions and 39 deletions

View File

@ -5,6 +5,7 @@ namespace Rector\ChangesReporting\ValueObjectFactory;
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
@ -14,13 +15,20 @@ final class ErrorFactory
* @var \Rector\Core\Error\ExceptionCorrector
*/
private $exceptionCorrector;
public function __construct(ExceptionCorrector $exceptionCorrector)
/**
* @readonly
* @var \Rector\Core\FileSystem\FilePathHelper
*/
private $filePathHelper;
public function __construct(ExceptionCorrector $exceptionCorrector, FilePathHelper $filePathHelper)
{
$this->exceptionCorrector = $exceptionCorrector;
$this->filePathHelper = $filePathHelper;
}
public function createAutoloadError(AnalysedCodeException $analysedCodeException, 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);
}
}

View File

@ -8,9 +8,9 @@ use PhpParser\Node\Expr\StaticCall;
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
{
/**
@ -27,10 +27,16 @@ final class InvalidNameNodeReporter
* @var \Rector\Core\Contract\PhpParser\NodePrinterInterface
*/
private $nodePrinter;
public function __construct(CurrentFileProvider $currentFileProvider, NodePrinterInterface $nodePrinter)
/**
* @readonly
* @var \Rector\Core\FileSystem\FilePathHelper
*/
private $filePathHelper;
public function __construct(CurrentFileProvider $currentFileProvider, NodePrinterInterface $nodePrinter, FilePathHelper $filePathHelper)
{
$this->currentFileProvider = $currentFileProvider;
$this->nodePrinter = $nodePrinter;
$this->filePathHelper = $filePathHelper;
}
/**
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $node
@ -42,15 +48,17 @@ final class InvalidNameNodeReporter
if ($file instanceof File) {
$smartFileInfo = $file->getSmartFileInfo();
$message .= \PHP_EOL . \PHP_EOL;
$message .= \sprintf('Caused in "%s" file on line %d on code "%s"', $smartFileInfo->getRelativeFilePathFromCwd(), $node->getStartLine(), $this->nodePrinter->print($node));
$relatilveFilePath = $this->filePathHelper->relativePath($smartFileInfo->getRealPath());
$message .= \sprintf('Caused in "%s" file on line %d on code "%s"', $relatilveFilePath, $node->getStartLine(), $this->nodePrinter->print($node));
}
$backtrace = \debug_backtrace();
$rectorBacktrace = $this->matchRectorBacktraceCall($backtrace);
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'];
}

View File

@ -53,18 +53,7 @@ abstract class AbstractRectorTestCase extends \Rector\Testing\PHPUnit\AbstractTe
{
// 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]);
$this->applicationFileProcessor = $this->getService(ApplicationFileProcessor::class);
@ -115,6 +104,20 @@ abstract class AbstractRectorTestCase extends \Rector\Testing\PHPUnit\AbstractTe
{
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, SmartFileInfo $fixtureFileInfo, bool $allowMatches = \true) : void
{
$this->parameterProvider->changeParameter(Option::SOURCE, [$originalFileInfo->getRealPath()]);
@ -123,9 +126,8 @@ abstract class AbstractRectorTestCase extends \Rector\Testing\PHPUnit\AbstractTe
if ($this->removedAndAddedFilesCollector->isFileRemoved($originalFileInfo)) {
return;
}
$relativeFilePathFromCwd = $fixtureFileInfo->getRelativeFilePathFromCwd();
try {
$this->assertStringEqualsFile($expectedFileInfo->getRealPath(), $changedContent, $relativeFilePathFromCwd);
$this->assertStringEqualsFile($expectedFileInfo->getRealPath(), $changedContent);
} catch (ExpectationFailedException $expectationFailedException) {
if (!$allowMatches) {
throw $expectationFailedException;
@ -135,7 +137,7 @@ abstract class AbstractRectorTestCase extends \Rector\Testing\PHPUnit\AbstractTe
// make sure we don't get a diff in which every line is different (because of differences in EOL)
$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);
}
}
private function normalizeNewlines(string $string) : string

View File

@ -17,12 +17,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'bb3ced496fe923594d4af178fdf61328dd731608';
public const PACKAGE_VERSION = 'b01ea044294135d9ae9546939d66b8181af50940';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2022-08-30 14:14:36';
public const RELEASE_DATE = '2022-08-30 14:59:35';
/**
* @var int
*/

View File

@ -4,6 +4,7 @@ declare (strict_types=1);
namespace Rector\Core\FileSystem;
use RectorPrefix202208\Symfony\Component\Filesystem\Filesystem;
use RectorPrefix202208\Webmozart\Assert\Assert;
final class FilePathHelper
{
/**
@ -17,8 +18,16 @@ final class FilePathHelper
}
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, '/');
}
private function normalizePath(string $filePath) : string

View File

@ -71,7 +71,7 @@ CODE_SAMPLE
*/
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
$oldClassRegex = $this->createOldClassRegex($oldClass);
@ -87,7 +87,7 @@ CODE_SAMPLE
* @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

2
vendor/autoload.php vendored
View File

@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit559da89fc514d48ba131cb908f0738e7::getLoader();
return ComposerAutoloaderInitedf27f069b71e894f79831b06b417de6::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit559da89fc514d48ba131cb908f0738e7
class ComposerAutoloaderInitedf27f069b71e894f79831b06b417de6
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit559da89fc514d48ba131cb908f0738e7
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit559da89fc514d48ba131cb908f0738e7', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitedf27f069b71e894f79831b06b417de6', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit559da89fc514d48ba131cb908f0738e7', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitedf27f069b71e894f79831b06b417de6', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit559da89fc514d48ba131cb908f0738e7::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitedf27f069b71e894f79831b06b417de6::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit559da89fc514d48ba131cb908f0738e7::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInitedf27f069b71e894f79831b06b417de6::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire559da89fc514d48ba131cb908f0738e7($fileIdentifier, $file);
composerRequireedf27f069b71e894f79831b06b417de6($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit559da89fc514d48ba131cb908f0738e7
* @param string $file
* @return void
*/
function composerRequire559da89fc514d48ba131cb908f0738e7($fileIdentifier, $file)
function composerRequireedf27f069b71e894f79831b06b417de6($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit559da89fc514d48ba131cb908f0738e7
class ComposerStaticInitedf27f069b71e894f79831b06b417de6
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -3192,9 +3192,9 @@ class ComposerStaticInit559da89fc514d48ba131cb908f0738e7
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit559da89fc514d48ba131cb908f0738e7::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit559da89fc514d48ba131cb908f0738e7::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit559da89fc514d48ba131cb908f0738e7::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitedf27f069b71e894f79831b06b417de6::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitedf27f069b71e894f79831b06b417de6::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitedf27f069b71e894f79831b06b417de6::$classMap;
}, null, ClassLoader::class);
}