Skip to content

[Php74] Add inlinePublic configurable for TypedPropertyRector #1745

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

Merged
merged 9 commits into from
Feb 2, 2022

Conversation

samsonasik
Copy link
Member

@samsonasik samsonasik commented Jan 30, 2022

@TomasVotruba this is alternative PR for #1744, which add a new config isSafeTyped to TypedPropertyRector which default to true. If it set to false, with:

  $services->set(TypedPropertyRector::class)
             ->configure([false]);

It will change the following code:

final class SimpleArray
{
    /**
     * @var array
     */
    private $foo;

    /**
     * @var array
     */
    protected $bar;

    /**
     * @var array
     */
    public $baz;
}

to :

 final class SimpleArray
 {
-    /**
-     * @var array
-     */
-    private $foo;
+    private array $foo;
 
-    /**
-     * @var array
-     */
-    protected $bar;
+    protected array $bar;
 
-    /**
-     * @var array
-     */
-    public $baz;
+    public array $baz;

Closes #1744
Closes rectorphp/rector#6872
Closes #1746

@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba it is ready for review.

@TomasVotruba
Copy link
Member

We're trying to separate PHP declaration rules (https://github.com/rectorphp/rector-src/blob/main/config/set/type-declaration-strict.php) and doblock rules (https://github.com/rectorphp/rector-src/blob/main/config/set/type-declaration.php).

It would be better to have this as a separated rule in a docblock rules. Type declarations and docblocks have different priorities and can behave differently. The optional configuration of the rule can be easily overlooked.

That's why it should extracted in a separated rule level. E.g. InlineVarDocToTypedPropertyRector

Could you add that one?

@samsonasik
Copy link
Member Author

I will try.

@samsonasik
Copy link
Member Author

@TomasVotruba as existing TypedPropertyRector is changing based on @var

$varType = $this->varDocPropertyTypeInferer->inferProperty($node);

It seems will be duplicated/ambiguous while adding feature for protected and public, it seems we need to create 2 new rules instead:

  • InlineVarDocToTypedInPrivatePropertyRector
  • InlineVarDocToTypedPropertyRector

The InlineVarDocToTypedPropertyRector can extends the InlineVarDocToTypedInPrivatePropertyRector with override the shouldSkip() method.

After that, remove the TypedPropertyRector
After that, register InlineVarDocToTypedInPrivatePropertyRector to php74 service set config
After that, register InlineVarDocToTypedPropertyRector to type-declaration service set config
After that, remove TypedPropertyRector from list service in https://getrector.org/demo page,

what do you think?

@TomasVotruba
Copy link
Member

@samsonasik It seems pretty complicated, you're right.

Let's take this way then, with explicit option name:

  $services->set(TypedPropertyRector::class)
             ->configure([TypedPropertyRector::INLINE_PUBLIC => true]);

@samsonasik samsonasik force-pushed the alternative-typed-property branch from 0d37ab6 to 04ec582 Compare February 2, 2022 10:11
@samsonasik samsonasik changed the title [Php74] Add isSafeTyped configurable for TypedPropertyRector [Php74] Add inlinePublic configurable for TypedPropertyRector Feb 2, 2022
@samsonasik
Copy link
Member Author

@TomasVotruba ok, implemented with set INLINE_PUBLIC option, default to false, when enabled to true, it will appy change to public and protected as well when not forbidden.

@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba I think it is ready.

@TomasVotruba
Copy link
Member

Looks good, thank you 👍

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

Successfully merging this pull request may close these issues.

Add new namespaced rules under "BcBreak" namespace
3 participants