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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ uuid-migration.json
# compiler
/compiler/composer.lock
/compiler/vendor
/tmp
/tmp

# from "scan-fatal-errors" command
rector-types.yaml
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ jobs:
name: Rector
script:
- composer rector
-
php: 7.3
name: "Scan Fatal Errors"
script:
- bin/rector scan-fatal-errors tests/Source/FatalErrors

-
name: Documentation
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,6 @@ parameters:
-
message: '#Method Rector\\Symfony\\Rector\\FrameworkBundle\\AbstractToConstructorInjectionRector\:\:getServiceTypeFromMethodCallArgument\(\) should return PHPStan\\Type\\Type but returns PHPStan\\Type\\Type\|null#'
path: packages/Symfony/src/Rector/FrameworkBundle/AbstractToConstructorInjectionRector.php

- '#Parameter \#1 \$error_handler of function set_error_handler expects \(callable\(int, string, string, int, array\)\: bool\)\|null, Closure\(int, string\)\: void given#'
- '#Parameter \#1 \$source of method Rector\\Scan\\ErrorScanner\:\:scanSource\(\) expects array<string\>, array<string\>\|string\|null given#'
64 changes: 0 additions & 64 deletions rector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ parameters:
exclude_paths:
- "/Fixture/"
- "/Expected/"
- "/Source/"
- "packages/Symfony/src/Bridge/DefaultAnalyzedSymfonyApplicationContainer.php"
- "src/Testing/PHPUnit/AbstractRectorTestCase.php"
- "packages/Php/tests/Rector/Name/ReservedObjectRector/*"
Expand All @@ -15,66 +14,3 @@ parameters:

# so Rector code is still PHP 7.2 compatible
php_version_features: '7.2'

rector_recipe:
# run "bin/rector create" to create a new Rector + tests from this config
package: "NetteToSymfony"
name: "FormControlToControllerAndFormTypeRector"
node_types:
# put main node first, it is used to create namespace
- "Class_"
description: "Change Form that extends Control to Controller and decoupled FormType"
code_before: |
<?php

use Nette\Application\UI\Form;
use Nette\Application\UI\Control;

class SomeForm extends Control
{
public function createComponentForm()
{
$form = new Form();
$form->addText('name', 'Your name');

$form->onSuccess[] = [$this, 'processForm'];
}

public function processForm(Form $form)
{
// process me
}
}

code_after: |
<?php

class SomeFormController extends AbstractController
{
/**
* @Route(...)
*/
public function actionSomeForm(Request $request): Response
{
$form = $this->createForm(SomeFormType::class);
$form->handleRequest($request);

if ($form->isSuccess() && $form->isValid()) {
// process me
}
}
}

class SomeFormType extends \Symfony\Component\Form\AbstractType
{
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options)
{
$builder->add('name', \Symfony\Component\Form\Extension\Core\Type\TextType::class, [
'label' => 'Your name'
]);
}
}

source: # e.g. link to RFC or headline in upgrade guide, 1 or more in the list
- "https://symfony.com/doc/current/forms.html#creating-form-classes"
set: "nette-to-symfony" # e.g. symfony30, target config to append this rector to
5 changes: 5 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ final class Option
* @var string
*/
public const SYMFONY_CONTAINER_XML_PATH_PARAMETER = 'symfony_container_xml_path';

/**
* @var string
*/
public const DUMP = 'dump';
}
108 changes: 108 additions & 0 deletions src/Console/Command/ScanFatalErrorsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Rector\Console\Command;

use Rector\Configuration\Option;
use Rector\Console\Shell;
use Rector\Scan\ErrorScanner;
use Rector\Scan\ScannedErrorToRectorResolver;
use Rector\Yaml\YamlPrinter;
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 Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\Console\ShellCode;

final class ScanFatalErrorsCommand extends AbstractCommand
{
/**
* @var string
*/
private const RECTOR_TYPES_YAML = 'rector-types.yaml';

/**
* @var SymfonyStyle
*/
private $symfonyStyle;

/**
* @var ScannedErrorToRectorResolver
*/
private $scannedErrorToRectorResolver;

/**
* @var ErrorScanner
*/
private $errorScanner;

/**
* @var YamlPrinter
*/
private $yamlPrinter;

public function __construct(
SymfonyStyle $symfonyStyle,
ScannedErrorToRectorResolver $scannedErrorToRectorResolver,
ErrorScanner $errorScanner,
YamlPrinter $yamlPrinter
) {
$this->symfonyStyle = $symfonyStyle;
$this->scannedErrorToRectorResolver = $scannedErrorToRectorResolver;
$this->errorScanner = $errorScanner;
$this->yamlPrinter = $yamlPrinter;

parent::__construct();
}

protected function configure(): void
{
$this->setName(CommandNaming::classToName(self::class));

$this->setDescription('Scan for fatal type errors and dumps config that fixes it');

$this->addArgument(
Option::SOURCE,
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
'Path to file/directory to process'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var string[] $source */
$source = $input->getArgument(Option::SOURCE);
$errors = $this->errorScanner->scanSource($source);

if ($errors === []) {
$this->symfonyStyle->success('No fatal errors found');
return ShellCode::SUCCESS;
}

$this->symfonyStyle->section(sprintf('Found %d Errors', count($errors)));
foreach ($errors as $error) {
$this->symfonyStyle->note($error);
}

$rectorConfiguration = $this->scannedErrorToRectorResolver->processErrors($errors);
if ($rectorConfiguration === []) {
$this->symfonyStyle->success('No fatal errors found');
return ShellCode::SUCCESS;
}

$this->yamlPrinter->printYamlToFile($rectorConfiguration, self::RECTOR_TYPES_YAML);

$this->symfonyStyle->note(sprintf('New config with types was created in "%s"', self::RECTOR_TYPES_YAML));

$this->symfonyStyle->success(sprintf(
'Now run Rector to refactor your code:%svendor/bin/rector p %s --config %s',
PHP_EOL . PHP_EOL,
implode(' ', $source),
self::RECTOR_TYPES_YAML
));

return Shell::CODE_SUCCESS;
}
}
20 changes: 15 additions & 5 deletions src/Console/Command/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use Rector\Console\Shell;
use Rector\Contract\Rector\RectorInterface;
use Rector\Php\TypeAnalyzer;
use Rector\Yaml\YamlPrinter;
use ReflectionClass;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Yaml\Yaml;
use Symplify\PackageBuilder\Console\Command\CommandNaming;

final class ShowCommand extends AbstractCommand
Expand All @@ -31,16 +31,26 @@ final class ShowCommand extends AbstractCommand
*/
private $typeAnalyzer;

/**
* @var YamlPrinter
*/
private $yamlPrinter;

/**
* @param RectorInterface[] $rectors
*/
public function __construct(SymfonyStyle $symfonyStyle, array $rectors, TypeAnalyzer $typeAnalyzer)
{
public function __construct(
SymfonyStyle $symfonyStyle,
array $rectors,
TypeAnalyzer $typeAnalyzer,
YamlPrinter $yamlPrinter
) {
$this->symfonyStyle = $symfonyStyle;
$this->rectors = $rectors;
$this->typeAnalyzer = $typeAnalyzer;
$this->yamlPrinter = $yamlPrinter;

parent::__construct();
$this->typeAnalyzer = $typeAnalyzer;
}

protected function configure(): void
Expand All @@ -60,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
continue;
}

$configurationYamlContent = Yaml::dump($configuration, 10, 4, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
$configurationYamlContent = $this->yamlPrinter->printYamlToString($configuration);

$lines = explode(PHP_EOL, $configurationYamlContent);
$indentedContent = ' ' . implode(PHP_EOL . ' ', $lines);
Expand Down
6 changes: 4 additions & 2 deletions src/FileSystem/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ public function findInDirectoriesAndFiles(array $source, array $suffixes, bool $
if ($matchDiff) {
$gitDiffFiles = $this->getGitDiff();

$smartFileInfos = array_filter($smartFileInfos, function ($splFile) use ($gitDiffFiles): bool {
return in_array($splFile->getRealPath(), $gitDiffFiles, true);
$smartFileInfos = array_filter($smartFileInfos, function (SmartFileInfo $fileInfo) use (
$gitDiffFiles
): bool {
return in_array($fileInfo->getRealPath(), $gitDiffFiles, true);
});

$smartFileInfos = array_values($smartFileInfos);
Expand Down
Loading