Skip to content

[PHP 7.4] Add @var removal to TypedPropertyRector#2532

Merged
TomasVotruba merged 5 commits intomasterfrom
remove-doc-typed-properties
Dec 30, 2019
Merged

[PHP 7.4] Add @var removal to TypedPropertyRector#2532
TomasVotruba merged 5 commits intomasterfrom
remove-doc-typed-properties

Conversation

@TomasVotruba
Copy link
Copy Markdown
Member

@TomasVotruba TomasVotruba commented Dec 30, 2019

Closes #2367 propperly

Before

 /**
  * @var int
  */
-public $count;
+public int $count;

Now

-/**
- * @var int
- */
-public $count;
+public int $count;

/cc @brendt @enumag @collapso @mallardduck @ruudk

@TomasVotruba TomasVotruba force-pushed the remove-doc-typed-properties branch from 1671d1c to 892a438 Compare December 30, 2019 14:21
@ruudk
Copy link
Copy Markdown
Contributor

ruudk commented Dec 30, 2019

Too bad that I did the work already in PHP-CS-Fixer/PHP-CS-Fixer#4713... Any idea how I can get that merged?

@TomasVotruba
Copy link
Copy Markdown
Member Author

TomasVotruba commented Dec 30, 2019

@ruudk That's ok, both tools have their user scope. I know many people who use CS only and many more who use Rector only.

Also, you give me next piece of motivation to make this happen. In older version of Rector this would be really complicatd. But with new API around php doc it was quite easy. So thank you as well 👍

Any idea how I can get that merged?

No idea, I don't contribute there.

@TomasVotruba TomasVotruba force-pushed the remove-doc-typed-properties branch from 892a438 to d8bc1da Compare December 30, 2019 14:42
@TomasVotruba
Copy link
Copy Markdown
Member Author

@ruudk Btw, next time you can contribute here first. I do merge almost any feature improvements.

@ruudk
Copy link
Copy Markdown
Contributor

ruudk commented Dec 30, 2019

It was more that you said in the other issue it belongs in CS fixer, that’s why I did the work there , and that’s why I was surprised by this PR. But in the end, it does not matter, great to have this automatically fixable by either CS fixer or Rector ☺️

@TomasVotruba
Copy link
Copy Markdown
Member Author

TomasVotruba commented Dec 30, 2019

At first yes, because it seemed as extra work not worth doing for me. Working PR with test is hard to reject :)

I do change my mind as a learning process, based on feedback and given data.

Comment thread packages/Php74/src/Rector/Property/TypedPropertyRector.php
@TomasVotruba TomasVotruba requested a review from enumag December 30, 2019 17:04
Comment thread packages/Php74/src/Rector/Property/TypedPropertyRector.php
// remove extra prefix
$typeName = ltrim($typeName, '\\');

if (in_array($typeName, ['array', 'iterable', 'Traversable'], true)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What exactly is this condition for? There are more types like these - Iterator, Generator, ext-ds types, Doctrine collections, some SPL types... trying to white-list them for some special handling doesn't make much sense to me.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

These all need to be listed there then. For phpdoc-parser it's just a string.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There is no way to whitelist everything. What is the purpose of this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Only those that have type in object fo PHPStan.

The mapPHPStanPhpDocTypeNodeToPHPStanType() method converts doc type to phpstan type

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If you're able to make it simpler and more robust, that would be great

Copy link
Copy Markdown
Contributor

@enumag enumag Dec 30, 2019

Choose a reason for hiding this comment

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

Only those that have type in object fo PHPStan.

Can you rephrase that? I don't understand...

Would something like === array || === iterable || is_subclass_of(Traversable) work?

Copy link
Copy Markdown
Member Author

@TomasVotruba TomasVotruba Dec 30, 2019

Choose a reason for hiding this comment

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

Can you rephrase that? I don't understand...

Sure, this is phpdoc-paser doc type:

@var int

To use in the Rector/PHPStan, we need to convert it to PHPStan type:

$integerType = new IntegerType();

That's all :)

Would something like === array || === iterable || is_subclass_of(Traversable) work?

Hard to tell, testing needs to be done, as this is 3rd party code we convert

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Let's address in new PR

@TomasVotruba
Copy link
Copy Markdown
Member Author

Thanks for the feedback!

Feel free to add more failing cases in new issues. Merging as it is, so it can be used out in the wild

@TomasVotruba TomasVotruba merged commit f4bfaeb into master Dec 30, 2019
@TomasVotruba TomasVotruba deleted the remove-doc-typed-properties branch December 30, 2019 19:04
@ruudk
Copy link
Copy Markdown
Contributor

ruudk commented Dec 30, 2019

I just pulled in master to test this out, but this rector only removes the superfluous PHPDoc when the property does not have a type but does have a phpdoc:

-    /**
-     * @var Currency
-     */
-    public $fromCurrency;
+    public Currency $fromCurrency;

but it ignores this code:

    /**
     * @var Currency
     */
    public Currency $toCurrency;

Since I already ran the TypedPropertyRector before on my code, all my properties now have types, with phpdocs. Any idea how to solve that with Rector?

@enumag
Copy link
Copy Markdown
Contributor

enumag commented Dec 30, 2019

@ruudk I'd recommend solving that with php-cs-fixer (with your patch).

@TomasVotruba
Copy link
Copy Markdown
Member Author

If you send PR to add this feature, I'd be happy to merge it

TomasVotruba added a commit that referenced this pull request Jun 19, 2022
rectorphp/rector-src@88e17ec [PHP 7.0] skip non-existing method in StaticCallOnNonStaticToInstanceCallRector (#2532)
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.

TypedPropertyRector does not remove docblocks

3 participants