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

fix: set nullable true when default value is null #2390

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

heiglandreas
Copy link
Contributor

Description

When reflection shows that the default parameter of a a property is null, then the nullable parameter is set to true for that property.

Currently that needs to be set via the attribute but that is a bit redundant as the information is already available from reflection

This change only does that for properties and for setter methods.

Right now getter methods that allow a null value to be returned are not taken into account for setting the nullable property.

This resets the state prior to v4.25.0 and is related to #2330

This is a refactor of #2377

What type of PR is this? (check all applicable)

  • Bug Fix
  • Feature
  • Refactor
  • Deprecation
  • Breaking Change
  • Documentation Update
  • CI

Checklist

  • I have made corresponding changes to the documentation (docs/)
  • I have made corresponding changes to the changelog (CHANGELOG.md)

@heiglandreas heiglandreas force-pushed the update-nullables-from-reflection branch from 4357ef7 to 9c775b0 Compare November 11, 2024 09:08
@heiglandreas heiglandreas changed the title Set nullable true when default value is null fix: set nullable true when default value is null Nov 11, 2024
@heiglandreas heiglandreas force-pushed the update-nullables-from-reflection branch from 9c775b0 to 3cb0e0c Compare November 11, 2024 10:38
Copy link

codecov bot commented Nov 11, 2024

Codecov Report

Attention: Patch coverage is 96.42857% with 1 line in your changes missing coverage. Please review.

Project coverage is 89.53%. Comparing base (c431718) to head (318d00d).
Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
...rc/ModelDescriber/Annotations/ReflectionReader.php 96.42% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2390      +/-   ##
==========================================
+ Coverage   89.50%   89.53%   +0.03%     
==========================================
  Files          78       78              
  Lines        2896     2915      +19     
==========================================
+ Hits         2592     2610      +18     
- Misses        304      305       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@heiglandreas heiglandreas force-pushed the update-nullables-from-reflection branch 2 times, most recently from 2ee2ccd to 7489b46 Compare November 11, 2024 11:02
When reflection shows that the default parameter of a a property is
null, then the `nullable` parameter is set to `true` for that property.

Currently that needs to be set via the attribute but that is a bit
redundant as the information is already available from reflection

This change only does that for properties and for setter methods.

Right now getter methods that allow a null value to be returned are not
taken into account for setting the `nullable` property.
@heiglandreas heiglandreas force-pushed the update-nullables-from-reflection branch from 7489b46 to 318d00d Compare November 11, 2024 12:57
@@ -134,12 +146,49 @@ public function getDefaultFromPropertyReflection(\ReflectionProperty $reflection
return Generator::UNDEFINED;
}

$defaultValue = $reflection->getDeclaringClass()->getDefaultProperties()[$propertyName] ?? null;
if (PHP_VERSION_ID < 80000) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why does this need to check the PHP version? I would rather this not be the case because the next major 5.x will move to PHP 8.1 minimum

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As hasDefaultValue and getDefaultValue are only available since PHP8.0 I needed this "workaround" for the PHP7 users that uses a quirky way of getting the default values. That way also does not take into account possible DocBlock types so it might report back an unexpected default value when an implicit null is set. In PHP8.0 and higher we check for DocBlock type-infos to make a possible NULL default value plausible or not.

In he next major release this part can safely be removed as PHP8.1 supports the required functions 😉

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.

2 participants