make use of new set from symplify

This commit is contained in:
TomasVotruba 2020-07-21 13:42:17 +02:00
parent 77ff617767
commit 2eb21e1087
14 changed files with 224 additions and 250 deletions

View File

@ -12,6 +12,7 @@ use Rector\Core\Configuration\RectorConfigsResolver;
use Rector\Core\Console\Application;
use Rector\Core\Console\Style\SymfonyStyleFactory;
use Rector\Core\DependencyInjection\RectorContainerFactory;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\PackageBuilder\Reflection\PrivatesCaller;
@ini_set('memory_limit', '-1'); // @ intentionally: continue anyway
@ -47,23 +48,23 @@ try {
/** @var Configuration $configuration */
$configuration = $container->get(Configuration::class);
$configuration->setFirstResolverConfig($rectorConfigsResolver->getFirstResolvedConfig());
$configuration->setFirstResolverConfigFileInfo($rectorConfigsResolver->getFirstResolvedConfig());
if ($rectorConfigsResolver->getFirstResolvedConfig()) {
/** @var ChangedFilesDetector $changedFilesDetector */
$changedFilesDetector = $container->get(ChangedFilesDetector::class);
$changedFilesDetector->setFirstUsedConfig(realpath($rectorConfigsResolver->getFirstResolvedConfig()));
$changedFilesDetector->setFirstResolvedConfigFileInfo($rectorConfigsResolver->getFirstResolvedConfig());
}
} catch (Throwable $throwable) {
$symfonyStyle = (new SymfonyStyleFactory(new PrivatesCaller()))->create();
$symfonyStyle->error($throwable->getMessage());
exit(1);
exit(ShellCode::ERROR);
}
$application = $container->get(Application::class);
exit($application->run());
final class AutoloadIncluder
{
/**

View File

@ -29,12 +29,15 @@
"symfony/dependency-injection": "^4.4.8|^5.0.6",
"symfony/finder": "^4.4.8|^5.0.6",
"symfony/process": "^4.4.8|^5.0.6",
"symplify/auto-bind-parameter": "^8.1.9",
"symplify/autowire-array-parameter": "^8.1.9",
"symplify/console-color-diff": "^8.1.9",
"symplify/package-builder": "^8.1.9",
"symplify/parameter-name-guard": "^8.1.9",
"symplify/set-config-resolver": "^8.1.9",
"symplify/auto-bind-parameter": "dev-master",
"symplify/autowire-array-parameter": "dev-master",
"symplify/console-color-diff": "dev-master",
"symplify/package-builder": "dev-master",
"symplify/parameter-name-guard": "dev-master",
"symplify/set-config-resolver": "dev-master",
"symplify/composer-json-manipulator": "dev-master",
"symplify/smart-file-system": "dev-master",
"symplify/coding-standard": "dev-master",
"tracy/tracy": "^2.7",
"phpstan/phpstan": "0.12.32"
},
@ -47,11 +50,11 @@
"psr/event-dispatcher": "^1.0",
"slam/phpstan-extensions": "^5.0",
"slevomat/coding-standard": "dev-master",
"symplify/changelog-linker": "^8.1.9",
"symplify/easy-coding-standard": "^8.1.9",
"symplify/easy-testing": "^8.1.9",
"symplify/monorepo-builder": "^8.1.9",
"symplify/phpstan-extensions": "^8.1.9",
"symplify/changelog-linker": "dev-master",
"symplify/easy-coding-standard": "dev-master",
"symplify/easy-testing": "dev-master",
"symplify/monorepo-builder": "dev-master",
"symplify/phpstan-extensions": "dev-master",
"thecodingmachine/phpstan-strict-rules": "^0.12"
},
"autoload": {

View File

@ -15,7 +15,13 @@ use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import(__DIR__ . '/nette-to-symfony/*.php');
$containerConfigurator->import(__DIR__ . '/nette-to-symfony-doctrine.php');
$containerConfigurator->import(__DIR__ . '/nette-control-to-symfony-controller.php');
$containerConfigurator->import(__DIR__ . '/nette-tester-to-phpunit.php');
$containerConfigurator->import(__DIR__ . '/kdyby-to-symfony.php');
$services = $containerConfigurator->services();

View File

@ -101,11 +101,11 @@ final class ChangedFilesDetector
/**
* @api
*/
public function setFirstUsedConfig(string $firstConfig): void
public function setFirstResolvedConfigFileInfo(SmartFileInfo $fileInfo): void
{
// the first config is core to all → if it was changed, just invalidate it
$configHash = $this->fileHashComputer->compute($firstConfig);
$this->storeConfigurationDataHash($firstConfig, $configHash);
$configHash = $this->fileHashComputer->compute($fileInfo);
$this->storeConfigurationDataHash($fileInfo, $configHash);
}
private function getFileInfoCacheKey(SmartFileInfo $smartFileInfo): string
@ -126,9 +126,9 @@ final class ChangedFilesDetector
$this->tagAwareAdapter->save($cacheItem);
}
private function storeConfigurationDataHash(string $configPath, string $configurationHash): void
private function storeConfigurationDataHash(SmartFileInfo $fileInfo, string $configurationHash): void
{
$key = self::CONFIGURATION_HASH_KEY . '_' . Strings::webalize($configPath);
$key = self::CONFIGURATION_HASH_KEY . '_' . Strings::webalize($fileInfo->getRealPath());
$this->invalidateCacheIfConfigurationChanged($key, $configurationHash);

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Rector\Caching\Config;
use Nette\Utils\Strings;
use Rector\Core\Exception\ShouldNotHappenException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
@ -13,33 +12,34 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symplify\SmartFileSystem\SmartFileInfo;
/**
* Inspired by https://github.com/symplify/easy-coding-standard/blob/e598ab54686e416788f28fcfe007fd08e0f371d9/packages/changed-files-detector/src/FileHashComputer.php
*/
final class FileHashComputer
{
public function compute(string $filePath): string
public function compute(SmartFileInfo $fileInfo): string
{
$this->ensureIsYamlOrPhp($filePath);
$this->ensureIsYamlOrPhp($fileInfo);
$containerBuilder = new ContainerBuilder();
$fileLoader = $this->createFileLoader($filePath, $containerBuilder);
$fileLoader = $this->createFileLoader($fileInfo, $containerBuilder);
$fileLoader->load($filePath);
$fileLoader->load($fileInfo->getRealPath());
return $this->arrayToHash($containerBuilder->getDefinitions()) .
$this->arrayToHash($containerBuilder->getParameterBag()->all());
}
private function ensureIsYamlOrPhp(string $filePath): void
private function ensureIsYamlOrPhp(SmartFileInfo $fileInfo): void
{
if (Strings::match($filePath, '#\.(yml|yaml|php)$#')) {
if ($fileInfo->hasSuffixes(['yml', 'yaml', 'php'])) {
return;
}
throw new ShouldNotHappenException(sprintf(
'Provide only YAML/PHP file, ready for Symfony Dependency Injection. "%s" given', $filePath
'Provide only YAML/PHP file, ready for Symfony Dependency Injection. "%s" given', $fileInfo
));
}
@ -51,16 +51,16 @@ final class FileHashComputer
return md5(serialize($array));
}
private function createFileLoader(string $filePath, ContainerBuilder $containerBuilder): LoaderInterface
private function createFileLoader(SmartFileInfo $fileInfo, ContainerBuilder $containerBuilder): LoaderInterface
{
$fileLocator = new FileLocator([dirname($filePath)]);
$fileLocator = new FileLocator([$fileInfo->getPath()]);
$loaderResolver = new LoaderResolver([
new GlobFileLoader($containerBuilder, $fileLocator),
new PhpFileLoader($containerBuilder, $fileLocator),
new YamlFileLoader($containerBuilder, $fileLocator),
]);
$loader = $loaderResolver->resolve($filePath);
$loader = $loaderResolver->resolve($fileInfo->getRealPath());
if (! $loader) {
throw new ShouldNotHappenException();
}

View File

@ -9,6 +9,7 @@ use Rector\Caching\Config\FileHashComputer;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\HttpKernel\RectorKernel;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class FileHashComputerTest extends AbstractKernelTestCase
{
@ -28,8 +29,8 @@ final class FileHashComputerTest extends AbstractKernelTestCase
*/
public function testHashIsIdentical(string $firstConfig, string $secondConfig): void
{
$configAHash = $this->fileHashComputer->compute($firstConfig);
$configBHash = $this->fileHashComputer->compute($secondConfig);
$configAHash = $this->fileHashComputer->compute(new SmartFileInfo($firstConfig));
$configBHash = $this->fileHashComputer->compute(new SmartFileInfo($secondConfig));
$this->assertSame($configAHash, $configBHash);
}
@ -43,6 +44,6 @@ final class FileHashComputerTest extends AbstractKernelTestCase
public function testInvalidType(): void
{
$this->expectException(ShouldNotHappenException::class);
$this->fileHashComputer->compute(__DIR__ . '/Source/file.xml');
$this->fileHashComputer->compute(new SmartFileInfo(__DIR__ . '/Source/file.xml'));
}
}

View File

@ -0,0 +1 @@
some xml

View File

@ -6,7 +6,7 @@ namespace Rector\RectorGenerator\ValueObject;
use Nette\Utils\Strings;
use Rector\Core\Util\StaticRectorStrings;
use Rector\Set\ValueObject\Set;
use Symplify\SetConfigResolver\ValueObject\Set;
final class Configuration
{
@ -164,7 +164,7 @@ final class Configuration
return null;
}
return $this->set->getFileRealPath();
return $this->set->getSetFileInfo()->getRealPath();
}
public function isPhpSnippet(): bool

View File

@ -4,16 +4,21 @@ declare(strict_types=1);
namespace Rector\Set;
use Rector\Set\ValueObject\Set;
use Rector\Set\ValueObject\SetList;
use Symplify\SetConfigResolver\Provider\AbstractSetProvider;
use Symplify\SetConfigResolver\ValueObject\Set;
use Symplify\SmartFileSystem\SmartFileInfo;
final class SetProvider
final class SetProvider extends AbstractSetProvider
{
/**
* @var array<string, string>
* @var Set[]
*/
private const SET_PATH_TO_FILEPATH = [
private $sets = [];
public function __construct()
{
$setNamesToSetPaths = [
SetList::ACTION_INJECTION_TO_CONSTRUCTOR_INJECTION => __DIR__ . '/../../../config/set/action-injection-to-constructor-injection.php',
SetList::ARRAY_STR_FUNCTIONS_TO_STATIC_CALL => __DIR__ . '/../../../config/set/laravel-array-str-functions-to-static-call.php',
SetList::CAKEPHP_30 => __DIR__ . '/../../../config/set/cakephp30.php',
@ -155,33 +160,16 @@ final class SetProvider
SetList::UNWRAP_COMPAT => __DIR__ . '/../../../config/set/unwrap-compat.php',
];
public function __construct()
{
$this->validateSetFilePaths();
foreach ($setNamesToSetPaths as $setName => $setPath) {
$this->sets[] = new Set($setName, new SmartFileInfo($setPath));
}
}
/**
* @return string[]
* @return Set[]
*/
public function provideSetNames(): array
public function provide(): array
{
return array_keys(self::SET_PATH_TO_FILEPATH);
}
public function provideFilePathByName(string $setName): ?Set
{
if (! isset(self::SET_PATH_TO_FILEPATH[$setName])) {
return null;
}
$setFilePath = self::SET_PATH_TO_FILEPATH[$setName];
return new Set($setFilePath);
}
private function validateSetFilePaths(): void
{
foreach (self::SET_PATH_TO_FILEPATH as $setFilePath) {
new SmartFileInfo($setFilePath);
}
return $this->sets;
}
}

View File

@ -1,30 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Set\ValueObject;
use Symplify\SmartFileSystem\SmartFileInfo;
final class Set
{
/**
* @var SmartFileInfo
*/
private $fileInfo;
public function __construct(string $filePath)
{
$this->fileInfo = new SmartFileInfo($filePath);
}
public function getFileRealPath(): string
{
return $this->fileInfo->getRealPath();
}
public function getFileInfo(): SmartFileInfo
{
return $this->fileInfo;
}
}

View File

@ -22,9 +22,9 @@ final class Configuration
private $isDryRun = false;
/**
* @var string|null
* @var SmartFileInfo|null
*/
private $configFilePath;
private $configFileInfo;
/**
* @var bool
@ -153,14 +153,18 @@ final class Configuration
/**
* @api
*/
public function setFirstResolverConfig(?string $firstResolvedConfig): void
public function setFirstResolverConfigFileInfo(?SmartFileInfo $firstResolvedConfigFileInfo): void
{
$this->configFilePath = $firstResolvedConfig;
$this->configFileInfo = $firstResolvedConfigFileInfo;
}
public function getConfigFilePath(): ?string
{
return $this->configFilePath;
if ($this->configFileInfo === null) {
return null;
}
return $this->configFileInfo->getRealPath();
}
public function getPrettyVersion(): string

View File

@ -33,9 +33,9 @@ final class RectorConfigsResolver
/**
* @noRector
*/
public function getFirstResolvedConfig(): ?string
public function getFirstResolvedConfig(): ?SmartFileInfo
{
return $this->configResolver->getFirstResolvedConfig();
return $this->configResolver->getFirstResolvedConfigFileInfo();
}
/**
@ -69,17 +69,17 @@ final class RectorConfigsResolver
$set = $this->setResolver->resolveSetFromInput($argvInput);
if ($set !== null) {
$configFileInfos[] = $set->getFileInfo();
$configFileInfos[] = $set->getSetFileInfo();
}
// And from --config or default one
$inputOrFallbackConfig = $this->configResolver->resolveFromInputWithFallback(
$inputOrFallbackConfigFileInfo = $this->configResolver->resolveFromInputWithFallback(
$argvInput,
['rector.php', 'rector.yml', 'rector.yaml']
);
if ($inputOrFallbackConfig !== null) {
$configFileInfos[] = new SmartFileInfo($inputOrFallbackConfig);
if ($inputOrFallbackConfigFileInfo !== null) {
$configFileInfos[] = $inputOrFallbackConfigFileInfo;
}
$setFileInfos = $this->resolveSetFileInfosFromConfigFileInfos($configFileInfos);

View File

@ -6,8 +6,8 @@ namespace Rector\Core\Set;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Set\SetProvider;
use Rector\Set\ValueObject\Set;
use Symfony\Component\Console\Input\InputInterface;
use Symplify\SetConfigResolver\ValueObject\Set;
use Symplify\SmartFileSystem\SmartFileInfo;
final class SetResolver
@ -29,21 +29,21 @@ final class SetResolver
return null;
}
return $this->setProvider->provideFilePathByName($setOption);
return $this->setProvider->provideByName($setOption);
}
public function resolveSetByName(string $name): ?Set
{
return $this->setProvider->provideFilePathByName($name);
return $this->setProvider->provideByName($name);
}
public function resolveSetFileInfoByName(string $name): SmartFileInfo
{
$set = $this->setProvider->provideFilePathByName($name);
$set = $this->setProvider->provideByName($name);
if ($set === null) {
throw new ShouldNotHappenException();
}
return $set->getFileInfo();
return $set->getSetFileInfo();
}
}

View File

@ -41,13 +41,13 @@ abstract class AbstractGenericRectorTestCase extends AbstractKernelTestCase
{
if ($this->provideSet() !== '') {
$setProvider = new SetProvider();
$set = $setProvider->provideFilePathByName($this->provideSet());
$set = $setProvider->provideByName($this->provideSet());
if ($set === null) {
$message = sprintf('Invalid set name provided "%s"', $this->provideSet());
throw new ShouldNotHappenException($message);
}
return $set->getFileInfo();
return $set->getSetFileInfo();
}
// can be implemented