Skip to content

Commit 506b769

Browse files
author
Iskandar Najmuddin
committed
Make parseDsn() method static, remove unused member vars
1 parent 62ed620 commit 506b769

File tree

3 files changed

+33
-46
lines changed

3 files changed

+33
-46
lines changed

lib/Resque.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ public static function redis()
5454
return self::$redis;
5555
}
5656

57-
$server = self::$redisServer;
58-
if (empty($server)) {
59-
$server = 'localhost:6379';
60-
}
61-
62-
self::$redis = new Resque_Redis($server, self::$redisDatabase);
57+
self::$redis = new Resque_Redis(self::$redisServer, self::$redisDatabase);
6358
return self::$redis;
6459
}
6560

lib/Resque/Redis.php

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ class Resque_Redis
2929
*/
3030
const DEFAULT_DATABASE = 0;
3131

32-
private $server;
33-
private $database;
34-
3532
/**
3633
* @var array List of all commands in Redis that supply a key as their
3734
* first argument. Used to prefix keys with the Resque namespace.
@@ -114,16 +111,12 @@ public static function prefix($namespace)
114111
*/
115112
public function __construct($server, $database = null)
116113
{
117-
$this->server = $server;
118-
$this->database = $database;
119-
120-
if (is_array($this->server)) {
114+
if (is_array($server)) {
121115
$this->driver = new Credis_Cluster($server);
122-
123116
}
124117
else {
125118

126-
list($host, $port, $dsnDatabase, $user, $password, $options) = $this->parseDsn($server);
119+
list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server);
127120
// $user is not used, only $password
128121

129122
// Look for known Credis_Client options
@@ -139,11 +132,10 @@ public function __construct($server, $database = null)
139132
// value passed into the constructor.
140133
if ($dsnDatabase !== false) {
141134
$database = $dsnDatabase;
142-
$this->database = $database;
143135
}
144136
}
145137

146-
if ($this->database !== null) {
138+
if ($database !== null) {
147139
$this->driver->select($database);
148140
}
149141
}
@@ -158,9 +150,10 @@ public function __construct($server, $database = null)
158150
* Note: the 'user' part of the DSN is not used.
159151
*
160152
* @param string $dsn A DSN string
161-
* @return array [host, port, db, user, pass, options]
153+
* @return array An array of DSN compotnents, with 'false' values for any unknown components. e.g.
154+
* [host, port, db, user, pass, options]
162155
*/
163-
public function parseDsn($dsn)
156+
public static function parseDsn($dsn)
164157
{
165158
if ($dsn == '') {
166159
// Use a sensible default for an empty DNS string
@@ -219,37 +212,38 @@ public function parseDsn($dsn)
219212
* @param array $args Array of supplied arguments to the method.
220213
* @return mixed Return value from Resident::call() based on the command.
221214
*/
222-
public function __call($name, $args) {
223-
if(in_array($name, $this->keyCommands)) {
224-
if(is_array($args[0])) {
225-
foreach($args[0] AS $i => $v) {
226-
$args[0][$i] = self::$defaultNamespace . $v;
227-
}
228-
} else {
229-
$args[0] = self::$defaultNamespace . $args[0];
230-
}
215+
public function __call($name, $args)
216+
{
217+
if (in_array($name, $this->keyCommands)) {
218+
if (is_array($args[0])) {
219+
foreach ($args[0] AS $i => $v) {
220+
$args[0][$i] = self::$defaultNamespace . $v;
221+
}
222+
}
223+
else {
224+
$args[0] = self::$defaultNamespace . $args[0];
225+
}
231226
}
232227
try {
233228
return $this->driver->__call($name, $args);
234229
}
235-
catch(CredisException $e) {
230+
catch (CredisException $e) {
236231
return false;
237232
}
238233
}
239234

240-
public static function getPrefix()
241-
{
242-
return self::$defaultNamespace;
243-
}
235+
public static function getPrefix()
236+
{
237+
return self::$defaultNamespace;
238+
}
244239

245-
public static function removePrefix($string)
246-
{
247-
$prefix=self::getPrefix();
240+
public static function removePrefix($string)
241+
{
242+
$prefix=self::getPrefix();
248243

249-
if (substr($string, 0, strlen($prefix)) == $prefix) {
250-
$string = substr($string, strlen($prefix), strlen($string) );
251-
}
252-
return $string;
253-
}
244+
if (substr($string, 0, strlen($prefix)) == $prefix) {
245+
$string = substr($string, strlen($prefix), strlen($string) );
246+
}
247+
return $string;
248+
}
254249
}
255-
?>

test/Resque/Tests/DsnTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ public function bogusDsnStringProvider()
159159
*/
160160
public function testParsingValidDsnString($dsn, $expected)
161161
{
162-
$resqueRedis = new Resque_Redis('localhost');
163-
$result = $resqueRedis->parseDsn($dsn);
162+
$result = Resque_Redis::parseDsn($dsn);
164163
$this->assertEquals($expected, $result);
165164
}
166165

@@ -170,9 +169,8 @@ public function testParsingValidDsnString($dsn, $expected)
170169
*/
171170
public function testParsingBogusDsnStringThrowsException($dsn)
172171
{
173-
$resqueRedis = new Resque_Redis('localhost');
174172
// The next line should throw an InvalidArgumentException
175-
$result = $resqueRedis->parseDsn($dsn);
173+
$result = Resque_Redis::parseDsn($dsn);
176174
}
177175

178176
}

0 commit comments

Comments
 (0)