From 200988eecf898d8d6cc2579193cb6784528676a5 Mon Sep 17 00:00:00 2001 From: Joshua Sherman Date: Wed, 15 Jan 2014 14:09:54 -0500 Subject: [PATCH] Swapped all array() for the shorthand [] Also finished up coverage on the Cache class. --- classes/API/Google/Profanity.php | 3 +- classes/API/Gravatar.php | 5 ++- classes/Browser.php | 2 +- classes/Cache.php | 12 +++--- classes/Config.php | 6 +-- classes/Convert.php | 16 +++---- classes/Dynamic.php | 6 ++- classes/File.php | 2 +- classes/Form.php | 30 ++++++------- classes/Model.php | 72 +++++++++++++++++--------------- classes/Module.php | 6 +-- classes/Number.php | 2 +- classes/Security.php | 26 ++++++++---- classes/Session.php | 9 ++-- classes/String.php | 4 +- classes/Validate.php | 2 +- tests/classes/CacheTest.php | 9 ++-- 17 files changed, 115 insertions(+), 97 deletions(-) diff --git a/classes/API/Google/Profanity.php b/classes/API/Google/Profanity.php index f2b73e3..fc590a2 100644 --- a/classes/API/Google/Profanity.php +++ b/classes/API/Google/Profanity.php @@ -34,7 +34,8 @@ class API_Google_Profanity { $response = json_decode(file_get_contents($endpoint . $word), true); - if ($response == null || !isset($response['response']) || !in_array($response['response'], array('true', 'false'))) + if ($response == null || !isset($response['response']) + || !in_array($response['response'], ['true', 'false'])) { throw new Exception('Invalid response from API.'); } diff --git a/classes/API/Gravatar.php b/classes/API/Gravatar.php index edb7ba9..e9bbb6f 100644 --- a/classes/API/Gravatar.php +++ b/classes/API/Gravatar.php @@ -63,11 +63,12 @@ class API_Gravatar { throw new Exception('Invalid size parameter, expecting an integer between 1 and 2048.'); } - elseif (!in_array($default, array('gravatar', '404', 'mm', 'identicon', 'monsterid', 'wavatar', 'retro', 'blank')) && !filter_var($default, FILTER_VALIDATE_URL)) + elseif (!in_array($default, ['gravatar', '404', 'mm', 'identicon', 'monsterid', 'wavatar', 'retro', 'blank']) + && !filter_var($default, FILTER_VALIDATE_URL)) { throw new Exception('Invalid default parameter, expecting gravatar, 404, mm, identicon, monsterid, wavatar, retro, blank or a valid URL.'); } - elseif (!in_array($rating, array('g', 'pg', 'r', 'x'))) + elseif (!in_array($rating, ['g', 'pg', 'r', 'x'])) { throw new Exception('Invalid rating parameter, expecting g, pg, r or x.'); } diff --git a/classes/Browser.php b/classes/Browser.php index 52e41ba..6c47a45 100644 --- a/classes/Browser.php +++ b/classes/Browser.php @@ -31,7 +31,7 @@ class Browser extends Object * @access private * @var array */ - private $attributes = array(); + private $attributes = []; /** * Get instance of the object diff --git a/classes/Cache.php b/classes/Cache.php index af7a11a..2d92ad1 100644 --- a/classes/Cache.php +++ b/classes/Cache.php @@ -75,13 +75,11 @@ class Cache extends Object // @todo Shouldn't need the isset() but Travis is failing some tests if (isset($this->config->pickles['cache']) && $this->config->pickles['cache']) { - if (!is_array($this->config->pickles['cache'])) + $datasources = $this->config->pickles['cache']; + + if (!is_array($datasources)) { - $datasources = array($this->config->pickles['cache']); - } - else - { - $datasources = $this->config->pickles['cache']; + $datasources = [$datasources]; } $this->connection = new Memcache(); @@ -195,7 +193,7 @@ class Cache extends Object { if (!is_array($keys)) { - $keys = array($keys); + $keys = [$keys]; } // Memcache() doesn't let you pass an array to delete all records the same way you can with get() diff --git a/classes/Config.php b/classes/Config.php index 1b2b0ff..ba44de6 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -34,7 +34,7 @@ class Config extends Object * * @var array */ - public $data = array(); + public $data = []; /** * Constructor @@ -83,7 +83,7 @@ class Config extends Object { if (!is_array($hosts)) { - $hosts = array($hosts); + $hosts = [$hosts]; } // Tries to determine the environment name @@ -161,7 +161,7 @@ class Config extends Object } // Defaults expected PICKLES options to false - foreach (array('cache', 'logging', 'minify') as $variable) + foreach (['cache', 'logging', 'minify'] as $variable) { if (!isset($this->data['pickles'][$variable])) { diff --git a/classes/Convert.php b/classes/Convert.php index 3838420..65f035c 100644 --- a/classes/Convert.php +++ b/classes/Convert.php @@ -33,13 +33,13 @@ class Convert * that an array's format isn't quite the same as well-formed XML. * * Input Array = - * array('children' => array( - * 'child' => array( - * array('name' => 'Wendy Darling'), - * array('name' => 'John Darling'), - * array('name' => 'Michael Darling') - * ) - * )) + * ['children' => [ + * 'child' => [ + * ['name' => 'Wendy Darling'], + * ['name' => 'John Darling'], + * ['name' => 'Michael Darling'], + * ], + * ]] * * Output XML = * @@ -68,7 +68,7 @@ class Convert if (is_array($value2)) { // Nest the value if the node is an integer - $new_value = (is_int($node2) ? $value2 : array($node2 => $value2)); + $new_value = (is_int($node2) ? $value2 : [$node2 => $value2]); $xml .= ($format ? str_repeat("\t", $level) : ''); $xml .= '<' . $node . '>' . ($format ? "\n" : ''); diff --git a/classes/Dynamic.php b/classes/Dynamic.php index c23058f..ed4f932 100644 --- a/classes/Dynamic.php +++ b/classes/Dynamic.php @@ -179,7 +179,11 @@ class Dynamic extends Object { // Minifies CSS with a few basic character replacements. $stylesheet = file_get_contents($original_filename); - $stylesheet = str_replace(array("\t", "\n", ', ', ' {', ': ', ';}', '{ ', '; '), array('', '', ',', '{', ':', '}', '{', ';'), $stylesheet); + $stylesheet = str_replace( + ["\t", "\n", ', ', ' {', ': ', ';}', '{ ', '; '], + ['', '', ',', '{', ':', '}', '{', ';'], + $stylesheet + ); $stylesheet = preg_replace('/\/\*.+?\*\//', '', $stylesheet); file_put_contents($minified_filename, $stylesheet); diff --git a/classes/File.php b/classes/File.php index 47644d9..8f61795 100644 --- a/classes/File.php +++ b/classes/File.php @@ -49,7 +49,7 @@ class File // Loop through said files, check for directories, and unlink files foreach ($files as $file) { - if (!in_array($file, array('.', '..'))) + if (!in_array($file, ['.', '..'])) { if (is_dir($directory . $file)) { diff --git a/classes/Form.php b/classes/Form.php index ca5d3d9..dab6f52 100644 --- a/classes/Form.php +++ b/classes/Form.php @@ -64,7 +64,7 @@ class Form extends Object $additional = ' ' . $additional; } - if (in_array($type, array('checkbox', 'radio')) && $checked == true) + if (in_array($type, ['checkbox', 'radio']) && $checked == true) { $additional .= ' checked="checked"'; } @@ -414,7 +414,7 @@ class Form extends Object */ public function stateSelect($name = 'state', $selected = null, $classes = null, $additional = null) { - $options = array( + $options = [ null => '-- Select State --', 'AK' => 'Alaska', 'AL' => 'Alabama', @@ -480,7 +480,7 @@ class Form extends Object 'AE' => 'Armed Forces Europe', 'AE' => 'Armed Forces Middle East', 'AP' => 'Armed Forces Pacific' - ); + ]; return $this->select($name, $options, $selected, $classes, $additional); } @@ -517,7 +517,7 @@ class Form extends Object list($selected_year, $selected_month, $selected_day) = explode('-', $selected); } - $month_options = array( + $month_options = [ null => 'Month', '01' => 'January', '02' => 'February', @@ -531,10 +531,10 @@ class Form extends Object '10' => 'October', '11' => 'November', '12' => 'December', - ); + ]; - $day_options = array(null => 'Day'); - $year_options = array(null => 'Year'); + $day_options = [null => 'Day']; + $year_options = [null => 'Year']; // Generates the list of days for ($i = 1; $i <= 31; ++$i) @@ -553,7 +553,7 @@ class Form extends Object } // Loops through and generates the selects - foreach (array('month', 'day', 'year') as $part) + foreach (['month', 'day', 'year'] as $part) { $options = $part . '_options'; $selected = 'selected_' . $part; @@ -599,7 +599,7 @@ class Form extends Object */ public function polarSelect($name = 'decision', $selected = 0, $classes = null, $additional = null) { - $options = array(1 => 'Yes', 0 => 'No'); + $options = [1 => 'Yes', 0 => 'No']; return $this->select($name, $options, $selected, $classes, $additional); } @@ -621,27 +621,27 @@ class Form extends Object { if ($value == null) { - $value = array( + $value = [ 'area_code' => '', 'prefix' => '', 'line_number' => '' - ); + ]; } else { $value = str_replace('-', '', $value); - $value = array( + $value = [ 'area_code' => substr($value, 0, 3), 'prefix' => substr($value, 3, 3), 'line_number' => substr($value, 6) - ); + ]; } - $parts = array( + $parts = [ 'area_code' => 3, 'prefix' => 3, 'line_number' => 4 - ); + ]; if ($additional) { diff --git a/classes/Model.php b/classes/Model.php index cd74518..24bf5e3 100644 --- a/classes/Model.php +++ b/classes/Model.php @@ -58,7 +58,7 @@ class Model extends Object * @access private * @var array */ - private $sql = array(); + private $sql = []; /** * Input Parameters Array @@ -66,7 +66,7 @@ class Model extends Object * @access private * @var array */ - private $input_parameters = array(); + private $input_parameters = []; /** * Insert Priority @@ -258,7 +258,7 @@ class Model extends Object * @access private * @var array */ - private $snapshot = array(); + private $snapshot = []; /** * MySQL? @@ -322,7 +322,7 @@ class Model extends Object $this->model = get_class($this); // Default column mapping - $columns = array( + $columns = [ 'id' => 'id', 'created_at' => 'created_at', 'created_id' => 'created_id', @@ -331,7 +331,7 @@ class Model extends Object 'deleted_at' => 'deleted_at', 'deleted_id' => 'deleted_id', 'is_deleted' => 'is_deleted', - ); + ]; // Grabs the config columns if no columns are set if ($this->columns === null && isset($this->db->columns)) @@ -364,7 +364,7 @@ class Model extends Object // Takes a snapshot of the [non-object] object properties foreach ($this as $variable => $value) { - if (!in_array($variable, array('db', 'cache', 'config', 'snapshot'))) + if (!in_array($variable, ['db', 'cache', 'config', 'snapshot'])) { $this->snapshot[$variable] = $value; } @@ -411,12 +411,12 @@ class Model extends Object && count($type_or_parameters) == 1 && count($type_or_parameters['conditions']) == 1) { - $cache_keys = array(); - $sorted_records = array(); + $cache_keys = []; + $sorted_records = []; if (!is_array($type_or_parameters['conditions'][$this->columns['id']])) { - $type_or_parameters['conditions'][$this->columns['id']] = array($type_or_parameters['conditions'][$this->columns['id']]); + $type_or_parameters['conditions'][$this->columns['id']] = [$type_or_parameters['conditions'][$this->columns['id']]]; } foreach ($type_or_parameters['conditions'][$this->columns['id']] as $id) @@ -426,7 +426,7 @@ class Model extends Object } $cached = $this->cache->get($cache_keys); - $partial_cache = array(); + $partial_cache = []; if ($cached !== false) { @@ -469,8 +469,8 @@ class Model extends Object && count($parameters_or_key) == 1 && count($parameters_or_key['conditions']) == 1) { - $cache_keys = array(); - $sorted_records = array(); + $cache_keys = []; + $sorted_records = []; foreach ($parameters_or_key['conditions'][$this->columns['id']] as $id) { @@ -479,7 +479,7 @@ class Model extends Object } $cached = $this->cache->get($cache_keys); - $partial_cache = array(); + $partial_cache = []; if ($cached !== false) { @@ -516,7 +516,7 @@ class Model extends Object elseif (ctype_digit((string)$type_or_parameters)) { $cache_key = strtoupper($this->model) . '-' . $type_or_parameters; - $parameters_or_key = array($this->columns['id'] => $type_or_parameters); + $parameters_or_key = [$this->columns['id'] => $type_or_parameters]; if ($this->columns['is_deleted']) { @@ -528,7 +528,7 @@ class Model extends Object elseif (ctype_digit((string)$parameters_or_key)) { $cache_key = strtoupper($this->model) . '-' . $parameters_or_key; - $parameters_or_key = array($this->columns['id'] => $parameters_or_key); + $parameters_or_key = [$this->columns['id'] => $parameters_or_key]; if ($this->columns['is_deleted']) { @@ -539,7 +539,7 @@ class Model extends Object } elseif ($this->columns['is_deleted']) { - $this->loadParameters(array($this->columns['is_deleted'] => '0')); + $this->loadParameters([$this->columns['is_deleted'] => '0']); } if (is_string($parameters_or_key)) @@ -553,10 +553,10 @@ class Model extends Object } // Starts with a basic SELECT ... FROM - $this->sql = array( + $this->sql = [ 'SELECT ' . (is_array($this->fields) ? implode(', ', $this->fields) : $this->fields), 'FROM ' . $this->table, - ); + ]; switch ($type_or_parameters) { @@ -645,12 +645,12 @@ class Model extends Object } } - $index_records = in_array($type_or_parameters, array('list', 'indexed')); + $index_records = in_array($type_or_parameters, ['list', 'indexed']); // Flattens the data into a list if ($index_records == true) { - $list = array(); + $list = []; foreach ($this->records as $record) { @@ -713,7 +713,7 @@ class Model extends Object { foreach ($this->joins as $join => $tables) { - $join_pieces = array((stripos('JOIN ', $join) === false ? 'JOIN' : strtoupper($join))); + $join_pieces = [(stripos('JOIN ', $join) === false ? 'JOIN' : strtoupper($join))]; if (is_array($tables)) { @@ -793,7 +793,7 @@ class Model extends Object if ($use_id) { - $this->conditions = array($this->columns['id'] => $this->conditions); + $this->conditions = [$this->columns['id'] => $this->conditions]; } $this->sql[] = 'WHERE ' . (is_array($this->conditions) ? $this->generateConditions($this->conditions) : $this->conditions); @@ -1237,7 +1237,7 @@ class Model extends Object if ($this->commit_type == 'queue') { $update = false; - $cache_keys = array(); + $cache_keys = []; /** * @todo I outta loop through twice to determine if it's an INSERT @@ -1256,10 +1256,10 @@ class Model extends Object if (!isset($sql)) { $sql = ''; - $input_parameters = array(); + $input_parameters = []; } - $update_fields = array(); + $update_fields = []; foreach ($record as $field => $value) { @@ -1326,7 +1326,7 @@ class Model extends Object } $values = '(' . implode(', ', array_fill(0, $field_count, '?')) . ')'; - $input_parameters = array(); + $input_parameters = []; // INSERT INTO ... $sql = 'INSERT INTO ' . $this->table . ' (' . implode(', ', $insert_fields) . ') VALUES ' . $values; @@ -1409,7 +1409,8 @@ class Model extends Object // PRIORITY syntax takes priority over DELAYED if ($this->mysql) { - if ($this->priority !== false && in_array(strtoupper($this->priority), array('LOW', 'HIGH'))) + if ($this->priority !== false + && in_array(strtoupper($this->priority), ['LOW', 'HIGH'])) { $sql .= ' ' . strtoupper($this->priority) . '_PRIORITY'; } @@ -1433,12 +1434,15 @@ class Model extends Object $input_parameters = null; // Limits the columns being updated - $record = ($update ? array_diff_assoc($this->record, isset($this->original[$this->index]) ? $this->original[$this->index] : array()) : $this->record); + $record = ($update ? array_diff_assoc( + $this->record, + isset($this->original[$this->index]) ? $this->original[$this->index] : [] + ) : $this->record); // Makes sure there's something to INSERT or UPDATE if (count($record) > 0) { - $insert_fields = array(); + $insert_fields = []; // Loops through all the columns and assembles the query foreach ($record as $column => $value) @@ -1454,7 +1458,7 @@ class Model extends Object $sql .= $column . ' = '; - if (in_array($value, array('++', '--'))) + if (in_array($value, ['++', '--'])) { $sql .= $column . ' ' . substr($value, 0, 1) . ' ?'; $value = 1; @@ -1566,7 +1570,7 @@ class Model extends Object if ($this->columns['is_deleted']) { $sql = 'UPDATE ' . $this->table . ' SET ' . $this->columns['is_deleted'] . ' = ?'; - $input_parameters = array('1'); + $input_parameters = ['1']; if ($this->columns['deleted_at']) { @@ -1631,7 +1635,7 @@ class Model extends Object if ($all_integers) { - $parameters = array('conditions' => array($this->columns['id'] => $parameters)); + $parameters = ['conditions' => [$this->columns['id'] => $parameters]]; } } @@ -1657,7 +1661,7 @@ class Model extends Object $key = trim(strtolower($key)); // Assigns valid keys to the appropriate class property - if (in_array($key, array('fields', 'table', 'joins', 'hints', 'conditions', 'group', 'having', 'order', 'limit', 'offset'))) + if (in_array($key, ['fields', 'table', 'joins', 'hints', 'conditions', 'group', 'having', 'order', 'limit', 'offset'])) { $this->$key = $value; $conditions = false; @@ -1706,7 +1710,7 @@ class Model extends Object */ public function fieldValues($field) { - $values = array(); + $values = []; foreach ($this->records as $record) { diff --git a/classes/Module.php b/classes/Module.php index cd41ba0..98614dc 100644 --- a/classes/Module.php +++ b/classes/Module.php @@ -146,7 +146,7 @@ class Module extends Object * @todo Move to public scope and rename __return so it's kinda obscured * @todo Will need to update leaderbin and sndcrd to use new variable */ - protected $return = array(); + protected $return = []; /** * Output @@ -264,7 +264,7 @@ class Module extends Object */ public function __validate() { - $errors = array(); + $errors = []; if ($this->validate !== false) { @@ -307,7 +307,7 @@ class Module extends Object } } - return $errors == array() ? false : $errors; + return $errors == [] ? false : $errors; } } diff --git a/classes/Number.php b/classes/Number.php index d4f5659..fd27fc9 100644 --- a/classes/Number.php +++ b/classes/Number.php @@ -37,7 +37,7 @@ class Number */ public static function ordinalIndicator($number, $superscript = false) { - if (!in_array(($number % 100), array(11, 12, 13))) + if (!in_array(($number % 100), [11, 12, 13])) { switch ($number % 10) { diff --git a/classes/Security.php b/classes/Security.php index 41777d6..c239c94 100644 --- a/classes/Security.php +++ b/classes/Security.php @@ -35,7 +35,7 @@ class Security * @access private * @var array */ - private static $cache = array(); + private static $cache = []; /** * Generate Hash @@ -60,14 +60,14 @@ class Security } else { - $salts = array('P1ck73', 'Ju1C3'); + $salts = ['P1ck73', 'Ju1C3']; } } // Forces the variable to be an array if (!is_array($salts)) { - $salts = array($salts); + $salts = [$salts]; } // Loops through the salts, applies them and calculates the hash @@ -163,12 +163,12 @@ class Security { $token = sha1(microtime()); - $_SESSION['__pickles']['security'] = array( + $_SESSION['__pickles']['security'] = [ 'token' => $token, 'user_id' => (int)$user_id, 'level' => $level, 'role' => $role, - ); + ]; setcookie('pickles_security_token', $token); @@ -240,14 +240,19 @@ class Security if ($config->security === false) { - $config = array(); + $config = []; } else { $config = $config->security; } - $defaults = array('login' => 'login', 'model' => 'User', 'column' => 'level'); + $defaults = [ + 'login' => 'login', + 'model' => 'User', + 'column' => 'level', + ]; + foreach ($defaults as $variable => $value) { if (!isset($config[$variable])) @@ -258,7 +263,12 @@ class Security // Uses the model to pull the user's access level $class = $config['model']; - $model = new $class(array('fields' => $config['column'], 'conditions' => array('id' => (int)$_SESSION['__pickles']['security']['user_id']))); + $model = new $class([ + 'fields' => $config['column'], + 'conditions' => [ + 'id' => (int)$_SESSION['__pickles']['security']['user_id'], + ], + ]); if ($model->count() == 0) { diff --git a/classes/Session.php b/classes/Session.php index 8f9fec4..2be2cdb 100644 --- a/classes/Session.php +++ b/classes/Session.php @@ -19,11 +19,10 @@ * Session Class * * Provides session handling via database instead of the file based session - * handling built into PHP. Using this class requires an array to be - * defined in place of the boolean true/false (on/off). If simply array(), - * the datasource will default to the value in - * $config['pickles']['datasource'] and if the table will default to - * "sessions". The format is as follows: + * handling built into PHP. Using this class requires an array to be defined + * in place of the boolean true/false (on/off). If simply an empty array, the + * datasource will default to the value in $config['pickles']['datasource'] and + * if the table will default to "sessions". The format is as follows: */ class Session extends Object { diff --git a/classes/String.php b/classes/String.php index 185008d..6b2f762 100644 --- a/classes/String.php +++ b/classes/String.php @@ -38,7 +38,7 @@ class String public static function formatPhoneNumber($number, $replacement = '$1-$2-$3') { // Strips characters we don't need - $number = str_replace(array('(', ')', ' ', '-', '.', '_'), '', $number); + $number = str_replace(['(', ')', ' ', '-', '.', '_'], '', $number); // Formats the number return preg_replace('/^(\d{3})(\d{3})(.+)$/', $replacement, $number); @@ -162,7 +162,7 @@ class String */ public static function random($length = 8, $alpha = true, $numeric = true, $similar = true) { - $characters = array(); + $characters = []; $string = ''; // Adds alpha characters to the list diff --git a/classes/Validate.php b/classes/Validate.php index 062f920..2bfeaed 100644 --- a/classes/Validate.php +++ b/classes/Validate.php @@ -35,7 +35,7 @@ class Validate */ public static function isValid($value, $rules) { - $errors = array(); + $errors = []; if (is_array($rules)) { diff --git a/tests/classes/CacheTest.php b/tests/classes/CacheTest.php index b708d49..786b13c 100644 --- a/tests/classes/CacheTest.php +++ b/tests/classes/CacheTest.php @@ -10,9 +10,10 @@ class CacheTest extends PHPUnit_Framework_TestCase $this->config = Config::getInstance(); $this->config->data['pickles']['cache'] = 'mc'; $this->config->data['datasources']['mc'] = [ - 'type' => 'memcache', - 'hostname' => 'localhost', - 'port' => 11211, + 'type' => 'memcache', + 'hostname' => 'localhost', + 'port' => 11211, + 'namespace' => 'ns', ]; $this->cache = Cache::getInstance(); @@ -46,7 +47,7 @@ class CacheTest extends PHPUnit_Framework_TestCase foreach ($keys as $key => $key_name) { $value = $values[$key]; - $expected[strtoupper($key_name)] = $value; + $expected['NS-' . strtoupper($key_name)] = $value; $this->cache->set($key_name, $value); }