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 CombineNestedDirnameFixer #3826

Merged
merged 1 commit into from Aug 22, 2018
Merged

Conversation

gharlan
Copy link
Contributor

@gharlan gharlan commented Jun 9, 2018

Input:

dirname(dirname(dirname($path)));

is fixed to:

dirname($path, 3);

(Requires PHP >= 7.0)

@gharlan gharlan changed the title Combine dirname Add CombineDirnameFixer Jun 9, 2018
@gharlan gharlan force-pushed the combine-dirname branch 5 times, most recently from 1714ae7 to 49f009a Compare June 10, 2018 09:19
* @param int $index Index of `dirname`
* @param null|int $firstArgumentEndIndex Index of last token of first argument of `dirname` call
*
* @return array|bool `false` when it is not a (suppoerted) `dirname` call, an array with infos about the dirname call otherwise
Copy link
Member

Choose a reason for hiding this comment

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

Typo: suppoerted => supported. Also info (information) is uncountable in english and cannot have an s.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed it.

* @param null|string $input
*
* @dataProvider provideFixCases
* @requires PHP 7.0
Copy link
Member

Choose a reason for hiding this comment

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

Please add PHP <7.0 tests showing that the fixer does nothing.

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

),
],
null,
'Risky when dirname() is overridden.'
Copy link
Contributor

@SpacePossum SpacePossum Jun 10, 2018

Choose a reason for hiding this comment

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

please use `dirname`

@gharlan
Copy link
Contributor Author

gharlan commented Jun 10, 2018

Should this be added to the @PHP70Migration:risky ruleset?

@julienfalque
Copy link
Member

Indeed, I would add it.

@SpacePossum SpacePossum added this to the 2.13.0 milestone Jun 22, 2018
@SpacePossum
Copy link
Contributor

@julienfalque are happy with changes?

@kubawerlos
Copy link
Contributor

@gharlan please, add a test case:

            [
                '<?php new dirname(dirname($path,2));',
                '<?php new dirname(dirname(dirname($path)));',
            ],

You may want to take a look at FunctionsAnalyzer::isGlobalFunctionCall.

@gharlan
Copy link
Contributor Author

gharlan commented Jul 12, 2018

@kubawerlos good catch 👍 I will fix it.

@staabm
Copy link
Contributor

staabm commented Jul 14, 2018

Really cool. Didnt know this $level parameter of dirname.

README.rst Outdated
@@ -390,6 +390,13 @@ Choose from the list of available rules:

Calling ``unset`` on multiple items should be done in one call.

* **combine_dirname** [@PHP70Migration:risky, @PHP71Migration:risky]
Copy link
Member

Choose a reason for hiding this comment

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

combine dirname with... ?
combine_multiple_dirname or combine_nested_dirname ?

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 for combine_nested_dirname

@gharlan gharlan changed the title Add CombineDirnameFixer Add CombineNestedDirnameFixer Jul 14, 2018
$this->doTest($expected, $input);
}

public function provideFixCases()
Copy link
Contributor

Choose a reason for hiding this comment

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

What about dirname(dirname($path, 2), 2)? Should that result in dirname($path, 4)? Can that be done as well? (I'd think so...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's already supported, I will add a test case for this.

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

@keradus keradus added the RTM Ready To Merge label Aug 22, 2018
@keradus keradus removed the RTM Ready To Merge label Aug 22, 2018
@keradus
Copy link
Member

keradus commented Aug 22, 2018

Thank you @gharlan.

@keradus keradus merged commit 398f343 into PHP-CS-Fixer:master Aug 22, 2018
keradus added a commit that referenced this pull request Aug 22, 2018
This PR was squashed before being merged into the 2.13-dev branch (closes #3826).

Discussion
----------

Add CombineNestedDirnameFixer

Input:

```php
dirname(dirname(dirname($path)));
```

is fixed to:

```php
dirname($path, 3);
```

(Requires PHP >= 7.0)

Commits
-------

398f343 Add CombineNestedDirnameFixer
@gharlan gharlan deleted the combine-dirname branch August 23, 2018 00:19
@SpacePossum
Copy link
Contributor

thanks @gharlan , great to see this in :)

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

8 participants