Comment fixes
[civicrm-core.git] / CRM / Core / Error.php
index e6b543fac9b7b6426449ade8972164310bb51cbf..42c614e8f48e0c4c079ec43ffa225108a39f6c61 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-
 /*
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
@@ -24,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  * Start of the Error framework. We should check out and inherit from
@@ -46,8 +45,8 @@ require_once 'Log.php';
  * Class CRM_Exception
  */
 class CRM_Exception extends PEAR_Exception {
-  // Redefine the exception so message isn't optional
   /**
+   * Redefine the exception so message isn't optional
    * Supported signatures:
    *  - PEAR_Exception(string $message);
    *  - PEAR_Exception(string $message, int $code);
@@ -58,13 +57,14 @@ class CRM_Exception extends PEAR_Exception {
    *  - PEAR_Exception(string $message, array $causes);
    *  - PEAR_Exception(string $message, array $causes, int $code);
    *
-   * @param string exception message
+   * @param string $message exception message
    * @param int $code
    * @param Exception $previous
    */
-  public function __construct($message = NULL, $code = 0, Exception$previous = NULL) {
+  public function __construct($message = NULL, $code = 0, Exception $previous = NULL) {
     parent::__construct($message, $code, $previous);
   }
+
 }
 
 /**
@@ -74,7 +74,6 @@ class CRM_Core_Error extends PEAR_ErrorStack {
 
   /**
    * Status code of various types of errors
-   * @var const
    */
   const FATAL_ERROR = 2;
   const DUPLICATE_CONTACT = 8001;
@@ -85,20 +84,17 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * We only need one instance of this object. So we use the singleton
    * pattern and cache the instance in this variable
    * @var object
-   * @static
    */
   private static $_singleton = NULL;
 
   /**
    * The logger object for this application
    * @var object
-   * @static
    */
   private static $_log = NULL;
 
   /**
    * If modeException == true, errors are raised as exception instead of returning civicrm_errors
-   * @static
    */
   public static $modeException = NULL;
 
@@ -112,9 +108,8 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * @param string $stackClass
    *
    * @return object
-   * @static
    */
-   public static function &singleton($package = NULL, $msgCallback = FALSE, $contextCallback = FALSE, $throwPEAR_Error = FALSE, $stackClass = 'PEAR_ErrorStack') {
+  public static function &singleton($package = NULL, $msgCallback = FALSE, $contextCallback = FALSE, $throwPEAR_Error = FALSE, $stackClass = 'PEAR_ErrorStack') {
     if (self::$_singleton === NULL) {
       self::$_singleton = new CRM_Core_Error('CiviCRM');
     }
@@ -173,7 +168,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * the errors we expect are from the pear modules DB, DB_DataObject
    * which currently use PEAR::raiseError to notify of error messages.
    *
-   * @param object PEAR_Error
+   * @param object $pearError PEAR_Error
    *
    * @return void
    */
@@ -188,15 +183,15 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     }
 
     // create the error array
-    $error               = array();
-    $error['callback']   = $pearError->getCallback();
-    $error['code']       = $pearError->getCode();
-    $error['message']    = $pearError->getMessage();
-    $error['mode']       = $pearError->getMode();
+    $error = array();
+    $error['callback'] = $pearError->getCallback();
+    $error['code'] = $pearError->getCode();
+    $error['message'] = $pearError->getMessage();
+    $error['mode'] = $pearError->getMode();
     $error['debug_info'] = $pearError->getDebugInfo();
-    $error['type']       = $pearError->getType();
-    $error['user_info']  = $pearError->getUserInfo();
-    $error['to_string']  = $pearError->toString();
+    $error['type'] = $pearError->getType();
+    $error['user_info'] = $pearError->getUserInfo();
+    $error['to_string'] = $pearError->toString();
     if (function_exists('mysql_error') &&
       mysql_error()
     ) {
@@ -250,24 +245,25 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     self::abend(1);
   }
 
-  // this function is used to trap and print errors
-  // during system initialization time. Hence the error
-  // message is quite ugly
   /**
+   * this function is used to trap and print errors
+   * during system initialization time. Hence the error
+   * message is quite ugly
+   *
    * @param $pearError
    */
   public static function simpleHandler($pearError) {
 
     // create the error array
-    $error               = array();
-    $error['callback']   = $pearError->getCallback();
-    $error['code']       = $pearError->getCode();
-    $error['message']    = $pearError->getMessage();
-    $error['mode']       = $pearError->getMode();
+    $error = array();
+    $error['callback'] = $pearError->getCallback();
+    $error['code'] = $pearError->getCode();
+    $error['message'] = $pearError->getMessage();
+    $error['mode'] = $pearError->getMode();
     $error['debug_info'] = $pearError->getDebugInfo();
-    $error['type']       = $pearError->getType();
-    $error['user_info']  = $pearError->getUserInfo();
-    $error['to_string']  = $pearError->toString();
+    $error['type'] = $pearError->getType();
+    $error['user_info'] = $pearError->getUserInfo();
+    $error['to_string'] = $pearError->toString();
 
     // ensure that debug does not check permissions since we are in bootstrap
     // mode and need to print a decent message to help the user
@@ -290,7 +286,6 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * Also, if we do not return any value the PEAR_ErrorStack::push() then does the
    * action of PEAR_ERRORSTACK_PUSHANDLOG which displays the errors on the screen,
    * since the logger set for this error stack is 'display' - see CRM_Core_Config::getLog();
-   *
    */
   public static function handlePES($pearError) {
     return PEAR_ERRORSTACK_PUSH;
@@ -299,14 +294,16 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   /**
    * Display an error page with an error message describing what happened
    *
-   * @param string $message the error message
-   * @param string $code the error code if any
-   * @param string $email the email address to notify of this situation
+   * @param string $message
+   *   The error message.
+   * @param string $code
+   *   The error code if any.
+   * @param string $email
+   *   The email address to notify of this situation.
    *
    * @throws Exception
    *
    * @return void
-   * @static
    */
   public static function fatal($message = NULL, $code = NULL, $email = NULL) {
     $vars = array(
@@ -322,7 +319,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
       $details = 'A fatal error was triggered';
       if ($message) {
         $details .= ': ' . $message;
-    }
+      }
       throw new Exception($details, $code);
     }
 
@@ -393,12 +390,12 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * @param Exception $exception
    *
    * @return void
-   * @static
    */
   public static function handleUnhandledException($exception) {
     try {
       CRM_Utils_Hook::unhandledException($exception);
-    } catch (Exception $other) {
+    }
+    catch (Exception $other) {
       // if the exception-handler generates an exception, then that sucks! oh, well. carry on.
       CRM_Core_Error::debug_var('handleUnhandledException_nestedException', self::formatTextException($other));
     }
@@ -465,16 +462,16 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * Outputs pre-formatted debug information. Flushes the buffers
    * so we can interrupt a potential POST/redirect
    *
-   * @param  string name of debug section
-   * @param  mixed  reference to variables that we need a trace of
-   * @param  bool   should we log or return the output
-   * @param  bool   whether to generate a HTML-escaped output
-   * @param  bool   should we check permissions before displaying output
+   * @param string $name name of debug section
+   * @param $variable mixed reference to variables that we need a trace of
+   * @param bool $log should we log or return the output
+   * @param bool $html whether to generate a HTML-escaped output
+   * @param bool $checkPermission should we check permissions before displaying output
    *                useful when we die during initialization and permissioning
    *                subsystem is not initialized - CRM-13765
    *
-   * @return string the generated output
-   * @static
+   * @return string
+   *   the generated output
    */
   public static function debug($name, $variable = NULL, $log = TRUE, $html = TRUE, $checkPermission = TRUE) {
     $error = self::singleton();
@@ -515,23 +512,27 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    *
    * @param string $variable_name
    * @param mixed $variable
-   * @param bool $print should we use print_r ? (else we use var_dump)
-   * @param bool $log should we log or return the output
-   * @param string $comp variable name
+   * @param bool $print
+   *   Should we use print_r ? (else we use var_dump).
+   * @param bool $log
+   *   Should we log or return the output.
+   * @param string $comp
+   *   Variable name.
    *
-   * @return string the generated output
+   * @return string
+   *   the generated output
    *
    *
-   * @static
    *
    * @see CRM_Core_Error::debug()
    * @see CRM_Core_Error::debug_log_message()
    */
-  static function debug_var($variable_name,
+  public static function debug_var(
+    $variable_name,
     $variable,
     $print = TRUE,
-    $log   = TRUE,
-    $comp  = ''
+    $log = TRUE,
+    $comp = ''
   ) {
     // check if variable is set
     if (!isset($variable)) {
@@ -562,13 +563,15 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * Display the error message on terminal
    *
    * @param $message
-   * @param bool $out should we log or return the output
+   * @param bool $out
+   *   Should we log or return the output.
    *
-   * @param string $comp message to be output
-   * @return string format of the backtrace
+   * @param string $comp
+   *   Message to be output.
+   * @return string
+   *   format of the backtrace
    *
    *
-   * @static
    */
   public static function debug_log_message($message, $out = FALSE, $comp = '') {
     $config = CRM_Core_Config::singleton();
@@ -595,11 +598,12 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * Append to the query log (if enabled)
    */
   public static function debug_query($string) {
-    if ( defined( 'CIVICRM_DEBUG_LOG_QUERY' ) ) {
-      if ( CIVICRM_DEBUG_LOG_QUERY == 'backtrace' ) {
-        CRM_Core_Error::backtrace( $string, true );
-      } else if ( CIVICRM_DEBUG_LOG_QUERY ) {
-        CRM_Core_Error::debug_var( 'Query', $string, false, true );
+    if (defined('CIVICRM_DEBUG_LOG_QUERY')) {
+      if (CIVICRM_DEBUG_LOG_QUERY == 'backtrace') {
+        CRM_Core_Error::backtrace($string, TRUE);
+      }
+      elseif (CIVICRM_DEBUG_LOG_QUERY) {
+        CRM_Core_Error::debug_var('Query', $string, FALSE, TRUE);
       }
     }
   }
@@ -613,7 +617,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     $dao = CRM_Core_DAO::executeQuery($query);
     $results = array();
     while ($dao->fetch()) {
-      $results[] = (array)$dao;
+      $results[] = (array) $dao;
     }
     CRM_Core_Error::debug_var('dao result', array('query' => $query, 'results' => $results));
   }
@@ -671,26 +675,33 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   /**
    * Render a backtrace array as a string
    *
-   * @param array $backTrace array of stack frames
-   * @param boolean $showArgs TRUE if we should try to display content of function arguments (which could be sensitive); FALSE to display only the type of each function argument
-   * @param int $maxArgLen maximum number of characters to show from each argument string
-   * @return string printable plain-text
+   * @param array $backTrace
+   *   Array of stack frames.
+   * @param bool $showArgs
+   *   TRUE if we should try to display content of function arguments (which could be sensitive); FALSE to display only the type of each function argument.
+   * @param int $maxArgLen
+   *   Maximum number of characters to show from each argument string.
+   * @return string
+   *   printable plain-text
    */
   public static function formatBacktrace($backTrace, $showArgs = TRUE, $maxArgLen = 80) {
     $message = '';
     foreach (self::parseBacktrace($backTrace, $showArgs, $maxArgLen) as $idx => $trace) {
       $message .= sprintf("#%s %s\n", $idx, $trace);
     }
-    $message .= sprintf("#%s {main}\n", 1+$idx);
+    $message .= sprintf("#%s {main}\n", 1 + $idx);
     return $message;
   }
 
   /**
    * Render a backtrace array as an array
    *
-   * @param array $backTrace array of stack frames
-   * @param boolean $showArgs TRUE if we should try to display content of function arguments (which could be sensitive); FALSE to display only the type of each function argument
-   * @param int $maxArgLen maximum number of characters to show from each argument string
+   * @param array $backTrace
+   *   Array of stack frames.
+   * @param bool $showArgs
+   *   TRUE if we should try to display content of function arguments (which could be sensitive); FALSE to display only the type of each function argument.
+   * @param int $maxArgLen
+   *   Maximum number of characters to show from each argument string.
    * @return array
    * @see debug_backtrace
    * @see Exception::getTrace()
@@ -707,7 +718,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
 
       if (!empty($trace['args'])) {
         foreach ($trace['args'] as $arg) {
-          if (! $showArgs || $skipArgs) {
+          if (!$showArgs || $skipArgs) {
             $args[] = '(' . gettype($arg) . ')';
             continue;
           }
@@ -715,25 +726,32 @@ class CRM_Core_Error extends PEAR_ErrorStack {
             case 'boolean':
               $args[] = $arg ? 'TRUE' : 'FALSE';
               break;
+
             case 'integer':
             case 'double':
               $args[] = $arg;
               break;
+
             case 'string':
-              $args[] = '"' . CRM_Utils_String::ellipsify(addcslashes((string) $arg, "\r\n\t\""), $maxArgLen). '"';
+              $args[] = '"' . CRM_Utils_String::ellipsify(addcslashes((string) $arg, "\r\n\t\""), $maxArgLen) . '"';
               break;
+
             case 'array':
-              $args[] = '(Array:'.count($arg).')';
+              $args[] = '(Array:' . count($arg) . ')';
               break;
+
             case 'object':
               $args[] = 'Object(' . get_class($arg) . ')';
               break;
+
             case 'resource':
               $args[] = 'Resource';
               break;
+
             case 'NULL':
               $args[] = 'NULL';
               break;
+
             default:
               $args[] = "($type)";
               break;
@@ -757,7 +775,8 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * Render an exception as HTML string
    *
    * @param Exception $e
-   * @return string printable HTML text
+   * @return string
+   *   printable HTML text
    */
   public static function formatHtmlException(Exception $e) {
     $msg = '';
@@ -774,13 +793,14 @@ class CRM_Core_Error extends PEAR_ErrorStack {
           $msg .= '<tbody>';
           foreach (array('Type', 'Code', 'Message', 'Mode', 'UserInfo', 'DebugInfo') as $f) {
             $msg .= sprintf('<tr><td>%s</td><td>%s</td></tr>', $f, call_user_func(array($ei->getCause(), "get$f")));
-    }
+          }
           $msg .= '</tbody></table>';
-    }
+        }
         $ei = $ei->getCause();
-  }
+      }
       $msg .= $e->toHtml();
-    } else {
+    }
+    else {
       $msg .= '<p><b>' . get_class($e) . ': "' . htmlentities($e->getMessage()) . '"</b></p>';
       $msg .= '<pre>' . htmlentities(self::formatBacktrace($e->getTrace())) . '</pre>';
     }
@@ -791,7 +811,8 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * Write details of an exception to the log
    *
    * @param Exception $e
-   * @return string printable plain text
+   * @return string
+   *   printable plain text
    */
   public static function formatTextException(Exception $e) {
     $msg = get_class($e) . ": \"" . $e->getMessage() . "\"\n";
@@ -826,12 +847,12 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   /**
    * Set a status message in the session, then bounce back to the referrer.
    *
-   * @param string $status The status message to set
+   * @param string $status
+   *   The status message to set.
    *
    * @param null $redirect
    * @param string $title
    * @return void
-   * @static
    */
   public static function statusBounce($status, $redirect = NULL, $title = NULL) {
     $session = CRM_Core_Session::singleton();
@@ -851,7 +872,6 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   /**
    * Reset the error stack
    *
-   * @static
    */
   public static function reset() {
     $error = self::singleton();
@@ -873,9 +893,10 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   /**
    * PEAR error-handler to quietly catch otherwise fatal errors. Intended for use with smtp transport.
    *
-   * @param object $obj       The PEAR_ERROR object
-   * @return object $obj
-   * @static
+   * @param object $obj
+   *   The PEAR_ERROR object.
+   * @return object
+   *   $obj
    */
   public static function nullHandler($obj) {
     CRM_Core_Error::debug_log_message("Ignoring exception thrown by nullHandler: {$obj->code}, {$obj->message}");
@@ -883,12 +904,11 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     return $obj;
   }
 
-  /*
+  /**
    * @deprecated
    * This function is no longer used by v3 api.
    * @fixme Some core files call it but it should be re-thought & renamed or removed
-   */
-  /**
+   *
    * @param $msg
    * @param null $data
    *
@@ -934,8 +954,8 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * @param $error
-   * @param const $type
+   * @param array $error
+   * @param int $type
    *
    * @return bool
    */
@@ -948,6 +968,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     }
     return FALSE;
   }
+
 }
 
 $e = new PEAR_ErrorStack('CRM');