From 86b79fe8146489ee7be7ff61d7aae0db3566571b Mon Sep 17 00:00:00 2001
From: Tim Otten <totten@civicrm.org>
Date: Sat, 15 Jul 2023 21:36:16 -0700
Subject: [PATCH] (REF) CMSUser::create - Rename misleading parameter

The second parameter is very confusing:

* The name `$mail` suggests that the parameter is an email address.
* The parameter `$mail` actually contains the name of the *field* which has the email address.

This is a weird contract. It invites misinterpretation -- and now, e.g., some of the `Standalone`
code has misused it.

To confirm that `$mail` is actually the parameter-name, note that:

1. `create()` actually looks for `$params[$mail]`
2. Callers like `CRM/Contact/Form/Task/Useradd.php` actually give a parameter name
---
 CRM/Core/BAO/CMSUser.php        | 11 ++++++-----
 CRM/Utils/System/Backdrop.php   |  4 ++--
 CRM/Utils/System/Base.php       |  7 ++++---
 CRM/Utils/System/Drupal.php     |  4 ++--
 CRM/Utils/System/Drupal8.php    |  4 ++--
 CRM/Utils/System/Joomla.php     |  4 ++--
 CRM/Utils/System/Standalone.php |  9 +++++----
 CRM/Utils/System/WordPress.php  |  4 ++--
 8 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/CRM/Core/BAO/CMSUser.php b/CRM/Core/BAO/CMSUser.php
index a07b61a5f0..e43a7b4998 100644
--- a/CRM/Core/BAO/CMSUser.php
+++ b/CRM/Core/BAO/CMSUser.php
@@ -28,16 +28,17 @@ class CRM_Core_BAO_CMSUser {
    * Create CMS user using Profile.
    *
    * @param array $params
-   * @param string $mail
-   *   Email id for cms user.
+   * @param string $mailParam
+   *   Name of the param which contains the email address.
+   *   Because. Right. OK. That's what it is.
    *
    * @return int
    *   contact id that has been created
    */
-  public static function create(&$params, $mail) {
+  public static function create(&$params, $mailParam) {
     $config = CRM_Core_Config::singleton();
 
-    $ufID = $config->userSystem->createUser($params, $mail);
+    $ufID = $config->userSystem->createUser($params, $mailParam);
 
     //if contact doesn't already exist create UF Match
     if ($ufID !== FALSE &&
@@ -46,7 +47,7 @@ class CRM_Core_BAO_CMSUser {
       // create the UF Match record
       $ufmatch['uf_id'] = $ufID;
       $ufmatch['contact_id'] = $params['contactID'];
-      $ufmatch['uf_name'] = $params[$mail];
+      $ufmatch['uf_name'] = $params[$mailParam];
       CRM_Core_BAO_UFMatch::create($ufmatch);
     }
 
diff --git a/CRM/Utils/System/Backdrop.php b/CRM/Utils/System/Backdrop.php
index 2b60e638e7..5cd20354cc 100644
--- a/CRM/Utils/System/Backdrop.php
+++ b/CRM/Utils/System/Backdrop.php
@@ -23,12 +23,12 @@ class CRM_Utils_System_Backdrop extends CRM_Utils_System_DrupalBase {
   /**
    * @inheritDoc
    */
-  public function createUser(&$params, $mail) {
+  public function createUser(&$params, $mailParam) {
     $form_state = form_state_defaults();
 
     $form_state['input'] = [
       'name' => $params['cms_name'],
-      'mail' => $params[$mail],
+      'mail' => $params[$mailParam],
       'op' => 'Create new account',
     ];
 
diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php
index 72ea4f1331..0c050d622c 100644
--- a/CRM/Utils/System/Base.php
+++ b/CRM/Utils/System/Base.php
@@ -363,13 +363,14 @@ abstract class CRM_Utils_System_Base {
    * Create a user in the CMS.
    *
    * @param array $params
-   * @param string $mail
-   *   Email id for cms user.
+   * @param string $mailParam
+   *   Name of the $param which contains the email address.
+   *   Because. Right. OK. That's what it is.
    *
    * @return int|bool
    *   uid if user exists, false otherwise
    */
-  public function createUser(&$params, $mail) {
+  public function createUser(&$params, $mailParam) {
     return FALSE;
   }
 
diff --git a/CRM/Utils/System/Drupal.php b/CRM/Utils/System/Drupal.php
index ebb1a37b79..ed8907df96 100644
--- a/CRM/Utils/System/Drupal.php
+++ b/CRM/Utils/System/Drupal.php
@@ -23,12 +23,12 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
   /**
    * @inheritDoc
    */
-  public function createUser(&$params, $mail) {
+  public function createUser(&$params, $mailParam) {
     $form_state = form_state_defaults();
 
     $form_state['input'] = [
       'name' => $params['cms_name'],
-      'mail' => $params[$mail],
+      'mail' => $params[$mailParam],
       'op' => 'Create new account',
     ];
 
diff --git a/CRM/Utils/System/Drupal8.php b/CRM/Utils/System/Drupal8.php
index c866e67d96..8fcddf4de2 100644
--- a/CRM/Utils/System/Drupal8.php
+++ b/CRM/Utils/System/Drupal8.php
@@ -23,7 +23,7 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase {
   /**
    * @inheritDoc
    */
-  public function createUser(&$params, $mail) {
+  public function createUser(&$params, $mailParam) {
     $user = \Drupal::currentUser();
     $user_register_conf = \Drupal::config('user.settings')->get('register');
     $verify_mail_conf = \Drupal::config('user.settings')->get('verify_mail');
@@ -35,7 +35,7 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase {
 
     /** @var \Drupal\user\Entity\User $account */
     $account = \Drupal::entityTypeManager()->getStorage('user')->create();
-    $account->setUsername($params['cms_name'])->setEmail($params[$mail]);
+    $account->setUsername($params['cms_name'])->setEmail($params[$mailParam]);
 
     // Allow user to set password only if they are an admin or if
     // the site settings don't require email verification.
diff --git a/CRM/Utils/System/Joomla.php b/CRM/Utils/System/Joomla.php
index 0117a19bfd..8cc3592de2 100644
--- a/CRM/Utils/System/Joomla.php
+++ b/CRM/Utils/System/Joomla.php
@@ -36,7 +36,7 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
   /**
    * @inheritDoc
    */
-  public function createUser(&$params, $mail) {
+  public function createUser(&$params, $mailParam) {
     $baseDir = JPATH_SITE;
     $userParams = JComponentHelper::getParams('com_users');
 
@@ -72,7 +72,7 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
     $values['name'] = $fullname;
     $values['username'] = trim($params['cms_name']);
     $values['password1'] = $values['password2'] = $params['cms_pass'];
-    $values['email1'] = $values['email2'] = trim($params[$mail]);
+    $values['email1'] = $values['email2'] = trim($params[$mailParam]);
 
     $lang = JFactory::getLanguage();
     $lang->load('com_users', $baseDir);
diff --git a/CRM/Utils/System/Standalone.php b/CRM/Utils/System/Standalone.php
index 34d40171d5..e0203e0be0 100644
--- a/CRM/Utils/System/Standalone.php
+++ b/CRM/Utils/System/Standalone.php
@@ -60,17 +60,18 @@ class CRM_Utils_System_Standalone extends CRM_Utils_System_Base {
    *    - 'cms_name'
    *    - 'cms_pass' plaintext password
    *    - 'notify' boolean
-   * @param string $mail
-   *   Email id for cms user.
+   * @param string $mailParam
+   *   Name of the param which contains the email address.
+   *   Because. Right. OK. That's what it is.
    *
    * @return int|bool
    *   uid if user was created, false otherwise
    */
-  public function createUser(&$params, $mail) {
+  public function createUser(&$params, $mailParam) {
     if ($this->missingStandaloneExtension()) {
       return FALSE;
     }
-    return Security::singleton()->createUser($params, $mail);
+    return Security::singleton()->createUser($params, $mailParam);
   }
 
   /**
diff --git a/CRM/Utils/System/WordPress.php b/CRM/Utils/System/WordPress.php
index 30b29cf720..4b623ce37a 100644
--- a/CRM/Utils/System/WordPress.php
+++ b/CRM/Utils/System/WordPress.php
@@ -874,11 +874,11 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
   /**
    * @inheritDoc
    */
-  public function createUser(&$params, $mail) {
+  public function createUser(&$params, $mailParam) {
     $user_data = [
       'ID' => '',
       'user_login' => $params['cms_name'],
-      'user_email' => $params[$mail],
+      'user_email' => $params[$mailParam],
       'nickname' => $params['cms_name'],
       'role' => get_option('default_role'),
     ];
-- 
2.25.1