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

TernaryOperatorSpacesFixer - fix for discovering ":" correctly #5065

Merged
merged 1 commit into from
Aug 6, 2020
Merged

TernaryOperatorSpacesFixer - fix for discovering ":" correctly #5065

merged 1 commit into from
Aug 6, 2020

Conversation

kubawerlos
Copy link
Contributor

@kubawerlos kubawerlos commented Jul 23, 2020

In the original code for each occurrence of ? it was assumed that the next : is the other part of the operator - which is not always true - see the latter test added to TernaryOperatorSpacesFixerTest.php.

Also, when there was no whitespace before ? the operator was counted twice, because:

  • the loop was iterating by increasing index
  • so at one moment ? was found at index n
  • as it had no whitespace before it, the whitespace was added - at index n, so the ? was moved to position n + 1
  • next loop iteration with n + 1 discovered the very same ? once again and increased $ternaryLevel for the second time.

This scenario is covered by the first test added to TernaryOperatorSpacesFixerTest.php - the second : (the one from switch) was recognized as part of the ternary operator.

THE FIX:
As it might look as overkill it is actually copied from #4021 - so it will be reused there and I believe is quite clean and - especially - bulletproof solution.

@SpacePossum SpacePossum added this to the 2.15.9 milestone Jul 28, 2020
@SpacePossum SpacePossum added the RTM Ready To Merge label Jul 28, 2020
@SpacePossum
Copy link
Contributor

Thank you @kubawerlos.

@SpacePossum SpacePossum removed the RTM Ready To Merge label Aug 6, 2020
@SpacePossum SpacePossum merged commit 02a63e3 into PHP-CS-Fixer:2.15 Aug 6, 2020
@kubawerlos kubawerlos deleted the fix_TernaryOperatorSpacesFixer branch August 6, 2020 20:17
@GrahamCampbell
Copy link
Contributor

@SpacePossum This PR causes warning on PHP 5.6 because of the declare strict types. I've prepped a fix: #5100.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants