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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Why refactor manually if Rector can handle 80% for you?
[![Downloads](https://img.shields.io/packagist/dt/rector/rector.svg?style=flat-square)](https://packagist.org/packages/rector/rector)
[![SonarCube](https://img.shields.io/badge/SonarCube_Debt-%3C25-brightgreen.svg?style=flat-square)](https://sonarcloud.io/dashboard?id=rectorphp_rector)

- **[All 450+ Rectors Overview](/docs/AllRectorsOverview.md)

![Rector-showcase](docs/images/rector-showcase-var.gif)

<br>
Expand Down Expand Up @@ -135,7 +137,7 @@ parameters:

```yaml
services:
Rector\Rector\Architecture\DependencyInjection\AnnotatedPropertyInjectToConstructorInjectionRector:
Rector\Core\Rector\Architecture\DependencyInjection\AnnotatedPropertyInjectToConstructorInjectionRector
$annotation: "inject"
```

Expand Down Expand Up @@ -312,7 +314,7 @@ use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Expr\MethodCall;
use Rector\Core\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;

final class MyFirstRector extends AbstractRector
Expand Down
2 changes: 1 addition & 1 deletion compiler/bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

declare(strict_types = 1);

use Rector\Compiler\Composer\ComposerJsonManipulator;
use Rector\Compiler\Console\CompileCommand;
use Rector\Compiler\Console\ComposerJsonManipulator;
use Symfony\Component\Console\Application;
use Symfony\Component\Filesystem\Filesystem;

Expand Down
21 changes: 20 additions & 1 deletion docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 448 Rectors Overview
# All 449 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand Down Expand Up @@ -7368,6 +7368,25 @@ Replace constant by new ones

<br>

### `RenameFuncCallToStaticCallRector`

- class: `Rector\Renaming\Rector\FuncCall\RenameFuncCallToStaticCallRector`

Rename func call to static call

```diff
class SomeClass
{
public function run()
{
- strPee('...');
+ \Strings::strPee('...');
}
}
```

<br>

### `RenameFunctionRector`

- class: `Rector\Renaming\Rector\Function_\RenameFunctionRector`
Expand Down
2 changes: 2 additions & 0 deletions helpers/extract_class_changes_from_diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use Nette\Utils\Strings;
use Symfony\Component\Yaml\Yaml;

// @todo change to command

require __DIR__ . '/../vendor/autoload.php';

### CONFIGURE INPUT HERE
Expand Down
6 changes: 3 additions & 3 deletions packages/rector-generator/src/Command/CreateRectorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function addOneMoreRectorNesting(string $content): string
{
$content = Strings::replace($content, '#Rector\\\\Rector\\\\#ms', 'Rector\\');
$content = Strings::replace($content, '#Rector\\\\Rector\\\\#ms', 'Rector\\Core\\');

return Strings::replace(
$content,
'#use Rector\\\\AbstractRector;#',
'use Rector\\Rector\\AbstractRector;'
'use Rector\\Core\\Rector\\AbstractRector;'
);
}

Expand Down Expand Up @@ -201,7 +201,7 @@ private function printSuccess(string $name): void
$this->symfonyStyle->listing($this->generatedFiles);

$this->symfonyStyle->success(sprintf(
'Now make these tests green:%svendor/bin/phpunit %s',
'Make tests green again:%svendor/bin/phpunit %s',
PHP_EOL . PHP_EOL,
$this->testCasePath
));
Expand Down
50 changes: 50 additions & 0 deletions packages/rector-generator/src/ConfigResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Rector\RectorGenerator;

use Nette\Utils\Strings;
use Rector\Core\Set\Set;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

final class ConfigResolver
{
public function resolveSetConfig(string $set): ?string
{
if ($set === '') {
return null;
}

$fileInfos = $this->getSetFileInfos($set);

if (count($fileInfos) === 0) {
// assume new one is created
$match = Strings::match($set, '#\/(?<name>[a-zA-Z_-]+])#');
if (isset($match['name'])) {
return Set::SET_DIRECTORY . '/' . $match['name'] . '/' . $set;
}

return null;
}

/** @var SplFileInfo $foundSetConfigFileInfo */
$foundSetConfigFileInfo = array_pop($fileInfos);

return $foundSetConfigFileInfo->getRealPath();
}

/**
* @return SplFileInfo[]
*/
private function getSetFileInfos(string $set): array
{
$fileSet = sprintf('#^%s(\.yaml)?$#', $set);
$finder = Finder::create()->files()
->in(Set::SET_DIRECTORY)
->name($fileSet);

return iterator_to_array($finder->getIterator());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
use Nette\Utils\Strings;
use PhpParser\Node;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Set\Set;
use Rector\RectorGenerator\ConfigResolver;
use Rector\RectorGenerator\Guard\RecipeGuard;
use Rector\RectorGenerator\Node\NodeClassProvider;
use Rector\RectorGenerator\ValueObject\Configuration;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

final class ConfigurationFactory
{
Expand All @@ -26,10 +24,19 @@ final class ConfigurationFactory
*/
private $recipeGuard;

public function __construct(NodeClassProvider $nodeClassProvider, RecipeGuard $recipeGuard)
{
/**
* @var ConfigResolver
*/
private $configResolver;

public function __construct(
NodeClassProvider $nodeClassProvider,
RecipeGuard $recipeGuard,
ConfigResolver $configResolver
) {
$this->nodeClassProvider = $nodeClassProvider;
$this->recipeGuard = $recipeGuard;
$this->configResolver = $configResolver;
}

/**
Expand All @@ -53,7 +60,7 @@ public function createFromRectorRecipe(array $rectorRecipe): Configuration
$this->normalizeCode($rectorRecipe['extra_file_content'] ?? ''),
$rectorRecipe['extra_file_name'],
array_filter((array) $rectorRecipe['source']),
$this->resolveSetConfig($rectorRecipe['set']),
$this->configResolver->resolveSetConfig($rectorRecipe['set']),
$this->detectPhpSnippet($rectorRecipe['code_before'])
);
}
Expand Down Expand Up @@ -103,35 +110,6 @@ private function normalizeCode(string $code): string
return trim($code);
}

private function resolveSetConfig(string $set): ?string
{
if ($set === '') {
return null;
}

$fileSet = sprintf('#^%s(\.yaml)?$#', $set);
$finder = Finder::create()->files()
->in(Set::SET_DIRECTORY)
->name($fileSet);

/** @var SplFileInfo[] $fileInfos */
$fileInfos = iterator_to_array($finder->getIterator());
if (count($fileInfos) === 0) {
// assume new one is created
$match = Strings::match($set, '#\/(?<name>[a-zA-Z_-]+])#');
if (isset($match['name'])) {
return Set::SET_DIRECTORY . '/' . $match['name'] . '/' . $set;
}

return null;
}

/** @var SplFileInfo $foundSetConfigFileInfo */
$foundSetConfigFileInfo = array_pop($fileInfos);

return $foundSetConfigFileInfo->getRealPath();
}

private function detectPhpSnippet(string $code): bool
{
return Strings::startsWith($code, '<?php');
Expand Down
10 changes: 5 additions & 5 deletions packages/rector-generator/src/Finder/TemplateFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public function find(Configuration $configuration): array

if ($configuration->getExtraFileContent()) {
$smartFileInfos[] = new SmartFileInfo(
__DIR__ . '/../../templates/packages/_Package_/tests/Rector/_Category_/_Name_/Source/extra_file.php.inc'
__DIR__ . '/../../templates/rules/_package_/tests/Rector/_Category_/_Name_/Source/extra_file.php.inc'
);
$smartFileInfos[] = new SmartFileInfo(
__DIR__ . '/../../templates/packages/_Package_/tests/Rector/_Category_/_Name_/_Name_ExtraTest.php.inc'
__DIR__ . '/../../templates/rules/_package_/tests/Rector/_Category_/_Name_/_Name_ExtraTest.php.inc'
);
} else {
$smartFileInfos[] = new SmartFileInfo(
__DIR__ . '/../../templates/packages/_Package_/tests/Rector/_Category_/_Name_/_Name_Test.php.inc'
__DIR__ . '/../../templates/rules/_package_/tests/Rector/_Category_/_Name_/_Name_Test.php.inc'
);
}

Expand All @@ -61,13 +61,13 @@ private function createFixtureSmartFileInfo(bool $isPhpSnippet): SmartFileInfo
{
if ($isPhpSnippet) {
return new SmartFileInfo(
__DIR__ . '/../../templates/packages/_Package_/tests/Rector/_Category_/_Name_/Fixture/fixture.php.inc'
__DIR__ . '/../../templates/rules/_package_/tests/Rector/_Category_/_Name_/Fixture/fixture.php.inc'
);
}

// is html snippet
return new SmartFileInfo(
__DIR__ . '/../../templates/packages/_Package_/tests/Rector/_Category_/_Name_/Fixture/html_fixture.php.inc'
__DIR__ . '/../../templates/rules/_package_/tests/Rector/_Category_/_Name_/Fixture/html_fixture.php.inc'
);
}
}
1 change: 1 addition & 0 deletions packages/rector-generator/src/TemplateVariablesFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function createFromConfiguration(Configuration $configuration): array
{
$data = [
'_Package_' => $configuration->getPackage(),
'_package_' => strtolower($configuration->getPackage()),
'_Category_' => $configuration->getCategory(),
'_Description_' => $configuration->getDescription(),
'_Name_' => $configuration->getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Rector\_Package_\Rector\_Category_;

use PhpParser\Node;
use Rector\Core\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ declare(strict_types=1);

namespace Rector\_Package_\Tests\Rector\_Category_\_Name_;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;

final class _Name_ExtraTest extends AbstractRectorTestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ declare(strict_types=1);

namespace Rector\_Package_\Tests\Rector\_Category_\_Name_;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;

final class _Name_Test extends AbstractRectorTestCase
{
Expand Down
Loading