Don't trigger E_NOTICE when looking for commands in the notice input

explode() only returns one item if there was no space, leading to an E_NOTICE about an undefined array index in the list($a,$b) pattern.
This commit is contained in:
Brion Vibber 2009-09-20 21:39:57 -07:00 committed by Brenda Wallace
parent 8aad3154a7
commit 0125f29324

View File

@ -28,7 +28,7 @@ class CommandInterpreter
# XXX: localise # XXX: localise
$text = preg_replace('/\s+/', ' ', trim($text)); $text = preg_replace('/\s+/', ' ', trim($text));
list($cmd, $arg) = explode(' ', $text, 2); list($cmd, $arg) = $this->split_arg($text);
# We try to support all the same commands as Twitter, see # We try to support all the same commands as Twitter, see
# http://getsatisfaction.com/twitter/topics/what_are_the_twitter_commands # http://getsatisfaction.com/twitter/topics/what_are_the_twitter_commands
@ -43,7 +43,7 @@ class CommandInterpreter
return new HelpCommand($user); return new HelpCommand($user);
case 'on': case 'on':
if ($arg) { if ($arg) {
list($other, $extra) = explode(' ', $arg, 2); list($other, $extra) = $this->split_arg($arg);
if ($extra) { if ($extra) {
return null; return null;
} else { } else {
@ -54,7 +54,7 @@ class CommandInterpreter
} }
case 'off': case 'off':
if ($arg) { if ($arg) {
list($other, $extra) = explode(' ', $arg, 2); list($other, $extra) = $this->split_arg($arg);
if ($extra) { if ($extra) {
return null; return null;
} else { } else {
@ -74,7 +74,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
return null; return null;
} }
list($other, $extra) = explode(' ', $arg, 2); list($other, $extra) = $this->split_arg($arg);
if ($extra) { if ($extra) {
return null; return null;
} else { } else {
@ -84,7 +84,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
return null; return null;
} }
list($other, $extra) = explode(' ', $arg, 2); list($other, $extra) = $this->split_arg($arg);
if ($extra) { if ($extra) {
return null; return null;
} else { } else {
@ -95,7 +95,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
return null; return null;
} }
list($other, $extra) = explode(' ', $arg, 2); list($other, $extra) = $this->split_arg($arg);
if ($extra) { if ($extra) {
return null; return null;
} else { } else {
@ -106,7 +106,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
return null; return null;
} }
list($other, $extra) = explode(' ', $arg, 2); list($other, $extra) = $this->split_arg($arg);
if ($extra) { if ($extra) {
return null; return null;
} else { } else {
@ -117,7 +117,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
return null; return null;
} }
list($other, $extra) = explode(' ', $arg, 2); list($other, $extra) = $this->split_arg($arg);
if ($extra) { if ($extra) {
return null; return null;
} else { } else {
@ -128,7 +128,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
return null; return null;
} }
list($other, $extra) = explode(' ', $arg, 2); list($other, $extra) = $this->split_arg($arg);
if (!$extra) { if (!$extra) {
return null; return null;
} else { } else {
@ -138,7 +138,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
return null; return null;
} }
list($other, $extra) = explode(' ', $arg, 2); list($other, $extra) = $this->split_arg($arg);
if ($extra) { if ($extra) {
return null; return null;
} else { } else {
@ -148,7 +148,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
return null; return null;
} }
list($other, $extra) = explode(' ', $arg, 2); list($other, $extra) = $this->split_arg($arg);
if ($extra) { if ($extra) {
return null; return null;
} else { } else {
@ -158,7 +158,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
return null; return null;
} }
list($other, $extra) = explode(' ', $arg, 2); list($other, $extra) = $this->split_arg($arg);
if ($extra) { if ($extra) {
return null; return null;
} else { } else {
@ -173,7 +173,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
return null; return null;
} }
list($other, $extra) = explode(' ', $arg, 2); list($other, $extra) = $this->split_arg($arg);
if ($extra) { if ($extra) {
return null; return null;
} else { } else {
@ -183,7 +183,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
return null; return null;
} }
list($word, $extra) = explode(' ', $arg, 2); list($word, $extra) = $this->split_arg($arg);
if ($extra) { if ($extra) {
return null; return null;
} else if ($word == 'off') { } else if ($word == 'off') {
@ -195,7 +195,7 @@ class CommandInterpreter
if (!$arg) { if (!$arg) {
return null; return null;
} }
list($word, $extra) = explode(' ', $arg, 2); list($word, $extra) = $this->split_arg($arg);
if ($extra) { if ($extra) {
return null; return null;
} else if ($word == 'all') { } else if ($word == 'all') {
@ -213,5 +213,17 @@ class CommandInterpreter
return false; return false;
} }
} }
/**
* Split arguments without triggering a PHP notice warning
*/
function split_arg($text)
{
$pieces = explode(' ', $text, 2);
if (count($pieces) == 1) {
$pieces[] = null;
}
return $pieces;
}
} }