make RectorServiceParametersShifter report better error in case of invalid configuration

This commit is contained in:
Tomas Votruba 2019-09-26 13:18:48 +02:00
parent e2a8659b2c
commit fcb865767a
6 changed files with 44 additions and 11 deletions

View File

@ -4,6 +4,7 @@ use Nette\Utils\Strings;
use Rector\Set\SetProvider;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Symplify\PackageBuilder\Console\ShellCode;
require __DIR__ . '/../vendor/autoload.php';
@ -11,10 +12,20 @@ $setProvider = new SetProvider();
// any file to be tested
$file = 'src/Rector/AbstractRector.php';
$excludedSets = [
// required Kernel class to be set in parameters
'symfony-code-quality'
];
$errors = [];
foreach ($setProvider->provide() as $setName) {
if (in_array($setName, $excludedSets, true)) {
continue;
}
$command = ['php', 'bin/rector', 'process', $file, '--set', $setName, '--dry-run'];
$process = new Process($command, __DIR__ . '/..');
echo sprintf('Set "%s" is OK' . PHP_EOL, $setName);
@ -25,7 +36,17 @@ foreach ($setProvider->provide() as $setName) {
continue;
}
echo $processFailedException->getMessage();
exit(1);
$errors[] = $processFailedException->getMessage();
}
}
if ($errors === []) {
exit(ShellCode::SUCCESS);
}
foreach ($errors as $error) {
echo $error;
echo PHP_EOL;
}
exit(ShellCode::ERROR);

View File

@ -32,8 +32,7 @@ services:
Rector\Celebrity\Rector\FuncCall\SetTypeToCastRector: ~
# class { var $value; } → class { public $value; }
Rector\Php52\Rector\Property\VarToPublicPropertyRector: ~pu
Rector\Php52\Rector\Property\VarToPublicPropertyRector: ~
# false or true → false || true
# false and true → false && true

View File

@ -38,8 +38,9 @@ services:
addConfig: load
Rector\Rector\Interface_\RemoveInterfacesRector:
# Removes "implements IPresenter"
- Nette\Application\IPresenter
$interfacesToRemove:
# Removes "implements IPresenter"
- Nette\Application\IPresenter
Rector\Renaming\Rector\Constant\RenameClassConstantRector:
Nette\Http\*Response:

View File

@ -18,8 +18,11 @@ final class RenameTesterTestToPHPUnitToTestFileRector extends AbstractFileSystem
public function refactor(SmartFileInfo $smartFileInfo): void
{
$oldRealPath = $smartFileInfo->getRealPath();
$newRealPath = $this->createNewRealPath($oldRealPath);
if (! Strings::endsWith($oldRealPath, '*.phpt')) {
return;
}
$newRealPath = $this->createNewRealPath($oldRealPath);
if ($newRealPath === $oldRealPath) {
return;
}

View File

@ -4,6 +4,7 @@ namespace Rector\DependencyInjection\Loader;
use Nette\Utils\Strings;
use Rector\Exception\Configuration\InvalidConfigurationException;
use Rector\Exception\ShouldNotHappenException;
use ReflectionClass;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Yaml\Yaml;
@ -43,13 +44,13 @@ final class RectorServiceParametersShifter
* @param mixed[] $configuration
* @return mixed[]
*/
public function process(array $configuration): array
public function process(array $configuration, string $file): array
{
if (! isset($configuration[self::SERVICES_KEY]) || ! is_array($configuration[self::SERVICES_KEY])) {
return $configuration;
}
$configuration[self::SERVICES_KEY] = $this->processServices($configuration[self::SERVICES_KEY]);
$configuration[self::SERVICES_KEY] = $this->processServices($configuration[self::SERVICES_KEY], $file);
return $configuration;
}
@ -58,13 +59,21 @@ final class RectorServiceParametersShifter
* @param mixed[] $services
* @return mixed[]
*/
private function processServices(array $services): array
private function processServices(array $services, string $file): array
{
foreach ($services as $serviceName => $serviceDefinition) {
if (! $this->isRectorClass($serviceName) || empty($serviceDefinition)) {
continue;
}
if (! is_array($serviceDefinition)) {
throw new ShouldNotHappenException(sprintf(
'Rector rule "%s" has invalid configuration in "%s". Fix it to an array',
$serviceName,
$file
));
}
$nonReservedNonVariables = $this->resolveRectorConfiguration($serviceDefinition);
// nothing to change

View File

@ -47,7 +47,7 @@ final class TolerantRectorYamlFileLoader extends AbstractParameterMergingYamlFil
$this->classExistenceValidator->ensureClassesAreValid($configuration, $file);
$configuration = $this->rectorServiceParametersShifter->process($configuration);
$configuration = $this->rectorServiceParametersShifter->process($configuration, $file);
return $this->parameterInImportResolver->process($configuration);
}