Register RectorConfig in container and set static instance to ensure singleton instance of RectorConfig#7888
Register RectorConfig in container and set static instance to ensure singleton instance of RectorConfig#7888guanguans wants to merge 1 commit intorectorphp:mainfrom
Conversation
guanguans
commented
Feb 11, 2026
- Ensure singleton instance of RectorConfig - Make RectorConfig accessible during bootstrap - Call setInstance and bind RectorConfig::class to same instance - Avoid missing or duplicate config when resolving services
There was a problem hiding this comment.
Pull request overview
This PR fixes an issue where custom Rector rules that inject RectorConfig via constructor were receiving an empty instance instead of the properly configured container. The fix ensures that the RectorConfig instance created in LazyContainerFactory::create() is registered both as a static singleton and as a container instance.
Changes:
- Register RectorConfig as static singleton using
setInstance()to enable global access viaContainer::getInstance() - Register RectorConfig in the container itself using
instance()to enable dependency injection
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Why you need it? |
|
@samsonasik I want to get the container instance more flexible to handle something, unrelated to the <?php
use PhpParser\Node;
use Rector\Config\RectorConfig;
use Rector\Rector\AbstractRector;
abstract class AbstractProxyRector extends AbstractRector
{
protected AbstractRector $proxyRector;
/**
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function __construct(RectorConfig $rectorConfig)
{
// RectorConfig::getInstance();
$this->proxyRector = clone $rectorConfig->make($this->proxyRectorClass());
}
/**
* {@inheritDoc}
*/
public function refactor(Node $node)
{
// ...
}
/**
* @return class-string<\Rector\Rector\AbstractRector>
*/
abstract protected function proxyRectorClass(): string;
} |
|
RectorConfig is exclusively internal. Injecting container to rule is a bad practice. |