Merge pull request #5513 from mallezie/contact-select-file-16178
[civicrm-core.git] / CRM / Utils / Token.php
index 90fb4ce49a0a8d74b0bd458f7997e93595d81d3b..b549e4aa9f1f6ae764e6456ca3b73bb8fd975f0f 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id: $
  *
  */
@@ -90,17 +90,11 @@ class CRM_Utils_Token {
     'welcome' => array('group'),
   );
 
+
   /**
-   * Check a string (mailing body) for required tokens.
-   *
-   * @param string $str
-   *   The message.
-   *
-   * @return true|array           true if all required tokens are found,
-   *                              else an array of the missing tokens
-   * @static
+   * @return array
    */
-  public static function requiredTokens(&$str) {
+  public static function getRequiredTokens() {
     if (self::$_requiredTokens == NULL) {
       self::$_requiredTokens = array(
         'domain.address' => ts("Domain address - displays your organization's postal address."),
@@ -112,9 +106,24 @@ class CRM_Utils_Token {
         ),
       );
     }
+    return self::$_requiredTokens;
+  }
+
+  /**
+   * Check a string (mailing body) for required tokens.
+   *
+   * @param string $str
+   *   The message.
+   *
+   * @return bool|array
+   *    true if all required tokens are found,
+   *    else an array of the missing tokens
+   */
+  public static function requiredTokens(&$str) {
+    $requiredTokens = self::getRequiredTokens();
 
     $missing = array();
-    foreach (self::$_requiredTokens as $token => $value) {
+    foreach ($requiredTokens as $token => $value) {
       if (!is_array($value)) {
         if (!preg_match('/(^|[^\{])' . preg_quote('{' . $token . '}') . '/', $str)) {
           $missing[$token] = $value;
@@ -142,7 +151,7 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Wrapper for token matching
+   * Wrapper for token matching.
    *
    * @param string $type
    *   The token type (domain,mailing,contact,action).
@@ -151,8 +160,8 @@ class CRM_Utils_Token {
    * @param string $str
    *   The string to search.
    *
-   * @return boolean          Was there a match
-   * @static
+   * @return bool
+   *   Was there a match
    */
   public static function token_match($type, $var, &$str) {
     $token = preg_quote('{' . "$type.$var") . '(\|.+?)?' . preg_quote('}');
@@ -160,7 +169,7 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Wrapper for token replacing
+   * Wrapper for token replacing.
    *
    * @param string $type
    *   The token type.
@@ -172,8 +181,8 @@ class CRM_Utils_Token {
    *
    * @param bool $escapeSmarty
    *
-   * @return string           The processed string
-   * @static
+   * @return string
+   *   The processed string
    */
   public static function &token_replace($type, $var, $value, &$str, $escapeSmarty = FALSE) {
     $token = preg_quote('{' . "$type.$var") . '(\|([^\}]+?))?' . preg_quote('}');
@@ -193,21 +202,21 @@ class CRM_Utils_Token {
    * @param string $token_type
    *   A string indicating the the type of token to be used in the expression.
    *
-   * @return string           regular expression sutiable for using in preg_replace
-   * @static
+   * @return string
+   *   regular expression sutiable for using in preg_replace
    */
   private static function tokenRegex($token_type) {
     return '/(?<!\{|\\\\)\{' . $token_type . '\.([\w]+(\-[\w\s]+)?)\}(?!\})/';
   }
 
   /**
-   * Escape the string so a malicious user cannot inject smarty code into the template
+   * Escape the string so a malicious user cannot inject smarty code into the template.
    *
    * @param string $string
    *   A string that needs to be escaped from smarty parsing.
    *
-   * @return string           the escaped string
-   * @static
+   * @return string
+   *   the escaped string
    */
   private static function tokenEscapeSmarty($string) {
     // need to use negative look-behind, as both str_replace() and preg_replace() are sequential
@@ -227,8 +236,8 @@ class CRM_Utils_Token {
    * @param null $knownTokens
    * @param bool $escapeSmarty
    *
-   * @return string           The processed string
-   * @static
+   * @return string
+   *   The processed string
    */
   public static function &replaceDomainTokens(
     $str,
@@ -239,14 +248,15 @@ class CRM_Utils_Token {
   ) {
     $key = 'domain';
     if (
-      !$knownTokens || empty($knownTokens[$key])) {
+      !$knownTokens || empty($knownTokens[$key])
+    ) {
       return $str;
     }
 
     $str = preg_replace_callback(
       self::tokenRegex($key),
-      function ($matches) use(&$domain, $html, $escapeSmarty) {
-      return CRM_Utils_Token::getDomainTokenReplacement($matches[1], $domain, $html, $escapeSmarty);
+      function ($matches) use (&$domain, $html, $escapeSmarty) {
+        return CRM_Utils_Token::getDomainTokenReplacement($matches[1], $domain, $html, $escapeSmarty);
       },
       $str
     );
@@ -327,12 +337,12 @@ class CRM_Utils_Token {
    *
    * @param bool $escapeSmarty
    *
-   * @return string           The processed string
-   * @static
+   * @return string
+   *   The processed string
    */
   public static function &replaceOrgTokens($str, &$org, $html = FALSE, $escapeSmarty = FALSE) {
-    self::$_tokens['org'] =
-      array_merge(
+    self::$_tokens['org']
+      array_merge(
         array_keys(CRM_Contact_BAO_Contact::importableFields('Organization')),
         array('address', 'display_name', 'checksum', 'contact_id')
       );
@@ -410,8 +420,8 @@ class CRM_Utils_Token {
    * @param null $knownTokens
    * @param bool $escapeSmarty
    *
-   * @return string           The processed sstring
-   * @static
+   * @return string
+   *   The processed sstring
    */
   public static function &replaceMailingTokens(
     $str,
@@ -427,8 +437,8 @@ class CRM_Utils_Token {
 
     $str = preg_replace_callback(
       self::tokenRegex($key),
-      function ($matches) use(&$mailing, $escapeSmarty) {
-      return CRM_Utils_Token::getMailingTokenReplacement($matches[1], $mailing, $escapeSmarty);
+      function ($matches) use (&$mailing, $escapeSmarty) {
+        return CRM_Utils_Token::getMailingTokenReplacement($matches[1], $mailing, $escapeSmarty);
       },
       $str
     );
@@ -544,8 +554,8 @@ class CRM_Utils_Token {
    *
    * @param bool $escapeSmarty
    *
-   * @return string             The processed string
-   * @static
+   * @return string
+   *   The processed string
    */
   public static function &replaceActionTokens(
     $str,
@@ -566,8 +576,8 @@ class CRM_Utils_Token {
 
     $str = preg_replace_callback(
       self::tokenRegex($key),
-      function ($matches) use(&$addresses, &$urls, $html, $escapeSmarty) {
-      return CRM_Utils_Token::getActionTokenReplacement($matches[1], $addresses, $urls, $html, $escapeSmarty);
+      function ($matches) use (&$addresses, &$urls, $html, $escapeSmarty) {
+        return CRM_Utils_Token::getActionTokenReplacement($matches[1], $addresses, $urls, $html, $escapeSmarty);
       },
       $str
     );
@@ -591,7 +601,7 @@ class CRM_Utils_Token {
     $escapeSmarty = FALSE
   ) {
     /* If the token is an email action, use it.  Otherwise, find the
-         * appropriate URL */
+     * appropriate URL */
 
     if (!in_array($token, self::$_tokens['action'])) {
       $value = "{action.$token}";
@@ -637,8 +647,8 @@ class CRM_Utils_Token {
    *
    * @param bool $escapeSmarty
    *
-   * @return string                    The processed string
-   * @static
+   * @return string
+   *   The processed string
    */
   public static function &replaceContactTokens(
     $str,
@@ -652,8 +662,8 @@ class CRM_Utils_Token {
     if (self::$_tokens[$key] == NULL) {
       /* This should come from UF */
 
-      self::$_tokens[$key] =
-        array_merge(
+      self::$_tokens[$key]
+        array_merge(
           array_keys(CRM_Contact_BAO_Contact::exportableFields('All')),
           array('checksum', 'contact_id')
         );
@@ -669,8 +679,8 @@ class CRM_Utils_Token {
 
     $str = preg_replace_callback(
       self::tokenRegex($key),
-      function ($matches) use(&$contact, $html, $returnBlankToken, $escapeSmarty) {
-      return CRM_Utils_Token::getContactTokenReplacement($matches[1], $contact, $html, $returnBlankToken, $escapeSmarty);
+      function ($matches) use (&$contact, $html, $returnBlankToken, $escapeSmarty) {
+        return CRM_Utils_Token::getContactTokenReplacement($matches[1], $contact, $html, $returnBlankToken, $escapeSmarty);
       },
       $str
     );
@@ -698,8 +708,8 @@ class CRM_Utils_Token {
     if (self::$_tokens['contact'] == NULL) {
       /* This should come from UF */
 
-      self::$_tokens['contact'] =
-        array_merge(
+      self::$_tokens['contact']
+        array_merge(
           array_keys(CRM_Contact_BAO_Contact::exportableFields('All')),
           array('checksum', 'contact_id')
         );
@@ -760,7 +770,10 @@ class CRM_Utils_Token {
     }
 
     if ($escapeSmarty
-        && !($returnBlankToken && $noReplace)) { // $returnBlankToken means the caller wants to do further attempts at processing unreplaced tokens -- so don't escape them yet in this case.
+      && !($returnBlankToken && $noReplace)
+    ) {
+      // $returnBlankToken means the caller wants to do further attempts at
+      // processing unreplaced tokens -- so don't escape them yet in this case.
       $value = self::tokenEscapeSmarty($value);
     }
 
@@ -781,8 +794,8 @@ class CRM_Utils_Token {
    *
    * @param bool $escapeSmarty
    *
-   * @return string             The processed string
-   * @static
+   * @return string
+   *   The processed string
    */
   public static function &replaceHookTokens(
     $str,
@@ -794,8 +807,8 @@ class CRM_Utils_Token {
     foreach ($categories as $key) {
       $str = preg_replace_callback(
         self::tokenRegex($key),
-        function ($matches) use(&$contact, $key, $html, $escapeSmarty) {
-        return CRM_Utils_Token::getHookTokenReplacement($matches[1], $contact, $key, $html, $escapeSmarty);
+        function ($matches) use (&$contact, $key, $html, $escapeSmarty) {
+          return CRM_Utils_Token::getHookTokenReplacement($matches[1], $contact, $key, $html, $escapeSmarty);
         },
         $str
       );
@@ -804,11 +817,12 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Parse html through Smarty resolving any smarty functions
+   * Parse html through Smarty resolving any smarty functions.
    * @param string $tokenHtml
    * @param array $entity
    * @param string $entityType
-   * @return string html parsed through smarty
+   * @return string
+   *   html parsed through smarty
    */
   public static function parseThroughSmarty($tokenHtml, $entity, $entityType = 'contact') {
     if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
@@ -856,16 +870,15 @@ class CRM_Utils_Token {
    *  this routine will remove the extra backslashes and braces
    *
    * @param $str ref to the string that will be scanned and modified
-   * @return void  this function works directly on the string that is passed
-   * @access public
-   * @static
+   * @return void
+   *   this function works directly on the string that is passed
    */
   public static function unescapeTokens(&$str) {
     $str = preg_replace('/\\\\|\{(\{\w+\.\w+\})\}/', '\\1', $str);
   }
 
   /**
-   * Replace unsubscribe tokens
+   * Replace unsubscribe tokens.
    *
    * @param string $str
    *   The string with tokens to be replaced.
@@ -877,10 +890,10 @@ class CRM_Utils_Token {
    *   Replace tokens with html or plain text.
    * @param int $contact_id
    *   The contact ID.
-   * @param string hash The security hash of the unsub event
+   * @param string $hash The security hash of the unsub event
    *
-   * @return string               The processed string
-   * @static
+   * @return string
+   *   The processed string
    */
   public static function &replaceUnsubscribeTokens(
     $str,
@@ -911,7 +924,7 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Replace resubscribe tokens
+   * Replace resubscribe tokens.
    *
    * @param string $str
    *   The string with tokens to be replaced.
@@ -923,10 +936,10 @@ class CRM_Utils_Token {
    *   Replace tokens with html or plain text.
    * @param int $contact_id
    *   The contact ID.
-   * @param string hash The security hash of the resub event
+   * @param string $hash The security hash of the resub event
    *
-   * @return string               The processed string
-   * @static
+   * @return string
+   *   The processed string
    */
   public static function &replaceResubscribeTokens(
     $str, &$domain, &$groups, $html,
@@ -952,8 +965,8 @@ class CRM_Utils_Token {
    * @param bool $html
    *   Replace tokens with html or plain text.
    *
-   * @return string               The processed string
-   * @static
+   * @return string
+   *   The processed string
    */
   public static function &replaceSubscribeTokens($str, $group, $url, $html) {
     if (self::token_match('subscribe', 'group', $str)) {
@@ -971,8 +984,8 @@ class CRM_Utils_Token {
    * @param string $str
    *   The string with tokens to be replaced.
    *
-   * @return string               The processed string
-   * @static
+   * @return string
+   *   The processed string
    */
   public static function &replaceSubscribeInviteTokens($str) {
     if (preg_match('/\{action\.subscribeUrl\}/', $str)) {
@@ -997,9 +1010,9 @@ class CRM_Utils_Token {
 
     if (preg_match('/\{action\.subscribe.\d+\}/', $str, $matches)) {
       foreach ($matches as $key => $value) {
-        $gid       = substr($value, 18, -1);
-        $config    = CRM_Core_Config::singleton();
-        $domain    = CRM_Core_BAO_MailSettings::defaultDomain();
+        $gid = substr($value, 18, -1);
+        $config = CRM_Core_Config::singleton();
+        $domain = CRM_Core_BAO_MailSettings::defaultDomain();
         $localpart = CRM_Core_BAO_MailSettings::defaultLocalpart();
         // we add the 0.0000000000000000 part to make this match the other email patterns (with action, two ids and a hash)
         $str = preg_replace('/' . preg_quote($value) . '/', "mailto:{$localpart}s.{$gid}.0.0000000000000000@$domain", $str);
@@ -1018,8 +1031,8 @@ class CRM_Utils_Token {
    * @param bool $html
    *   Replace tokens with html or plain text.
    *
-   * @return string               The processed string
-   * @static
+   * @return string
+   *   The processed string
    */
   public static function &replaceWelcomeTokens($str, $group, $html) {
     if (self::token_match('welcome', 'group', $str)) {
@@ -1034,8 +1047,8 @@ class CRM_Utils_Token {
    * @param string $str
    *   The string to search.
    *
-   * @return array            Array of tokens that weren't replaced
-   * @static
+   * @return array
+   *   Array of tokens that weren't replaced
    */
   public static function &unmatchedTokens(&$str) {
     //preg_match_all('/[^\{\\\\]\{(\w+\.\w+)\}[^\}]/', $str, $match);
@@ -1044,7 +1057,7 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Find and replace tokens for each component
+   * Find and replace tokens for each component.
    *
    * @param string $str
    *   The string to search.
@@ -1056,8 +1069,8 @@ class CRM_Utils_Token {
    * @param bool $escapeSmarty
    * @param bool $returnEmptyToken
    *
-   * @return string           The processed string
-   * @static
+   * @return string
+   *   The processed string
    */
   public static function &replaceComponentTokens(&$str, $contact, $components, $escapeSmarty = FALSE, $returnEmptyToken = TRUE) {
     if (!is_array($components) || empty($contact)) {
@@ -1083,13 +1096,13 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Get array of string tokens
+   * Get array of string tokens.
    *
-   * @param $string
+   * @param string $string
    *   The input string to parse for tokens.
    *
-   * @return array $tokens array of tokens mentioned in field@access public
-   * @static
+   * @return array
+   *   array of tokens mentioned in field
    */
   public static function getTokens($string) {
     $matches = array();
@@ -1118,17 +1131,17 @@ class CRM_Utils_Token {
    * Function to determine which values to retrieve to insert into tokens. The heavy resemblance between this function
    * and getTokens appears to be historical rather than intentional and should be reviewed
    * @param $string
-   * @return array fields to pass in as return properties when populating token
-   *
+   * @return array
+   *   fields to pass in as return properties when populating token
    */
   public static function getReturnProperties(&$string) {
     $returnProperties = array();
     $matches = array();
     preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/',
-        $string,
-        $matches,
-        PREG_PATTERN_ORDER
-      );
+      $string,
+      $matches,
+      PREG_PATTERN_ORDER
+    );
     if ($matches[1]) {
       foreach ($matches[1] as $token) {
         list($type, $name) = preg_split('/\./', $token, 2);
@@ -1148,9 +1161,9 @@ class CRM_Utils_Token {
    * @param $contactIDs
    * @param array $returnProperties
    *   Of required properties.
-   * @param bool $skipOnHoldDon't return on_hold contact info also.
+   * @param bool $skipOnHold Don't return on_hold contact info also.
    *   Don't return on_hold contact info also.
-   * @param bool $skipDeceasedDon't return deceased contact info.
+   * @param bool $skipDeceased Don't return deceased contact info.
    *   Don't return deceased contact info.
    * @param array $extraParams
    *   Extra params.
@@ -1161,9 +1174,8 @@ class CRM_Utils_Token {
    *   The mailing list jobID - this is a legacy param.
    *
    * @return array
-   * @static
    */
-  static function getTokenDetails(
+  public static function getTokenDetails(
     $contactIDs,
     $returnProperties = NULL,
     $skipOnHold = TRUE,
@@ -1182,7 +1194,10 @@ class CRM_Utils_Token {
     foreach ($contactIDs as $key => $contactID) {
       $params[] = array(
         CRM_Core_Form::CB_PREFIX . $contactID,
-        '=', 1, 0, 0,
+        '=',
+        1,
+        0,
+        0,
       );
     }
 
@@ -1256,7 +1271,10 @@ class CRM_Utils_Token {
 
         //special case for greeting replacement
         foreach (array(
-          'email_greeting', 'postal_greeting', 'addressee') as $val) {
+                   'email_greeting',
+                   'postal_greeting',
+                   'addressee',
+                 ) as $val) {
           if (!empty($contactDetails[$contactID][$val])) {
             $contactDetails[$contactID][$val] = $contactDetails[$contactID]["{$val}_display"];
           }
@@ -1280,7 +1298,7 @@ class CRM_Utils_Token {
    *
    * @param array $contactIDs
    *   This should always be array(0) or its not anonymous - left to keep signature same.
-   * as main fn
+   *   as main fn
    * @param string $returnProperties
    * @param bool $skipOnHold
    * @param bool $skipDeceased
@@ -1289,17 +1307,19 @@ class CRM_Utils_Token {
    * @param string $className
    *   Sent as context to the hook.
    * @param string $jobID
-   * @return array contactDetails with hooks swapped out
+   * @return array
+   *   contactDetails with hooks swapped out
    */
   public function getAnonymousTokenDetails($contactIDs = array(
-    0),
-    $returnProperties = NULL,
-    $skipOnHold = TRUE,
-    $skipDeceased = TRUE,
-    $extraParams = NULL,
-    $tokens = array(),
-    $className = NULL,
-    $jobID = NULL) {
+      0,
+    ),
+                                           $returnProperties = NULL,
+                                           $skipOnHold = TRUE,
+                                           $skipDeceased = TRUE,
+                                           $extraParams = NULL,
+                                           $tokens = array(),
+                                           $className = NULL,
+                                           $jobID = NULL) {
     $details = array(0 => array());
     // also call a hook and get token details
     CRM_Utils_Hook::tokenValues($details[0],
@@ -1325,16 +1345,16 @@ class CRM_Utils_Token {
    * @param string $className
    *
    * @return array
-   * @static
    */
-  static function getContributionTokenDetails(
+  public static function getContributionTokenDetails(
     $contributionIDs,
     $returnProperties = NULL,
     $extraParams = NULL,
     $tokens = array(),
     $className = NULL
   ) {
-    //@todo - this function basically replications calling civicrm_api3('contribution', 'get', array('id' => array('IN' => array())
+    //@todo - this function basically replicates calling
+    //civicrm_api3('contribution', 'get', array('id' => array('IN' => array())
     if (empty($contributionIDs)) {
       // putting a fatal here so we can track if/when this happens
       CRM_Core_Error::fatal();
@@ -1379,17 +1399,20 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Get Membership Token Details
+   * Get Membership Token Details.
    * @param array $membershipIDs
    *   Array of membership IDS.
    */
   public static function getMembershipTokenDetails($membershipIDs) {
-    $memberships = civicrm_api3('membership', 'get', array('options' => array('limit' => 200000), 'membership_id' => array('IN' => (array) $membershipIDs)));
+    $memberships = civicrm_api3('membership', 'get', array(
+        'options' => array('limit' => 200000),
+        'membership_id' => array('IN' => (array) $membershipIDs),
+      ));
     return $memberships['values'];
   }
+
   /**
    * Replace greeting tokens exists in message/subject
-   *
    */
   public static function replaceGreetingTokens(&$tokenString, $contactDetails = NULL, $contactId = NULL, $className = NULL, $escapeSmarty = FALSE) {
 
@@ -1409,7 +1432,8 @@ class CRM_Utils_Token {
       // check if there are any unevaluated tokens
       $greetingTokens = self::getTokens($tokenString);
 
-      // $greetingTokens not empty, means there are few tokens which are not evaluated, like custom data etc
+      // $greetingTokens not empty, means there are few tokens which are not
+      // evaluated, like custom data etc
       // so retrieve it from database
       if (!empty($greetingTokens) && array_key_exists('contact', $greetingTokens)) {
         $greetingsReturnProperties = array_flip(CRM_Utils_Array::value('contact', $greetingTokens));
@@ -1472,7 +1496,10 @@ class CRM_Utils_Token {
     $flattenTokens = array();
 
     foreach (array(
-      'html', 'text', 'subject') as $prop) {
+               'html',
+               'text',
+               'subject',
+             ) as $prop) {
       if (!isset($tokens[$prop])) {
         continue;
       }
@@ -1498,8 +1525,8 @@ class CRM_Utils_Token {
    * @param null $knownTokens
    * @param bool $escapeSmarty
    *
-   * @return string           The processed string
-   * @static
+   * @return string
+   *   The processed string
    */
   public static function &replaceUserTokens($str, $knownTokens = NULL, $escapeSmarty = FALSE) {
     $key = 'user';
@@ -1511,8 +1538,8 @@ class CRM_Utils_Token {
 
     $str = preg_replace_callback(
       self::tokenRegex($key),
-      function ($matches) use($escapeSmarty) {
-      return CRM_Utils_Token::getUserTokenReplacement($matches[1], $escapeSmarty);
+      function ($matches) use ($escapeSmarty) {
+        return CRM_Utils_Token::getUserTokenReplacement($matches[1], $escapeSmarty);
       },
       $str
     );
@@ -1548,19 +1575,18 @@ class CRM_Utils_Token {
   }
 
   /**
-   *
    */
   protected static function _buildContributionTokens() {
     $key = 'contribution';
     if (self::$_tokens[$key] == NULL) {
       self::$_tokens[$key] = array_keys(array_merge(CRM_Contribute_BAO_Contribution::exportableFields('All'),
-          array('campaign', 'financial_type')
-        ));
+        array('campaign', 'financial_type')
+      ));
     }
   }
 
   /**
-   * Store membership tokens on the static _tokens array
+   * Store membership tokens on the static _tokens array.
    */
   protected static function _buildMembershipTokens() {
     $key = 'membership';
@@ -1575,7 +1601,7 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Replace tokens for an entity
+   * Replace tokens for an entity.
    * @param string $entity
    * @param array $entityArray
    *   (e.g. in format from api).
@@ -1584,7 +1610,8 @@ class CRM_Utils_Token {
    * @param array $knownTokens
    *   Array of tokens present.
    * @param bool $escapeSmarty
-   * @return string string with replacements made
+   * @return string
+   *   string with replacements made
    */
   public static function replaceEntityTokens($entity, $entityArray, $str, $knownTokens = array(), $escapeSmarty = FALSE) {
     if (!$knownTokens || empty($knownTokens[$entity])) {
@@ -1602,7 +1629,7 @@ class CRM_Utils_Token {
   }
 
   /**
-   * Replace Contribution tokens in html
+   * Replace Contribution tokens in html.
    *
    * @param string $str
    * @param array $contribution
@@ -1610,7 +1637,7 @@ class CRM_Utils_Token {
    * @param string $knownTokens
    * @param bool|string $escapeSmarty
    *
-   * @return unknown|Ambigous <string, mixed>|mixed
+   * @return mixed
    */
   public static function replaceContributionTokens($str, &$contribution, $html = FALSE, $knownTokens = NULL, $escapeSmarty = FALSE) {
     $key = 'contribution';
@@ -1626,8 +1653,8 @@ class CRM_Utils_Token {
 
     $str = preg_replace_callback(
       self::tokenRegex($key),
-      function ($matches) use(&$contribution, $html, $escapeSmarty) {
-      return CRM_Utils_Token::getContributionTokenReplacement($matches[1], $contribution, $html, $escapeSmarty);
+      function ($matches) use (&$contribution, $html, $escapeSmarty) {
+        return CRM_Utils_Token::getContributionTokenReplacement($matches[1], $contribution, $html, $escapeSmarty);
       },
       $str
     );
@@ -1679,7 +1706,8 @@ class CRM_Utils_Token {
    * @param array $membership
    *   An api result array for a single membership.
    * @param bool $escapeSmarty
-   * @return string token replacement
+   * @return string
+   *   token replacement
    */
   public static function getMembershipTokenReplacement($token, $membership, $escapeSmarty = FALSE) {
     $entity = 'membership';
@@ -1696,7 +1724,10 @@ class CRM_Utils_Token {
 
       case 'fee':
         try {
-          $value = civicrm_api3('membership_type', 'getvalue', array('id' => $membership['membership_type_id'], 'return' => 'minimum_fee'));
+          $value = civicrm_api3('membership_type', 'getvalue', array(
+              'id' => $membership['membership_type_id'],
+              'return' => 'minimum_fee',
+            ));
         }
         catch (CiviCRM_API3_Exception $e) {
           // we can anticipate we will get an error if the minimum fee is set to 'NULL' because of the way the
@@ -1763,7 +1794,8 @@ class CRM_Utils_Token {
   }
 
   /**
-   * @return array: legacy_token => new_token
+   * @return array
+   *   legacy_token => new_token
    */
   public static function legacyContactTokens() {
     return array(
@@ -1776,6 +1808,7 @@ class CRM_Utils_Token {
 
   /**
    * Formats a token list for the select2 widget
+   *
    * @param $tokens
    * @return array
    */
@@ -1813,4 +1846,5 @@ class CRM_Utils_Token {
 
     return $output;
   }
+
 }