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

Add PhpdocFullyQualifiedTypesFixer #3644

Closed
wants to merge 11 commits into from

Conversation

Slamdunk
Copy link
Contributor

@Slamdunk Slamdunk commented Mar 28, 2018

Reference: #3276
What about fixing PHPDoc too?

 use Foo\Bar;

 /**
- * @param \Foo\Bar $foo
+ * @param Bar $foo
  */
 function foo($foo) {}

Ping @veewee

@keradus
Copy link
Member

keradus commented Mar 28, 2018

3276 was about changing FQCN to short CN, why doing the reverse ?

@Slamdunk
Copy link
Contributor Author

3276 was about changing FQCN to short CN, why doing the reverse ?

The topic reports the Expected VS Actual diff of the test, nothing to reverse here.

@veewee
Copy link
Contributor

veewee commented Mar 28, 2018

@Slamdunk : that could indeed be a new feature. Currently I did not touch any docblocks. Personally, I am removing these kind of hard to maintain @param docblocks since they do not make any sense when using strict types anymore.

@Slamdunk
Copy link
Contributor Author

Personally, I am removing these kind of hard to maintain @param docblocks since they do not make any sense when using strict types anymore.

Heh, if only we could...

that could indeed be a new feature. Currently I did not touch any docblocks.

I pinged you because #3276 seems a complex work and maybe you faces some issue I need to know before building this new feature.

@veewee
Copy link
Contributor

veewee commented Mar 28, 2018

Didn't face any issues regarding to the doblocks.
The fixer itself isn't that complex, the hard part was abstracting the logic into generic analyzers.
It should be fairly simple to detect the function docblock and change the param / return tags.

You could add a fixFunctionDocblocks() method underneeth this line:
https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/2.11/src/Fixer/Import/FullyQualifiedStrictTypesFixer.php#L97

The way I think it should work:

  • Search for previous docblock token and make sure it is the doblock of current function.
  • Parse param and return tags. (There is a class for that)
  • Split up the method detectAndReplaceTypeWithShortType() in detectShortType() and replaceFqcnWithShortType() or something like that
  • Reuse the detectShortType to replace the param / return tags
  • replace old docblocks with new docblocks
  • party like it's 1999

@keradus
Copy link
Member

keradus commented Mar 28, 2018

The topic reports the Expected VS Actual diff of the test, nothing to reverse here.

I treated your opening post as feature request definition, not as output of failing test :D

little 👍 for the feature, but indeed, it's better to move it to prototype if possible, but that would be harder, so probably it's worth to do easier feature first.

@Slamdunk
Copy link
Contributor Author

Slamdunk commented Mar 29, 2018

I treated your opening post as feature request definition, not as output of failing test :D

You are right, it was misleading. I do so for bug reports, but it is wrong for new feature .

it's better to move it to prototype if possible

I don't understand what you mean

@ostrolucky
Copy link
Contributor

ostrolucky commented Mar 30, 2018

This should be different fixer, in Phpdoc namespace - to have better ability to deal with priority conflicts. FullyQualifiedStrictTypesFixer is wrong name for such a thing.

@julienfalque
Copy link
Member

👍 for this. I'm fine if the current fixer also deals with PHPDocs rather than creating a new fixer.

@ostrolucky
Copy link
Contributor

For others looking for this, there is Symplify\CodingStandard\Fixer\Import\ImportNamespacedNameFixer, which does this and additionally imports the statement if it wasn't imported yet

@Slamdunk Slamdunk changed the title FullyQualifiedStrictTypesFixer: fix PHPDoc too Add PhpdocFullyQualifiesTypesFixer Jan 29, 2020
public function getPriority()
{
/*
* Should be run before all other docblock fixers apart from the
Copy link
Contributor

Choose a reason for hiding this comment

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

is this a copy-paste-comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes , I've started from PhpdocScalarFixer and it seemed to me a good idea to retain part of it.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, than we should back up this comments claim with priority test fixtures so we no longer need this comment 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, I've added all the priority tests that I've could imaging, and then had to delete most of them except two cause of a test allowing only meaningful priority tests 😃

Copy link
Contributor

Choose a reason for hiding this comment

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

That is my concern 😃 so a lot PHPDoc fixer claim that they must run before a whole bunch of other fixers, yet creating a priority test to prove it seems not possible.

return $type;
}

return (new TypeShortNameResolver())->resolve($tokens, $type);
Copy link
Contributor

Choose a reason for hiding this comment

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

is there nice way to prevent creating a new instance of new TypeShortNameResolver() for each candidate? (maybe private property of the fixer)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TypeShortNameResolver is a stateless class, and I've read few times @julienfalque to prefer not saving anything privately due to performance reason.

Moreover, FullyQualifiedStrictTypesFixer do the same:

https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/c8afb599858876e95e8ebfcd97812d383fa23f02/src/Fixer/Import/FullyQualifiedStrictTypesFixer.php#L155

Copy link
Contributor

Choose a reason for hiding this comment

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

this is no longer true

@SpacePossum SpacePossum added this to the 2.17.0 milestone Jan 29, 2020
@julienfalque julienfalque changed the title Add PhpdocFullyQualifiesTypesFixer Add PhpdocFullyQualifiedTypesFixer Jan 30, 2020
@Slamdunk
Copy link
Contributor Author

Could someone please fix the PR label to kind/feature?

@keradus keradus modified the milestones: 2.17.0, 2.17.1, 2.18.0 Dec 7, 2020
@keradus keradus removed this from the 2.18.0 milestone Jan 18, 2021
@Wirone Wirone added the status/to verify issue needs to be confirmed or analysed to continue label Jun 6, 2023
@Wirone Wirone added topic/phpdoc PHPDoc tags, Doctrine Annotations etc. topic/fqcn Fully Qualified Class Name usage and conversions and removed status/to verify issue needs to be confirmed or analysed to continue labels Jun 17, 2023
@Wirone Wirone added the status/rebase required PR is outdated and required synchronisation with main branch label Jul 2, 2023
use Foo\Bar;

/**
* @return null|int|Bar
Copy link
Contributor

Choose a reason for hiding this comment

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

add some more complex phpdoc types like nested type, generic, array shape, conditional type

}

// Dummy start and end indexes, we only need isReservedType result
if ((new TypeAnalysis(substr($type, 1), 1, 2))->isReservedType()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@Wirone
Copy link
Member

Wirone commented Jan 9, 2024

Actually it was implemented in #7622 so we can close this 🙂. Thanks @Slamdunk for your work!

@Wirone Wirone closed this Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature status/rebase required PR is outdated and required synchronisation with main branch topic/fqcn Fully Qualified Class Name usage and conversions topic/phpdoc PHPDoc tags, Doctrine Annotations etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants