From ee3db087e164a0af3871725f65bceb7931fe35cb Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sun, 15 Dec 2019 08:40:07 +1100 Subject: [PATCH] [REF] Switch CRM_Utils_Error::fatal calls for CRM_Core_Exception in Grant. utils, bin/ and Civi/Angular/ folders --- CRM/Grant/BAO/Grant.php | 4 +++- CRM/Utils/Hook.php | 7 +++---- CRM/Utils/HttpClient.php | 6 ++++-- CRM/Utils/Mail.php | 6 ++++-- CRM/Utils/Mail/EmailProcessor.php | 11 ++++++++--- CRM/Utils/Migrate/Export.php | 6 ++++-- CRM/Utils/Migrate/Import.php | 4 ++-- CRM/Utils/System.php | 12 ++++++++---- CRM/Utils/Type.php | 7 +++---- CRM/Utils/Weight.php | 9 +++++++-- Civi/Angular/Page/Modules.php | 4 +++- bin/ContributionProcessor.php | 6 +++--- 12 files changed, 52 insertions(+), 30 deletions(-) diff --git a/CRM/Grant/BAO/Grant.php b/CRM/Grant/BAO/Grant.php index 12147c190e..baf098c4fc 100644 --- a/CRM/Grant/BAO/Grant.php +++ b/CRM/Grant/BAO/Grant.php @@ -71,6 +71,8 @@ class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant { * * @return array * Array of event summary values + * + * @throws CRM_Core_Exception */ public static function getGrantStatusOptGroup() { @@ -81,7 +83,7 @@ class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant { $og = CRM_Core_BAO_OptionGroup::retrieve($params, $defaults); if (!$og) { - CRM_Core_Error::fatal('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql'); + throw new CRM_Core_Exception('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql'); } return $og; diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index 89dd2be19d..b84ec7ac87 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -57,7 +57,7 @@ abstract class CRM_Utils_Hook { private $commonIncluded = FALSE; /** - * @var array(string) + * @var array|string */ private $commonCiviModules = []; @@ -249,7 +249,7 @@ abstract class CRM_Utils_Hook { * @param mixed $arg6 * * @return array|bool - * @throws \Exception + * @throws \CRM_Core_Exception */ public function runHooks( $civiModules, $fnSuffix, $numParams, @@ -307,8 +307,7 @@ abstract class CRM_Utils_Hook { break; default: - CRM_Core_Error::fatal(ts('Invalid hook invocation')); - break; + throw new CRM_Core_Exception(ts('Invalid hook invocation')); } if (!empty($fResult) && diff --git a/CRM/Utils/HttpClient.php b/CRM/Utils/HttpClient.php index 4bab3001ea..c7989ec626 100644 --- a/CRM/Utils/HttpClient.php +++ b/CRM/Utils/HttpClient.php @@ -62,16 +62,18 @@ class CRM_Utils_HttpClient { * @param string $localFile * Path at which to store the .zip file. * @return STATUS_OK|STATUS_WRITE_ERROR|STATUS_DL_ERROR + * + * @throws CRM_Core_Exception */ public function fetch($remoteFile, $localFile) { // Download extension zip file ... if (!function_exists('curl_init')) { - CRM_Core_Error::fatal('Cannot install this extension - curl is not installed!'); + throw new CRM_Core_Exception('Cannot install this extension - curl is not installed!'); } list($ch, $caConfig) = $this->createCurl($remoteFile); if (preg_match('/^https:/', $remoteFile) && !$caConfig->isEnableSSL()) { - CRM_Core_Error::fatal('Cannot install this extension - does not support SSL'); + throw new CRM_Core_Exception('Cannot install this extension - does not support SSL'); } $fp = @fopen($localFile, "w"); diff --git a/CRM/Utils/Mail.php b/CRM/Utils/Mail.php index 6e7cc0c6cc..b9aa6ef617 100644 --- a/CRM/Utils/Mail.php +++ b/CRM/Utils/Mail.php @@ -25,6 +25,8 @@ class CRM_Utils_Mail { * to the mailer through the container. * * @return Mail + * + * @throws CRM_Core_Exception */ public static function createMailer() { $mailingInfo = Civi::settings()->get('mailing_backend'); @@ -37,7 +39,7 @@ class CRM_Utils_Mail { elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { if ($mailingInfo['smtpServer'] == '' || !$mailingInfo['smtpServer']) { CRM_Core_Error::debug_log_message(ts('There is no valid smtp server setting. Click Administer >> System Setting >> Outbound Email to set the SMTP Server.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); - CRM_Core_Error::fatal(ts('There is no valid smtp server setting. Click Administer >> System Setting >> Outbound Email to set the SMTP Server.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); + throw new CRM_Core_Exception(ts('There is no valid smtp server setting. Click Administer >> System Setting >> Outbound Email to set the SMTP Server.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); } $params['host'] = $mailingInfo['smtpServer'] ? $mailingInfo['smtpServer'] : 'localhost'; @@ -81,7 +83,7 @@ class CRM_Utils_Mail { !$mailingInfo['sendmail_path'] ) { CRM_Core_Error::debug_log_message(ts('There is no valid sendmail path setting. Click Administer >> System Setting >> Outbound Email to set the sendmail server.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); - CRM_Core_Error::fatal(ts('There is no valid sendmail path setting. Click Administer >> System Setting >> Outbound Email to set the sendmail server.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); + throw new CRM_Core_Exception(ts('There is no valid sendmail path setting. Click Administer >> System Setting >> Outbound Email to set the sendmail server.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); } $params['sendmail_path'] = $mailingInfo['sendmail_path']; $params['sendmail_args'] = $mailingInfo['sendmail_args']; diff --git a/CRM/Utils/Mail/EmailProcessor.php b/CRM/Utils/Mail/EmailProcessor.php index 90b65cdc04..9d68310b9d 100644 --- a/CRM/Utils/Mail/EmailProcessor.php +++ b/CRM/Utils/Mail/EmailProcessor.php @@ -75,6 +75,10 @@ class CRM_Utils_Mail_EmailProcessor { /** * Process the mailboxes that aren't default (ie. that aren't used by civiMail for the bounce). + * + * @return bool + * + * @throws CRM_Core_Exception. */ public static function processActivities() { $dao = new CRM_Core_DAO_MailSettings(); @@ -87,7 +91,7 @@ class CRM_Utils_Mail_EmailProcessor { self::_process(FALSE, $dao, TRUE); } if (!$found) { - CRM_Core_Error::fatal(ts('No mailboxes have been configured for Email to Activity Processing')); + throw new CRM_Core_Exception(ts('No mailboxes have been configured for Email to Activity Processing')); } return $found; } @@ -114,6 +118,7 @@ class CRM_Utils_Mail_EmailProcessor { * Create activities. * * @throws Exception + * @throws CRM_Core_Exception */ public static function _process($civiMail, $dao, $is_create_activities) { // 0 = activities; 1 = bounce; @@ -125,7 +130,7 @@ class CRM_Utils_Mail_EmailProcessor { : CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Inbound Email'); if (!$emailActivityTypeId) { - CRM_Core_Error::fatal(ts('Could not find a valid Activity Type ID for Inbound Email')); + throw new CRM_Core_Exception(ts('Could not find a valid Activity Type ID for Inbound Email')); } $config = CRM_Core_Config::singleton(); @@ -156,7 +161,7 @@ class CRM_Utils_Mail_EmailProcessor { $message = ts('Could not connect to MailStore for ') . $dao->username . '@' . $dao->server . '

'; $message .= ts('Error message: '); $message .= '

' . $e->getMessage() . '

'; - CRM_Core_Error::fatal($message); + throw new CRM_Core_Exception($message); } // process fifty at a time, CRM-4002 diff --git a/CRM/Utils/Migrate/Export.php b/CRM/Utils/Migrate/Export.php index e8f5c00951..73469e553f 100644 --- a/CRM/Utils/Migrate/Export.php +++ b/CRM/Utils/Migrate/Export.php @@ -19,7 +19,8 @@ class CRM_Utils_Migrate_Export { const XML_VALUE_SEPARATOR = ":;:;:;"; /** - * @var array description of export field mapping + * @var array + * Description of export field mapping * * @code * 'exampleEntityMappingName' => array( @@ -343,6 +344,7 @@ class CRM_Utils_Migrate_Export { * * @return string * XML + * @throws CRM_Core_Exception */ public function toXML() { $buffer = ''; @@ -356,7 +358,7 @@ class CRM_Utils_Migrate_Export { $buffer .= " _xml[$key]['scope']}>\n"; } elseif ($this->_xml[$key]['required']) { - CRM_Core_Error::fatal("No records in DB for $key"); + throw new CRM_Core_Exception("No records in DB for $key"); } } $buffer .= "\n"; diff --git a/CRM/Utils/Migrate/Import.php b/CRM/Utils/Migrate/Import.php index 0f5d0443a8..420198d791 100644 --- a/CRM/Utils/Migrate/Import.php +++ b/CRM/Utils/Migrate/Import.php @@ -398,7 +398,7 @@ AND v.name = %1 * @param $xml * @param $idMap * - * @throws Exception + * @throws CRM_Core_Exception */ public function profileFields(&$xml, &$idMap) { foreach ($xml->ProfileFields as $profileFieldsXML) { @@ -423,7 +423,7 @@ AND f.column_name = %2 ]; $cfID = CRM_Core_DAO::singleValueQuery($sql, $params); if (!$cfID) { - CRM_Core_Error::fatal(ts("Could not find custom field for %1, %2, %3", + throw new CRM_Core_Exception(ts("Could not find custom field for %1, %2, %3", [ 1 => $profileField->field_name, 2 => $tableName, diff --git a/CRM/Utils/System.php b/CRM/Utils/System.php index 28f9e9ba9e..21cfda96a1 100644 --- a/CRM/Utils/System.php +++ b/CRM/Utils/System.php @@ -980,6 +980,8 @@ class CRM_Utils_System { * Returns TRUE if the requirement is met, FALSE if the requirement is not * met and we're not aborting due to the failed requirement. If $abort is * TRUE and the requirement fails, this function does not return. + * + * @throws CRM_Core_Exception */ public static function checkPHPVersion($ver = 5, $abort = TRUE) { $phpVersion = substr(PHP_VERSION, 0, 1); @@ -988,7 +990,7 @@ class CRM_Utils_System { } if ($abort) { - CRM_Core_Error::fatal(ts('This feature requires PHP Version %1 or greater', + throw new CRM_Core_Exception(ts('This feature requires PHP Version %1 or greater', [1 => $ver] )); } @@ -1059,6 +1061,8 @@ class CRM_Utils_System { * * @return string * civicrm version + * + * @throws CRM_Core_Exception */ public static function version() { static $version; @@ -1075,7 +1079,7 @@ class CRM_Utils_System { // pattern check if (!CRM_Utils_System::isVersionFormatValid($version)) { - CRM_Core_Error::fatal('Unknown codebase version.'); + throw new CRM_Core_Exception('Unknown codebase version.'); } } @@ -1161,7 +1165,7 @@ class CRM_Utils_System { * * @param bool|FALSE $abort * - * @throws \Exception + * @throws \CRM_Core_Exception */ public static function redirectToSSL($abort = FALSE) { $config = CRM_Core_Config::singleton(); @@ -1177,7 +1181,7 @@ class CRM_Utils_System { Civi::log()->warning('CiviCRM thinks site is not SSL, redirecting to {url}', ['url' => $url]); if (!self::checkURL($url, TRUE)) { if ($abort) { - CRM_Core_Error::fatal('HTTPS is not set up on this machine'); + throw new CRM_Core_Exception('HTTPS is not set up on this machine'); } else { CRM_Core_Session::setStatus(ts('HTTPS is not set up on this machine'), ts('Warning'), 'alert'); diff --git a/CRM/Utils/Type.php b/CRM/Utils/Type.php index b57ea91540..54abc84763 100644 --- a/CRM/Utils/Type.php +++ b/CRM/Utils/Type.php @@ -213,7 +213,7 @@ class CRM_Utils_Type { * @param string $type * The type to verify against. * @param bool $abort - * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data. + * If TRUE, the operation will throw an CRM_Core_Exception on invalid data. * * @return mixed * The data, escaped if necessary. @@ -326,17 +326,16 @@ class CRM_Utils_Type { break; default: - CRM_Core_Error::fatal( + throw new CRM_Core_Exception( $type . " is not a recognised (camel cased) data type." ); - break; } // @todo Use exceptions instead of CRM_Core_Error::fatal(). if ($abort) { $data = htmlentities($data); - CRM_Core_Error::fatal("$data is not of the type $type"); + throw new CRM_Core_Exception("$data is not of the type $type"); } return NULL; } diff --git a/CRM/Utils/Weight.php b/CRM/Utils/Weight.php index 6df6d5bddd..47154d1f4d 100644 --- a/CRM/Utils/Weight.php +++ b/CRM/Utils/Weight.php @@ -14,12 +14,13 @@ */ class CRM_Utils_Weight { /** - * @var array, list of GET fields which must be validated + * List of GET fields which must be validated * * To reduce the size of this patch, we only sign the exploitable fields * which make up "$baseURL" in addOrder() (eg 'filter' or 'dao'). * Less-exploitable fields (eg 'dir') are left unsigned. * 'id','src','dst','dir' + * @var array */ public static $SIGNABLE_FIELDS = ['reset', 'dao', 'idName', 'url', 'filter']; @@ -418,13 +419,17 @@ class CRM_Utils_Weight { } } + /** + * + * @throws CRM_Core_Exception + */ public static function fixOrder() { $signature = CRM_Utils_Request::retrieve('_sgn', 'String'); $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), self::$SIGNABLE_FIELDS); // Validate $_GET values b/c subsequent code reads $_GET (via CRM_Utils_Request::retrieve) if (!$signer->validate($signature, $_GET)) { - CRM_Core_Error::fatal('Request signature is invalid'); + throw new CRM_Core_Exception('Request signature is invalid'); } // Note: Ensure this list matches self::$SIGNABLE_FIELDS diff --git a/Civi/Angular/Page/Modules.php b/Civi/Angular/Page/Modules.php index 3b6b5a75ba..aa3655b8c7 100644 --- a/Civi/Angular/Page/Modules.php +++ b/Civi/Angular/Page/Modules.php @@ -24,6 +24,8 @@ class Modules extends \CRM_Core_Page { * "civicrm/ajax/anulgar-modules" route). * * @deprecated + * + * @throws \CRM_Core_Exception */ public function run() { /** @@ -56,7 +58,7 @@ class Modules extends \CRM_Core_Page { break; default: - \CRM_Core_Error::fatal("Unrecognized format"); + throw new \CRM_Core_Exception("Unrecognized format"); } \CRM_Utils_System::civiExit(); diff --git a/bin/ContributionProcessor.php b/bin/ContributionProcessor.php index 3434316ac5..cf3323b5c1 100644 --- a/bin/ContributionProcessor.php +++ b/bin/ContributionProcessor.php @@ -204,7 +204,7 @@ class CiviContributeProcessor { $handle = fopen($csvFile, "r"); if (!$handle) { - CRM_Core_Error::fatal("Can't locate csv file."); + throw new CRM_Core_Exception("Can't locate csv file."); } require_once "CRM/Contribute/BAO/Contribution/Utils.php"; @@ -231,7 +231,7 @@ class CiviContributeProcessor { CRM_Core_Error::debug_log_message("Considering first row ( line $row ) as HEADER ..

", TRUE); if (empty($header)) { - CRM_Core_Error::fatal("Header is empty."); + throw new CRM_Core_Exception("Header is empty."); } } $row++; @@ -254,7 +254,7 @@ class CiviContributeProcessor { CRM_Core_DAO::$_nullObject, FALSE, 0, 'REQUEST' ); if ($start < $end) { - CRM_Core_Error::fatal("Start offset can't be less than End offset."); + throw new CRM_Core_Exception("Start offset can't be less than End offset."); } $start = date('Y-m-d', time() - $start * 24 * 60 * 60) . 'T00:00:00.00Z'; -- 2.25.1