8000 [Configuration] Fix root standalone registered rules verify on RectorConfigBuilder by samsonasik · Pull Request #6840 · rectorphp/rector-src · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[Configuration] Fix root standalone registered rules verify on RectorConfigBuilder #6840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8000
Merged
merged 6 commits into from
Apr 16, 2025
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
10 changes: 10 additions & 0 deletions src/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,18 @@ final class RectorConfig extends Container
*/
private array $autotagInterfaces = [Command::class, ResetableInterface::class];

private static ?bool $recreated = null;

public static function configure(): RectorConfigBuilder
{
if (self::$recreated === null) {
self::$recreated = false;
} elseif (self::$recreated === false) {
self::$recreated = true;
}

SimpleParameterProvider::setParameter(Option::IS_RECTORCONFIG_BUILDER_RECREATED, self::$recreated);

return new RectorConfigBuilder();
}

Expand Down
6 changes: 6 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ final class Option
*/
public const REGISTERED_RECTOR_SETS = 'registered_rector_sets';

/**
* @internal For verify RectorConfigBuilder instance recreated
* @var string
*/
public const IS_RECTORCONFIG_BUILDER_RECREATED = 'is_rectorconfig_builder_recreated';

/**
* @internal For verify skipped rules exists in registered rules
* @var string
Expand Down
17 changes: 10 additions & 7 deletions src/Configuration/RectorConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,13 +788,16 @@ public function withRules(array $rules): self
{
$this->rules = array_merge($this->rules, $rules);

// log all explicitly registered rules
// we only check the non-configurable rules, as the configurable ones might override them
$nonConfigurableRules = array_filter(
$rules,
fn (string $rule): bool => ! is_a($rule, ConfigurableRectorInterface::class, true)
);
SimpleParameterProvider::addParameter(Option::ROOT_STANDALONE_REGISTERED_RULES, $nonConfigurableRules);
if (SimpleParameterProvider::provideBoolParameter(Option::IS_RECTORCONFIG_BUILDER_RECREATED, false) === false) {
// log all explicitly registered rules on root rector.php
// we only check the non-configurable rules, as the configurable ones might override them
$nonConfigurableRules = array_filter(
$rules,
fn (string $rule): bool => ! is_a($rule, ConfigurableRectorInterface::class, true)
);

SimpleParameterProvider::addParameter(Option::ROOT_STANDALONE_REGISTERED_RULES, $nonConfigurableRules);
}

return $this;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/Config/RectorConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Config;

use Rector\Config\RectorConfig;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Symfony\Set\TwigSetList;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector;

final class RectorConfigTest extends AbstractLazyTestCase
{
public function test(): void
{
RectorConfig::configure()
->withSets([
TwigSetList::TWIG_134
])
->withRules([
ReturnTypeFromReturnNewRector::class
])(new RectorConfig());

// only collect root withRules()
$this->assertCount(1, SimpleParameterProvider::provideArrayParameter(Option::ROOT_STANDALONE_REGISTERED_RULES));
}
}
0