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

VACMS-16882: Resolves Error: Call to a member function getValue() on null #16900

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ public function addToNationalOutreachCalendar(EventInterface $node): void {
if ($node->hasField(EventOutreachInterface::LISTING_FIELD) &&
$node->hasField(EventOutreachInterface::PUBLISH_TO_OUTREACH_CAL_FIELD) &&
$node->hasField(EventOutreachInterface::ADDITIONAL_LISTING_FIELD)) {
$addToCalValue = $node->get(EventOutreachInterface::PUBLISH_TO_OUTREACH_CAL_FIELD)->first()->getValue();
if (isset($addToCalValue['value'])) {
if ($node->get(EventOutreachInterface::PUBLISH_TO_OUTREACH_CAL_FIELD)->first()) {
$addToCalValue = $node->get(EventOutreachInterface::PUBLISH_TO_OUTREACH_CAL_FIELD)->first()->getValue();
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like a good combination to defend against NULL.

$listings = $node->get(EventOutreachInterface::LISTING_FIELD)->getValue();
$additionalListings = $node->get(EventOutreachInterface::ADDITIONAL_LISTING_FIELD)->getValue();
assert(array_key_exists('value', $addToCalValue));
Copy link
Contributor

Choose a reason for hiding this comment

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

I am pretty sure I am missing the use case/edge case here so apologies for the potentially dumb question here.

Is there an edge case where it would get past line 49 and still not have a 'value' here at line 53? If there is, it might be good to spell out with a comment the situation that would lead to the assertion failing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question Steve. My thinking in this case was that there is actually no guarantee that, even though the field has a value, that the property 'value' exists in the returned array. For entity reference fields for example, the primary field property is 'target_id'. So this assertion was a way for me to protect against any future changes to the target field where it changes to a different type, or for some reason the 'value' property changes to something else.

Generally I've been trying to get away from using magic methods to get to field values, which would actually improve the readability of this code, but this is the outcome of doing that in this situation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense. Thanks for the explanation @dsasser.

if ($addToCalValue['value'] === 1 || $this->outreachHubOnlyUser()) {
// Add to Outreach calendar selected, or user is Outreach Hub only
// user.
Expand Down
Loading