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

Make TimestampableEntity getter type hints nullable #2531

Merged
merged 2 commits into from
Jun 20, 2023

Conversation

villermen
Copy link
Contributor

Signals nullability for return types of TimestampableEntity::getCreatedAt() and TimestampableEntity::getUpdatedAt(). They may actually return null when they are not yet persisted.

This helps static type checkers understand this trait better.

@codecov-commenter
Copy link

codecov-commenter commented Nov 6, 2022

Codecov Report

Base: 80.16% // Head: 80.16% // Decreases project coverage by -0.00% ⚠️

Coverage data is based on head (b54a15e) compared to base (d1988ae).
Patch has no changes to coverable lines.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #2531      +/-   ##
============================================
- Coverage     80.16%   80.16%   -0.01%     
  Complexity     3142     3142              
============================================
  Files           158      158              
  Lines          7705     7708       +3     
============================================
+ Hits           6177     6179       +2     
- Misses         1528     1529       +1     
Impacted Files Coverage Δ
src/Timestampable/Traits/TimestampableEntity.php 100.00% <ø> (ø)
src/Tree/Strategy/ORM/Nested.php 95.68% <0.00%> (-0.28%) ⬇️
src/Uploadable/UploadableListener.php 93.54% <0.00%> (-0.03%) ⬇️
...e/Entity/Repository/MaterializedPathRepository.php 98.05% <0.00%> (-0.02%) ⬇️
src/Tree/Strategy/ORM/Closure.php 97.18% <0.00%> (+0.05%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@github-actions
Copy link

github-actions bot commented Jun 1, 2023

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@villermen
Copy link
Contributor Author

Would love for this to receive a response, so please don't auto close.

@github-actions github-actions bot removed the Stale label Jun 19, 2023
Copy link
Collaborator

@phansys phansys left a comment

Choose a reason for hiding this comment

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

Hi @villermen! Thank you for your first contribution.
Please, be aware about the PR's labels. It currently requires a rebase.

@@ -53,7 +53,7 @@ public function setCreatedAt(\DateTime $createdAt)
/**
* Returns createdAt.
*
* @return \DateTime
* @return \DateTime|null
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we are going to allow null as return type for these methods, I guess we should first update the hints for the properties that are being returned here.

@villermen
Copy link
Contributor Author

@phansys Thanks for the review! I have rebased the PR against main, and applied nullable hints to all timestampable traits.

Side question: Is there anything against adding them as native return type? E.g., getCreatedAt(): ?\DateTime? I noticed the minimum required PHP version is 7.2 where this is supported, and it's not technically a BC break right?

@phansys
Copy link
Collaborator

phansys commented Jun 20, 2023

Side question: Is there anything against adding them as native return type? E.g., getCreatedAt(): ?\DateTime?

The reason behind that decision is to avoid issues for users that are not respecting the type hints declared in our API. Because its nature, I guess changes like this will be part of the next major release.

@phansys phansys merged commit 9c46ada into doctrine-extensions:main Jun 20, 2023
@phansys
Copy link
Collaborator

phansys commented Jun 20, 2023

Thank you @villermen!

@nvdbeek
Copy link

nvdbeek commented Oct 24, 2023

It is technically correct that these properties may be null when retrieved before being persisted. However in which situations would you actually call these methods before then? Is it not improper to call this method when they are nullable, wouldn't be better to throw exceptions in the situation when this data is not available yet. Making these return types nullables forces us to either throw exceptions or add assertions which are unnecessary in most cases.

@villermen
Copy link
Contributor Author

@nvdbeek I created this PR to have the documentation match the behavior, mainly to correct static type check inconsistencies I was experiencing. I would be in favor of a non-null property too, but I don't think an exception will help prevent undesirable situations. null conveys better that the value has not yet been set and allows creating simple fallback logic with ??, ?-> and the likes.

Forcing initialization of the properties upon construction (not on get) so that they are always available may also prove difficult when using traits. I wouldn't know how to approach that, but if you have an idea there a PR is probably welcome =)

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.

4 participants