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

Api4 - Prevent developer error mixing up addValue with addWhere #25905

Merged
merged 1 commit into from
Mar 24, 2023

Conversation

colemanw
Copy link
Member

Overview

This adds a sanity check in the api to prevent the common mistake of accidentally using addValue as if it takes 3 arguments like addWhere.

Technical Details

This is an easy mistake to make because the function is similar to addWhere:

Wrong:

Address::create()
  ->addValue('city', '=', 'Paris')

Right:

Address::create()
  ->addValue('city', 'Paris')

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.

@civibot
Copy link

civibot bot commented Mar 23, 2023

(Standard links)

@civibot civibot bot added the master label Mar 23, 2023
@mattwire
Copy link
Contributor

Don't think I've ever got it wrong that way around but I do keep forgetting to put the operator in addWhere()!

@colemanw
Copy link
Member Author

@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) {
Copy link
Member

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.

Copy link
Contributor

@elisseck elisseck left a 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 via cv 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 function addValue 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)

@colemanw
Copy link
Member Author

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!

@colemanw colemanw merged commit 5b35294 into civicrm:master Mar 24, 2023
@colemanw colemanw deleted the getSetApiValues branch March 24, 2023 01:30
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.

4 participants