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

Minor improvements in code complexity with early returns and early validation #116

Merged
merged 4 commits into from
May 6, 2020
Merged

Minor improvements in code complexity with early returns and early validation #116

merged 4 commits into from
May 6, 2020

Conversation

Ayesh
Copy link
Contributor

@Ayesh Ayesh commented Apr 7, 2020

Hi there,
I noticed a few minor improvements that we could make in the Guard class.


  1. Guard::__construct has a call that it initializes $this->keyPair = null. This can be moved to the property declaration without changing the functionality.
  2. There is one check for $strength < 16 check, which can be moved to the top as well, so the later calls are not executed if we have to throw an exception. The early calls do not mutate state.

  1. Guard::validateToken() and Guard::enforceStorageLimit() methods have nested if calls, which can be removed in favor of early returns.

Fingers crossed this can be merged, and I'm open to make any further changes if necessary. Thank you for your time.

@l0gicgate
Copy link
Member

Are you planning to fix this PR?

@Ayesh
Copy link
Contributor Author

Ayesh commented Apr 15, 2020

@l0gicgate - I will update tests to make sure we improve the coverage. Previously, the entirety of the test-covered code was inside a big if statement, which made the coverage reach 100%. But now, the logic is inverted and there is an early-return, which there is no longer a test for. I will push another commit with tests. Thank you.

@l0gicgate
Copy link
Member

@Ayesh I’ll wait until coverage is back to 100% to merge! Thanks

During a refacotiring of this method, the early-return `if` block was not tested properly.
There is a new test \Slim\Tests\Csrf\GuardTest::testNotEnforceStorageLimitWithArrayWhenLimitIsZero that makes sure the storage array provided remains the same after a token is processed when the storage limit is set to zero, which should trigger the early-return `if` block, this adding more coverage.
@Ayesh
Copy link
Contributor Author

Ayesh commented Apr 15, 2020

@l0gicgate - I added a new test to cover the branch that fell behind in tests. Coverage back to 100% now. Thanks.

Copy link
Member

@l0gicgate l0gicgate left a comment

Choose a reason for hiding this comment

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

Looks good to me @adriansuter can you this a quick look

@l0gicgate l0gicgate added this to the 1.0.1 milestone May 6, 2020
@l0gicgate l0gicgate merged commit 6613d91 into slimphp:master May 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants