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

PhpdocNoEmptyReturnFixer - account for null[] #3891

Merged

Conversation

dmvdbrugge
Copy link
Contributor

@dmvdbrugge dmvdbrugge commented Jul 9, 2018

This fixes #3869

@@ -105,7 +105,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
*/
private function fixAnnotation(DocBlock $doc, Annotation $annotation)
{
if (1 === Preg::match('/@return\s+(void|null)(?!\|)/', $doc->getLine($annotation->getStart())->getContent())) {
if (1 === Preg::match('/@return\s+(void|null)(?!\||\[)/', $doc->getLine($annotation->getStart())->getContent())) {
Copy link
Member

Choose a reason for hiding this comment

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

maybe instead of negative lookahead, let us ensure there is whitespace character after null ? or use DocBlock component here and rely on logic that is already there...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There doesn't have to be a whitespace character, as discussed before on gitter. But you wanted me to fix that case as separate PR 😉

Copy link
Member

Choose a reason for hiding this comment

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

still, I believe we shall switch to DocBlock, and if issue exist fix it there, for all fixers, and not here, for single fixer

$expected = <<<'EOF'
<?php
/**
* @return null[]|string[]
Copy link
Contributor

Choose a reason for hiding this comment

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

could you add a test to make sure the change is case-insensitive, i.e. NULL[] and VOID[] get fixed as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, they didn't because Annotation::getTypes isn't normalized. Fix incoming.

@dmvdbrugge dmvdbrugge force-pushed the 3869-phpdoc_no_empty_return-less-greedy branch 2 times, most recently from 99a7880 to edf3e5e Compare July 16, 2018 12:15
*
* @dataProvider provideNormalizedTypesCases
*/
public function testNormalizedTypes($input, $expected)
Copy link
Contributor

Choose a reason for hiding this comment

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

please flip the arguments

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@dmvdbrugge dmvdbrugge force-pushed the 3869-phpdoc_no_empty_return-less-greedy branch from edf3e5e to 4fe7b78 Compare July 30, 2018 17:47
@@ -226,6 +226,15 @@ public function setTypes(array $types)
$this->clearCache();
}

public function getNormalizedTypes()
Copy link
Contributor

Choose a reason for hiding this comment

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

please add a PHPDoc with a return type hint, should be @return string[] I think

@@ -226,6 +226,15 @@ public function setTypes(array $types)
$this->clearCache();
}

public function getNormalizedTypes()
{
$normalized = array_map('strtolower', $this->getTypes());
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we want this refactored to not rely on the function name, but I'll leave that to a second reviewer :)

Copy link
Member

Choose a reason for hiding this comment

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

yes, we don't want to call functions by string with their name, as it's impossible to analyse with static code analysis
please use anonymous function as a wrapper here

Copy link
Contributor

Choose a reason for hiding this comment

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

Would be good if there were a fixer for that ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did it this way because I had seen that in NativeConstantInvocationFixer. Want me to fix it there as well, or just here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed here, let me know if you want me to fix the other one too.

- Use $annotation->getTypes() instead of Preg::match
- Apply yoda
- Use normalized types
- Add test for new method
- With test params in correct order
- Add phpdoc and use anonymous function instead of string
@dmvdbrugge dmvdbrugge force-pushed the 3869-phpdoc_no_empty_return-less-greedy branch from e04702a to 23084e9 Compare August 16, 2018 08:56
@keradus keradus added this to the 2.12.3 milestone Aug 19, 2018
@keradus keradus changed the title bug #3869 PhpdocNoEmptyReturnFixer - account for null[] PhpdocNoEmptyReturnFixer - account for null[] Aug 19, 2018
@keradus
Copy link
Member

keradus commented Aug 19, 2018

Thank you @dmvdbrugge.

@keradus keradus merged commit 23084e9 into PHP-CS-Fixer:2.12 Aug 19, 2018
keradus added a commit that referenced this pull request Aug 19, 2018
This PR was merged into the 2.12 branch.

Discussion
----------

PhpdocNoEmptyReturnFixer - account for null[]

This fixes #3869

Commits
-------

23084e9 PhpdocNoEmptyReturnFixer - account for null[]
@dmvdbrugge dmvdbrugge deleted the 3869-phpdoc_no_empty_return-less-greedy branch August 20, 2018 07:49
SpacePossum added a commit that referenced this pull request Apr 19, 2020
…r last annotation (dmvdbrugge)

This PR was merged into the 2.15 branch.

Discussion
----------

Fix handling /** and */ on the same line as the first and/or last annotation

In both `Annotation::remove()` and `PhpdocNoEmptyReturnFixer`

This PR is sort-of combined with #3891

Commits
-------

ad23563 Fix handling `/**` and `*/` on the same line as the first and/or last annotation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants