Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ script:
bin/rector process src --set symfony40 --dry-run
composer docs
php bin/check_services_in_yaml_configs.php
php bin/run_all_sets.php
fi

# Eat your own dog food
Expand Down
52 changes: 52 additions & 0 deletions bin/run_all_sets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types=1);

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';

$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);

try {
$process->mustRun();
} catch (ProcessFailedException $processFailedException) {
if (! Strings::match($processFailedException->getMessage(), '#(Fatal error)|(\[ERROR\])#')) {
continue;
}

$errors[] = $processFailedException->getMessage();
}
}

if ($errors === []) {
exit(ShellCode::SUCCESS);
}

foreach ($errors as $error) {
echo $error;
echo PHP_EOL;
}

exit(ShellCode::ERROR);
3 changes: 1 addition & 2 deletions config/set/celebrity/celebrity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ~
Copy link
Copy Markdown
Member Author

@TomasVotruba TomasVotruba Sep 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gnutix This was the issue :D slow PHPStorm + my hasty hands to run pu as phpunit

Copy link
Copy Markdown
Contributor

@gnutix gnutix Sep 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahahah that's a nice one xD Too bad it's still "valid YAML"... can't catch this with a linter.
I confirm that disabling the celebrity import works.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed :D

But now it's not valid for the configuration - it will throw exception here now: https://github.com/rectorphp/rector/pull/2043/files#diff-4b356c6550ab7f23c4b4d17595032977R69


# false or true → false || true
# false and true → false && true
Expand Down
3 changes: 2 additions & 1 deletion config/set/easy-corp/easy-admin-bundle20.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
services:
# first replace ->get("...") by constructor injection in every child of "EasyCorp\Bundle\EasyAdminBundle\AdminController"
Rector\Symfony\Rector\FrameworkBundle\GetToConstructorInjectionRector:
EasyCorp\Bundle\EasyAdminBundle\AdminController: ~
$getMethodAwareTypes:
- 'EasyCorp\Bundle\EasyAdminBundle\AdminController'

# then rename the "EasyCorp\Bundle\EasyAdminBundle\AdminController" class
Rector\Renaming\Rector\Class_\RenameClassRector:
Expand Down
5 changes: 3 additions & 2 deletions config/set/framework-migration/nette-to-symfony.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ public function getDefinition(): RectorDefinition
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;
}
Expand Down
25 changes: 25 additions & 0 deletions packages/NodeTypeResolver/src/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ public function __construct(
*/
public function isObjectType(Node $node, $requiredType): bool
{
$this->ensureRequiredTypeIsStringOrObjectType($requiredType, 'isObjectType');

if (is_string($requiredType)) {
if (Strings::contains($requiredType, '*')) {
return $this->isFnMatch($node, $requiredType);
Expand Down Expand Up @@ -699,4 +701,27 @@ private function unionWithParentClassesInterfacesAndUsedTraits(Type $type): Type

return $type;
}

/**
* @param mixed $requiredType
*/
private function ensureRequiredTypeIsStringOrObjectType($requiredType, string $location): void
{
if (is_string($requiredType)) {
return;
}

if ($requiredType instanceof ObjectType) {
return;
}

$reportedType = is_object($requiredType) ? get_class($requiredType) : $requiredType;

throw new ShouldNotHappenException(sprintf(
'Value passed to "%s()" must be string or "%s". "%s" given',
$location,
ObjectType::class,
$reportedType
));
}
}
30 changes: 9 additions & 21 deletions src/Console/Command/SetsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use Nette\Utils\Strings;
use Rector\Console\Shell;
use Rector\Set\SetProvider;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symplify\PackageBuilder\Console\Command\CommandNaming;

final class SetsCommand extends AbstractCommand
Expand All @@ -18,9 +18,15 @@ final class SetsCommand extends AbstractCommand
*/
private $symfonyStyle;

public function __construct(SymfonyStyle $symfonyStyle)
/**
* @var SetProvider
*/
private $setProvider;

public function __construct(SymfonyStyle $symfonyStyle, SetProvider $setProvider)
{
$this->symfonyStyle = $symfonyStyle;
$this->setProvider = $setProvider;

parent::__construct();
}
Expand All @@ -34,7 +40,7 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$sets = $this->getAvailbleSets();
$sets = $this->setProvider->provide();

if ($input->getArgument('name')) {
$sets = $this->filterSetsByName($input, $sets);
Expand All @@ -46,24 +52,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Shell::CODE_SUCCESS;
}

/**
* @return string[]
*/
private function getAvailbleSets(): array
{
$finder = Finder::create()->files()
->in(__DIR__ . '/../../../config/set');

$sets = [];
foreach ($finder->getIterator() as $fileInfo) {
$sets[] = $fileInfo->getBasename('.' . $fileInfo->getExtension());
}

sort($sets);

return array_unique($sets);
}

/**
* @param string[] $sets
* @return string[]
Expand Down
15 changes: 12 additions & 3 deletions src/DependencyInjection/Loader/RectorServiceParametersShifter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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;
Expand Down Expand Up @@ -43,13 +44,13 @@ public function __construct()
* @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;
}
Expand All @@ -58,13 +59,21 @@ public function process(array $configuration): array
* @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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function loadFile($file)

$this->classExistenceValidator->ensureClassesAreValid($configuration, $file);

$configuration = $this->rectorServiceParametersShifter->process($configuration);
$configuration = $this->rectorServiceParametersShifter->process($configuration, $file);

return $this->parameterInImportResolver->process($configuration);
}
Expand Down
26 changes: 26 additions & 0 deletions src/Set/SetProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace Rector\Set;

use Symfony\Component\Finder\Finder;

final class SetProvider
{
/**
* @return string[]
*/
public function provide(): array
{
$finder = Finder::create()->files()
->in(__DIR__ . '/../../config/set');

$sets = [];
foreach ($finder->getIterator() as $fileInfo) {
$sets[] = $fileInfo->getBasename('.' . $fileInfo->getExtension());
}

sort($sets);

return array_unique($sets);
}
}