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

Refactor Current operation - Let specify a default value #205

Merged
merged 9 commits into from
Oct 25, 2021

Conversation

drupol
Copy link
Collaborator

@drupol drupol commented Oct 20, 2021

This PR:

  • Update the Current operation and allow users to specify a default value when the user index is out of bound. The default value by default is null to stay backward compatible.
  • It breaks backward compatibility in a minor way... if the Current class is used on its own.
  • Has unit tests (phpspec)
  • Has static analysis tests (psalm, phpstan)
  • Has documentation

@drupol drupol force-pushed the refactor/current--let-specify-a-default-value branch from df658a1 to d3e95e2 Compare October 20, 2021 19:52
@drupol drupol force-pushed the refactor/current--let-specify-a-default-value branch 2 times, most recently from bfefcea to 01bcd17 Compare October 20, 2021 19:59
@drupol drupol self-assigned this Oct 20, 2021
@drupol drupol force-pushed the refactor/current--let-specify-a-default-value branch from 01bcd17 to 97ab2c7 Compare October 20, 2021 20:02
@drupol drupol marked this pull request as ready for review October 20, 2021 20:28
@drupol drupol removed their assignment Oct 20, 2021
Copy link
Collaborator

@AlexandruGG AlexandruGG left a comment

Choose a reason for hiding this comment

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

We also need some new checks that use a default value with current here:
https://github.com/loophp/collection/blob/master/tests/static-analysis/current.php

docs/pages/api.rst Outdated Show resolved Hide resolved
tests/static-analysis/get.php Show resolved Hide resolved
get_checkIntElement(Collection::fromIterable([1, 2, 3])->get(1)->current());
/** @psalm-suppress PossiblyNullArgument @phpstan-ignore-next-line */
Copy link
Collaborator

Choose a reason for hiding this comment

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

same with all of these, has the behaviour changed?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this slight behaviour change is because of the Psalm issue: vimeo/psalm#6685

@drupol drupol force-pushed the refactor/current--let-specify-a-default-value branch 2 times, most recently from 08cb578 to 296c496 Compare October 24, 2021 17:59
@drupol drupol force-pushed the refactor/current--let-specify-a-default-value branch from 296c496 to 8f3eef0 Compare October 24, 2021 18:44
@drupol drupol force-pushed the refactor/current--let-specify-a-default-value branch from 8f3eef0 to 85a1a2f Compare October 24, 2021 18:45
* @param TKey $key
* @param T|null $default
* @param V|null $default
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
* @param V|null $default
* @param V $default

I'm wondering whether this will be enough to prevent NullArgument in the static analysis checks here: https://github.com/loophp/collection/pull/205/files#diff-a937b0d4bd3a09eb4429e096dacf9b26f2dd67d7c351847aa12020dc44fdabe3R71

the return type would also be @return Collection<TKey, T|V>

It's basically saying that null is included in the template V. Seems to be working fine for the updated find operation: https://github.com/loophp/collection/pull/204/files?diff=split&w=1#diff-24b4b03ac10151d1e45f941532d787d12da924e784810b814dcb366380c2bdbcR26-R33. See the static analysis checks there as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Faire enough. TBH, this is something that I'm unsure of. I never know if we have to add this |null or not.

I will follow your advice and remove it. Let me know if it's ok.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'll have a look and do some testing in a bit

Copy link
Collaborator

Choose a reason for hiding this comment

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

I made a commit to remove some of these remaining null: f6e05ee

@drupol drupol force-pushed the refactor/current--let-specify-a-default-value branch from ca94605 to 0c5e433 Compare October 24, 2021 19:33
@drupol drupol self-assigned this Oct 24, 2021
@drupol
Copy link
Collaborator Author

drupol commented Oct 24, 2021

I'll add the suggested tests tomorrow.

@drupol
Copy link
Collaborator Author

drupol commented Oct 25, 2021

Ok I'm done with this one, let me know what you think @AlexandruGG .

I removed the null from the allowed values to be returned. WDYT?

@drupol drupol force-pushed the refactor/current--let-specify-a-default-value branch from f30db13 to 4430daa Compare October 25, 2021 06:32
@drupol drupol removed their assignment Oct 25, 2021
Copy link
Collaborator

@AlexandruGG AlexandruGG left a comment

Choose a reason for hiding this comment

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

@drupol thanks for this nice addition; it should be good to go now, but have a look at the last commit I made

@drupol
Copy link
Collaborator Author

drupol commented Oct 25, 2021

Excellent.

@drupol drupol merged commit b91405e into master Oct 25, 2021
@drupol drupol deleted the refactor/current--let-specify-a-default-value branch October 25, 2021 07:40
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

2 participants