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 FopenFlagOrderFixer & FopenFlagsFixer #3812

Merged
merged 1 commit into from Aug 22, 2018
Merged

Add FopenFlagOrderFixer & FopenFlagsFixer #3812

merged 1 commit into from Aug 22, 2018

Conversation

SpacePossum
Copy link
Contributor

@SpacePossum SpacePossum commented Jun 5, 2018

closes #3417

ping @ntzm , hope you can help me out with:

  • define the order of all flags
  • do we want to have an option to remove flags when found, like t
  • do we want to have an option to add flags when not found, like b

@keradus
Copy link
Member

keradus commented Jun 7, 2018

do we want to have an option to remove flags when found, like t
do we want to have an option to add flags when not found, like b

That could be different fixer that would ensure there is always b and never t.
Splitting it into 2 options doesn't make sense to me - from manual:

For portability, it is strongly recommended that you always use the 'b' flag when opening files

@SpacePossum
Copy link
Contributor Author

ping @ntzm could you provide a string that shows all possible flags in the correct order? Than this fixer can be finished :)

@ntzm
Copy link
Contributor

ntzm commented Jun 22, 2018

Sorry missed your initial ping, will sort at the weekend

@SpacePossum SpacePossum added the status/input required For the issue to be confirmed or the PR to be process; additional input of the author is required label Jun 22, 2018
@ntzm
Copy link
Contributor

ntzm commented Jun 25, 2018

b should always be last (apart from +, e.g. wb, wb+), but the order of the others doesn't matter. Might be worth putting them in alphabetical order for uniformity.

@SpacePossum SpacePossum removed WIP status/input required For the issue to be confirmed or the PR to be process; additional input of the author is required labels Jun 25, 2018
@SpacePossum
Copy link
Contributor Author

thanks @ntzm :)
I think this one should be about finished than, let me know if anything is still open

@SpacePossum
Copy link
Contributor Author

@ntzm anything you would like me to change here?

$content = $tokens[$argumentFlagIndex]->getContent();
$contentQuote = $content[0]; // `'` or `"`

$content = substr($content, 1, -1);
Copy link
Contributor

Choose a reason for hiding this comment

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

How about binary strings? e.g. b'rw', B"rw" 😄

$argumentFlagIndex = null;

for ($i = $argumentStartIndex; $i <= $argumentEndIndex; ++$i) {
if ($tokens[$i]->equalsAny([[T_WHITESPACE], [T_COMMENT], [T_DOC_COMMENT]])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think isGivenKind([T_WHITESPACE, T_COMMENT, T_DOC_COMMENT]) may be more optimal here

@ntzm
Copy link
Contributor

ntzm commented Jul 10, 2018

Think something is wrong with my GitHub notifications, seem to be missing some, sorry about the delay

@SpacePossum SpacePossum changed the title FopenFlagOrderFixer - introduction FopenFlagOrderFixer & FopenFlagsFixer - introduction Aug 8, 2018
@SpacePossum SpacePossum removed the WIP label Aug 8, 2018
Copy link
Contributor

@dmvdbrugge dmvdbrugge left a comment

Choose a reason for hiding this comment

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

Just minor points

*
* @author SpacePossum
*/
abstract class AbstractFopenFlagOrderFixer extends AbstractFunctionReferenceFixer
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest removing Order from this name, as only one of the extending classes actually has to do with ordering.

@@ -166,6 +166,7 @@
'dir_constant' => true,
'ereg_to_preg' => true,
'error_suppression' => true,
'fopen_flag_order' => true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want the other rule as well?


if (strlen($mode) > 13) { // 13 === length 'r+w+a+x+c+etb'
return; // sanity check to be less risky
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Line 65 - 79 seem to be duplicated on both fixers, can they be a protected function on the base class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

line 77 shouldn't be the same, besides that I think the shared logic shouldn't be made abstract as it will limit other possible fixers

Copy link
Contributor

Choose a reason for hiding this comment

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

As the abstract fixer is called AbstractFopenFlagFixer all extensions should operate only on the flags of fopen. I would thus like to make the case that actually all logic in this function except lines 77-86 should be in the parent class. The method now only needs to take in string $mode and return either a fixed or non-altered $mode. For the other fixer, the lines are 78-83 (plus the implode of 84).

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 disagree; other fixers might want to operate on more tokens, like complex arguments, and not (only) when the mode is set as literal.

$arguments = $argumentsAnalyzer->getArguments(
$tokens,
$index,
$tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index)
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 already done in $this->find() and available as $candidate[2].

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, fixed

@keradus keradus added has PR and removed has PR labels Aug 16, 2018
@keradus keradus changed the title FopenFlagOrderFixer & FopenFlagsFixer - introduction Add FopenFlagOrderFixer & FopenFlagsFixer Aug 22, 2018
@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 @SpacePossum.

@keradus keradus merged commit 8682d86 into PHP-CS-Fixer:master Aug 22, 2018
keradus added a commit that referenced this pull request Aug 22, 2018
This PR was merged into the 2.13-dev branch.

Discussion
----------

Add FopenFlagOrderFixer & FopenFlagsFixer

closes #3417

ping @ntzm , hope you can help me out with:
- define the order of all flags
- ~do we want to have an option to remove flags when found, like `t`~
- ~do we want to have an option to add flags when not found, like `b`~

Commits
-------

8682d86 Add FopenFlagOrderFixer & FopenFlagsFixer
@keradus keradus deleted the master_FopenFlagOrderFixer branch August 22, 2018 18:57
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