Skip to content

Commit

Permalink
Merge pull request #13985 from seamuslee001/new_coder_crm_utils
Browse files Browse the repository at this point in the history
(NFC) Bring CRM/Utils folder up to future coder standards
  • Loading branch information
eileenmcnaughton authored Apr 7, 2019
2 parents 5a25f6f + 6714d8d commit de0e753
Show file tree
Hide file tree
Showing 72 changed files with 397 additions and 238 deletions.
4 changes: 2 additions & 2 deletions CRM/Utils/API/AbstractFieldCoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function isSkippedField($fldName) {
*
* @param array|string $values the field value from the API
*/
public abstract function encodeInput(&$values);
abstract public function encodeInput(&$values);

/**
* Decode output.
Expand All @@ -101,7 +101,7 @@ public abstract function encodeInput(&$values);
*
* @return mixed
*/
public abstract function decodeOutput(&$values);
abstract public function decodeOutput(&$values);

/**
* @inheritDoc
Expand Down
27 changes: 18 additions & 9 deletions CRM/Utils/API/HTMLInputCoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,28 @@ public function getSkipFields() {
'honor_block_text',
'pay_later_text',
'pay_later_receipt',
'label', // This is needed for FROM Email Address configuration. dgg
'url', // This is needed for navigation items urls
// This is needed for FROM Email Address configuration. dgg
'label',
// This is needed for navigation items urls
'url',
'details',
'msg_text', // message templates’ text versions
'text_message', // (send an) email to contact’s and CiviMail’s text version
'data', // data i/p of persistent table
'sqlQuery', // CRM-6673
// message templates’ text versions
'msg_text',
// (send an) email to contact’s and CiviMail’s text version
'text_message',
// data i/p of persistent table
'data',
// CRM-6673
'sqlQuery',
'pcp_title',
'pcp_intro_text',
'new', // The 'new' text in word replacements
'replyto_email', // e.g. '"Full Name" <user@example.org>'
// The 'new' text in word replacements
'new',
// e.g. '"Full Name" <user@example.org>'
'replyto_email',
'operator',
'content', // CRM-20468
// CRM-20468
'content',
];
$custom = CRM_Core_DAO::executeQuery('SELECT id FROM civicrm_custom_field WHERE html_type = "RichTextEditor"');
while ($custom->fetch()) {
Expand Down
3 changes: 2 additions & 1 deletion CRM/Utils/API/MatchOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ public function match($entity, $createParams, $keys, $isMandatory) {
if ($isMandatory) {
throw new API_Exception("Failed to match existing record");
}
return $createParams; // OK, don't care
// OK, don't care
return $createParams;
}
elseif ($getResult['count'] == 1) {
$item = array_shift($getResult['values']);
Expand Down
24 changes: 12 additions & 12 deletions CRM/Utils/Address/BatchUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
*/
class CRM_Utils_Address_BatchUpdate {

var $start = NULL;
var $end = NULL;
var $geocoding = 1;
var $parse = 1;
var $throttle = 0;
public $start = NULL;
public $end = NULL;
public $geocoding = 1;
public $parse = 1;
public $throttle = 0;

var $returnMessages = [];
var $returnError = 0;
public $returnMessages = [];
public $returnError = 0;

/**
* Class constructor.
Expand Down Expand Up @@ -260,16 +260,16 @@ public function processContacts($processGeocode, $parseStreetAddress) {

$this->returnMessages[] = ts("Addresses Evaluated: %1", [
1 => $totalAddresses,
]) . "\n";
]) . "\n";
if ($processGeocode) {
$this->returnMessages[] = ts("Addresses Geocoded: %1", [
1 => $totalGeocoded,
]) . "\n";
1 => $totalGeocoded,
]) . "\n";
}
if ($parseStreetAddress) {
$this->returnMessages[] = ts("Street Addresses Parsed: %1", [
1 => $totalAddressParsed,
]) . "\n";
1 => $totalAddressParsed,
]) . "\n";
if ($unparseableContactAddress) {
$this->returnMessages[] = "<br />\n" . ts("Following is the list of contacts whose address is not parsed:") . "<br />\n";
foreach ($unparseableContactAddress as $contactLink) {
Expand Down
3 changes: 2 additions & 1 deletion CRM/Utils/Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,8 @@ public static function formatArrayKeys(&$array) {
(count($keys) == 1 &&
(current($keys) > 1 ||
is_string(current($keys)) ||
(current($keys) == 1 && $array[1] == 1) // handle (0 => 4), (1 => 1)
// handle (0 => 4), (1 => 1)
(current($keys) == 1 && $array[1] == 1)
)
)
) {
Expand Down
3 changes: 2 additions & 1 deletion CRM/Utils/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ public static function assertValidKey($key) {
* Ex: 'ArrayCache', 'Memcache', 'Redis'.
*/
public static function getCacheDriver() {
$className = 'ArrayCache'; // default to ArrayCache for now
// default to ArrayCache for now
$className = 'ArrayCache';

// Maintain backward compatibility for now.
// Setting CIVICRM_USE_MEMCACHE or CIVICRM_USE_ARRAYCACHE will
Expand Down
12 changes: 8 additions & 4 deletions CRM/Utils/Cache/APCcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
*/
class CRM_Utils_Cache_APCcache implements CRM_Utils_Cache_Interface {

use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation
// TODO Consider native implementation.
use CRM_Utils_Cache_NaiveMultipleTrait;
// TODO Native implementation
use CRM_Utils_Cache_NaiveHasTrait;

const DEFAULT_TIMEOUT = 3600;
const DEFAULT_PREFIX = '';
Expand Down Expand Up @@ -123,8 +125,10 @@ public function delete($key) {
public function flush() {
$allinfo = apc_cache_info('user');
$keys = $allinfo['cache_list'];
$prefix = $this->_prefix; // Our keys follows this pattern: ([A-Za-z0-9_]+)?CRM_[A-Za-z0-9_]+
$lp = strlen($prefix); // Get prefix length
// Our keys follows this pattern: ([A-Za-z0-9_]+)?CRM_[A-Za-z0-9_]+
$prefix = $this->_prefix;
// Get prefix length
$lp = strlen($prefix);

foreach ($keys as $key) {
$name = $key['info'];
Expand Down
4 changes: 3 additions & 1 deletion CRM/Utils/Cache/ArrayCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
class CRM_Utils_Cache_Arraycache implements CRM_Utils_Cache_Interface {

use CRM_Utils_Cache_NaiveMultipleTrait;
use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation
// TODO Native implementation
use CRM_Utils_Cache_NaiveHasTrait;

const DEFAULT_TIMEOUT = 3600;

/**
* The cache storage container, an in memory array by default
* @var array
*/
protected $_cache;

Expand Down
3 changes: 2 additions & 1 deletion CRM/Utils/Cache/ArrayDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
*/
class CRM_Utils_Cache_ArrayDecorator implements CRM_Utils_Cache_Interface {

use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
// TODO Consider native implementation.
use CRM_Utils_Cache_NaiveMultipleTrait;

/**
* @var int
Expand Down
3 changes: 2 additions & 1 deletion CRM/Utils/Cache/FastArrayDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
*/
class CRM_Utils_Cache_FastArrayDecorator implements CRM_Utils_Cache_Interface {

use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
// TODO Consider native implementation.
use CRM_Utils_Cache_NaiveMultipleTrait;

/**
* @var int
Expand Down
7 changes: 4 additions & 3 deletions CRM/Utils/Cache/Memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
*/
class CRM_Utils_Cache_Memcache implements CRM_Utils_Cache_Interface {

use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
// TODO Consider native implementation.
use CRM_Utils_Cache_NaiveMultipleTrait;

const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = 11211;
Expand Down Expand Up @@ -163,7 +164,6 @@ public function has($key) {
return ($result !== FALSE);
}


/**
* @param $key
*
Expand Down Expand Up @@ -194,7 +194,8 @@ protected function getTruePrefix() {
$value = $this->_cache->get($key);
if ($value === FALSE) {
$value = uniqid();
$this->_cache->set($key, $value, FALSE, 0); // Indefinite.
// Indefinite.
$this->_cache->set($key, $value, FALSE, 0);
}
$this->_truePrefix = [
'value' => $value,
Expand Down
9 changes: 6 additions & 3 deletions CRM/Utils/Cache/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
*/
class CRM_Utils_Cache_Memcached implements CRM_Utils_Cache_Interface {

use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
// TODO Consider native implementation.
use CRM_Utils_Cache_NaiveMultipleTrait;

const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = 11211;
Expand Down Expand Up @@ -227,7 +228,8 @@ public function cleanKey($key) {
$maxLen = self::MAX_KEY_LEN - strlen($truePrefix);
$key = preg_replace('/\s+|\W+/', '_', $key);
if (strlen($key) > $maxLen) {
$md5Key = md5($key); // this should be 32 characters in length
// this should be 32 characters in length
$md5Key = md5($key);
$subKeyLen = $maxLen - 1 - strlen($md5Key);
$key = substr($key, 0, $subKeyLen) . "_" . $md5Key;
}
Expand Down Expand Up @@ -256,7 +258,8 @@ protected function getTruePrefix() {
$value = $this->_cache->get($key);
if ($this->_cache->getResultCode() === Memcached::RES_NOTFOUND) {
$value = uniqid();
$this->_cache->add($key, $value, 0); // Indefinite.
// Indefinite.
$this->_cache->add($key, $value, 0);
}
$this->_truePrefix = [
'value' => $value,
Expand Down
1 change: 1 addition & 0 deletions CRM/Utils/Cache/NaiveMultipleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public function deleteMultiple($keys) {
}

/**
* @param $func
* @param $keys
* @throws \CRM_Utils_Cache_InvalidArgumentException
*/
Expand Down
6 changes: 4 additions & 2 deletions CRM/Utils/Cache/NoCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
*/
class CRM_Utils_Cache_NoCache implements CRM_Utils_Cache_Interface {

use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation
// TODO Consider native implementation.
use CRM_Utils_Cache_NaiveMultipleTrait;
// TODO Native implementation
use CRM_Utils_Cache_NaiveHasTrait;

/**
* We only need one instance of this object. So we use the singleton
Expand Down
9 changes: 6 additions & 3 deletions CRM/Utils/Cache/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
*/
class CRM_Utils_Cache_Redis implements CRM_Utils_Cache_Interface {

use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation
// TODO Consider native implementation.
use CRM_Utils_Cache_NaiveMultipleTrait;
// TODO Native implementation
use CRM_Utils_Cache_NaiveHasTrait;

const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = 6379;
Expand Down Expand Up @@ -76,7 +78,8 @@ class CRM_Utils_Cache_Redis implements CRM_Utils_Cache_Interface {
public static function connect($config) {
$host = isset($config['host']) ? $config['host'] : self::DEFAULT_HOST;
$port = isset($config['port']) ? $config['port'] : self::DEFAULT_PORT;
$pass = CRM_Utils_Constant::value('CIVICRM_DB_CACHE_PASSWORD'); // Ugh.
// Ugh.
$pass = CRM_Utils_Constant::value('CIVICRM_DB_CACHE_PASSWORD');
$id = implode(':', ['connect', $host, $port /* $pass is constant */]);
if (!isset(Civi::$statics[__CLASS__][$id])) {
// Ideally, we'd track the connection in the service-container, but the
Expand Down
10 changes: 7 additions & 3 deletions CRM/Utils/Cache/SerializeCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
class CRM_Utils_Cache_SerializeCache implements CRM_Utils_Cache_Interface {

use CRM_Utils_Cache_NaiveMultipleTrait;
use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation
// TODO Native implementation
use CRM_Utils_Cache_NaiveHasTrait;

/**
* The cache storage container, an array by default, stored in a file under templates
* @var array
*/
private $_cache;

Expand Down Expand Up @@ -101,7 +103,8 @@ public function set($key, $value, $ttl = NULL) {
throw new \RuntimeException("FIXME: " . __CLASS__ . "::set() should support non-NULL TTL");
}
if (file_exists($this->fileName($key))) {
return FALSE; // WTF, write-once cache?!
// WTF, write-once cache?!
return FALSE;
}
$this->_cache[$key] = $value;
$bytes = file_put_contents($this->fileName($key), "<?php //" . serialize($value));
Expand All @@ -127,7 +130,8 @@ public function delete($key) {
public function flush($key = NULL) {
$prefix = "CRM_";
if (!$handle = opendir(CIVICRM_TEMPLATE_COMPILEDIR)) {
return FALSE; // die? Error?
// die? Error?
return FALSE;
}
while (FALSE !== ($entry = readdir($handle))) {
if (substr($entry, 0, 4) == $prefix) {
Expand Down
5 changes: 3 additions & 2 deletions CRM/Utils/Cache/SqlGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class CRM_Utils_Cache_SqlGroup implements CRM_Utils_Cache_Interface {
const DEFAULT_TTL = 21600;

const TS_FMT = 'Y-m-d H:i:s';
use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
// TODO Consider native implementation.
use CRM_Utils_Cache_NaiveMultipleTrait;

/**
* The host name of the memcached server.
Expand All @@ -52,7 +53,7 @@ class CRM_Utils_Cache_SqlGroup implements CRM_Utils_Cache_Interface {
protected $group;

/**
* @var int $componentID The optional component ID (so componenets can share the same name space)
* @var int
*/
protected $componentID;

Expand Down
3 changes: 2 additions & 1 deletion CRM/Utils/Cache/Tiered.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
*/
class CRM_Utils_Cache_Tiered implements CRM_Utils_Cache_Interface {

use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
// TODO Consider native implementation.
use CRM_Utils_Cache_NaiveMultipleTrait;

/**
* @var array
Expand Down
4 changes: 2 additions & 2 deletions CRM/Utils/Check/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
+--------------------------------------------------------------------+
*/

use GuzzleHttp\Client;

/**
*
Expand Down Expand Up @@ -62,7 +61,8 @@ public function checkAll() {
/**
* Check if file exists on given URL.
*
* @param $url
* @param string $url
* @param int $timeout
* @return bool
* @throws \GuzzleHttp\Exception\GuzzleException
*/
Expand Down
Loading

0 comments on commit de0e753

Please sign in to comment.