-
-
Notifications
You must be signed in to change notification settings - Fork 827
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
Api4 - Prevent developer error mixing up addValue
with addWhere
#25905
Conversation
(Standard links)
|
Don't think I've ever got it wrong that way around but I do keep forgetting to put the operator in addWhere()! |
b7b5e0f
to
1f34d32
Compare
@mattwire doing that also throws an exception :) |
*/ | ||
public function addValue(string $fieldName, $value) { | ||
// Prevent accidentally using this function like `addWhere` which takes 3 args. | ||
if ($value === '=' && func_num_args() > 2) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, nice. I don't know if reflective performance still matters these days, but we don't have to worry - since it only needs to check in a small edge-case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
General standards
- r-explain: pass
- r-user: pass
- r-doc: pass
- r-run: undecided
Ran viacv php:scr
under php 8.0:
$results = \Civi\Api4\Contact::create(FALSE) ->addValue('contact_type', 'Individual') ->addValue('first_name', '=', 'Test') ->addValue('last_name', 'addValue') ->execute();
Previous Behavior: Runs without issue, results in an incorrect contact with first name '='.
New Behavior: Receive API error "APIv4 functionaddValue
incorrectly called with 3 arguments."
Only undecided here to note that it appears that now we will receive API errors where we didn't before (even if the behavior was incorrect). That may be fine however. These should probably be in try/catch anyways.
On mattermost (https://chat.civicrm.org/civicrm/pl/5apjsytjw7gwmrsy7dq1386nfe) it seems like the thought was this would throw an error, however, if the type was incorrect. I wasn't able to make this happen with a contribution create date either ("now" was filled instead for "date_received" in this case).
- Developer standards
- r-tech: pass (love the use of trait)
- r-code: pass
- r-maint: pass
- r-test: pass (coleman noted on mattermost there is great test coverage here already)
Thanks for the review - I agree that try/catch should be used. IMO any developers who have done this incorrectly and now get an error will benefit in the long run, as will any future developers who make this mistake! |
Overview
This adds a sanity check in the api to prevent the common mistake of accidentally using
addValue
as if it takes 3 arguments likeaddWhere
.Technical Details
This is an easy mistake to make because the function is similar to
addWhere
:Wrong:
Right:
Now the API will throw an exception telling you what you did wrong.
Comments
Instead of pasting this check in 5 places I used a trait.