Skip to content
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

[DX] Make PhpDocInfoFactory explicitly required in Rector rule constructor, if needed #5051

Merged
merged 2 commits into from
Sep 20, 2023

Conversation

TomasVotruba
Copy link
Member

@TomasVotruba TomasVotruba commented Sep 20, 2023

AbstractRector is quite heavy class with "handy" dependencies. At least that was the original idea :)

Now it contains ~15 services just in case they're used in child service:

public function autowire(
NodeNameResolver $nodeNameResolver,
NodeTypeResolver $nodeTypeResolver,
SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
NodeFactory $nodeFactory,
PhpDocInfoFactory $phpDocInfoFactory,
StaticTypeMapper $staticTypeMapper,
Skipper $skipper,
ValueResolver $valueResolver,
BetterNodeFinder $betterNodeFinder,
NodeComparator $nodeComparator,
CurrentFileProvider $currentFileProvider,
CreatedByRuleDecorator $createdByRuleDecorator,
ChangedNodeScopeRefresher $changedNodeScopeRefresher,
): void {
$this->nodeNameResolver = $nodeNameResolver;
$this->nodeTypeResolver = $nodeTypeResolver;
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
$this->nodeFactory = $nodeFactory;
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->staticTypeMapper = $staticTypeMapper;
$this->skipper = $skipper;
$this->valueResolver = $valueResolver;
$this->betterNodeFinder = $betterNodeFinder;
$this->nodeComparator = $nodeComparator;
$this->currentFileProvider = $currentFileProvider;
$this->createdByRuleDecorator = $createdByRuleDecorator;
$this->changedNodeScopeRefresher = $changedNodeScopeRefresher;
}

This design doesn't seem smart anymore and only forces each rule to decide if they use global dependency or local one. It's like having Doctrine serivce in every Controller.

Instead, there should be zero global dependencies (unless used in the AbstractRector itself) and each rule should ask only what they need. Goal is to make design from ambiguous to single dependency injection, and lighten all the rules.

@TomasVotruba TomasVotruba changed the title tv less autowire [dx] Make PhpDocInfoFactory explicitly required in Rector rule constructor, if needed Sep 20, 2023
@TomasVotruba TomasVotruba changed the title [dx] Make PhpDocInfoFactory explicitly required in Rector rule constructor, if needed [DX] Make PhpDocInfoFactory explicitly required in Rector rule constructor, if needed Sep 20, 2023
@TomasVotruba TomasVotruba force-pushed the tv-less-autowire branch 8 times, most recently from 46ec435 to b66b567 Compare September 20, 2023 12:35
@TomasVotruba TomasVotruba enabled auto-merge (squash) September 20, 2023 12:35
* Handle deprecated dependencies compatbility
*/
public function __get(string $name): mixed
{
Copy link
Member

Choose a reason for hiding this comment

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

I think this should has soft warning:

        if (! property_exists($this, $name) && isset($this->deprecatedDependencies[$name])) {
            $this->symfonyStyle->warning(
                sprintf('pull %s property from AbstractRector is deprecated, inject via __construct() instead', $name)
            );
       }

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea 👍 Could you add it?

Copy link
Member

Choose a reason for hiding this comment

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

I wil try 👍

Copy link
Member

Choose a reason for hiding this comment

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

adding warning on __get() seems make too many verbose in console:

➜  rector-src git:(main) ✗ bin/rector process src 
   0/146 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░]   0%

pull phpDocInfoFactory property from AbstractRector on class Rector\PHPUnit\CodeQuality\Rector\Class_\AddSeeTestAnnotationRector is deprecated, inject via __construct() instead
pull phpDocInfoFactory property from AbstractRector on class Rector\PHPUnit\CodeQuality\Rector\Class_\AddSeeTestAnnotationRector is deprecated, inject via __construct() instead
pull phpDocInfoFactory property from AbstractRector on class Rector\PHPUnit\CodeQuality\Rector\Class_\AddSeeTestAnnotationRector is deprecated, inject via __construct() instead
pull phpDocInfoFactory property from AbstractRector on class Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\TicketAnnotationToAttributeRector is deprecated, inject via __construct() instead
pull phpDocInfoFactory property from AbstractRector on class Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\AnnotationWithValueToAttributeRector is deprecated, inject via __construct() instead

if next release is 0.19, then I think the __get() itself is unnecessary, then add release note instead.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, message seems can be cached, see #5069

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants