HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Suggestions for improving collection of related PHP functions?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
phprelatedcollectionforsuggestionsfunctionsimproving

Problem

I am working on a Web chat application. In the app, users can run commands (such as /nick, /msg, etc) by starting with a / and then typing the command followed by parameters. For example, if someone wanted to change their username to "foobar", they would enter this in:

/whois foobar


However, I cannot think of a good way to organize the code for this. This is what I have so far:

In User.php

function User_command(array $args)
{
   $command = $args['command']; 
   list($command_name, $command_parameters) = explode(' ', $command, 2); 
  $command_name = str_replace('/', '', strtolower($command_name)); 

$result = Command::process($command_name, $command_parameters); 

if (!$result)
    return responseFailure(); 

addQueueEntry($result['json'], $result['user_id'], $result['room_id'], $result['added_by']); 
return responseSuccess(); 
}


In Command.php

```
class Command
{
public function process($command, $parameters)
{
$method = 'MinteCommand_' . $command;

if (function_exists($method))
return $method($parameters);

return null;
}
}

/ Note: When implementing commands, you must specify all fields for $response /
/ If you don't, then they might not work correctly or as intended. /

function MinteCommand_notice($message)
{
$response = array();
$response['json'] = array('addChatNotice' => $message);
$response['user_id'] = null;
$response['room_id'] = MINTE;
$response['added_by'] = SYSTEM;

return $response;
}

function MinteCommand_announce($message)
{
$response = array();
$response['json'] = array('addChatAnnouncement' => $message);
$response['user_id'] = null;
$response['room_id'] = MINTE;
$response['added_by'] = SYSTEM;

return $response;
}

function MinteCommand_announcement($message)
{
// Command "announcement" is an alternative spelling for command "announce"
return MinteCommand_announce($message);
}

function MinteCommand_clear($message)
{
$response = array();
$response['json'] = array('clearChat

Solution

Just an idea, but maybe this will help you from a manageability point of view:

public function process($command, $parameters) {
      $method = 'MinteCommand_' . $command; 
      $methodFile = dirname(__FILE__) . 'commands/' . $method . ".php";
      if (file_exists($methodFile)) {
            require_once($methodFile);
            if (function_exists($method)) { 
                 return $method($parameters);  
            } else { 
                 throw "invalid function";
            }
      } else {
            throw "invalid function file";
      }
 }


The only benefit to this is that you simply create a new file with the new command (function) inside of that file and it'll automatically search for it if the command is issued. One caveat will be the limit on the number of commands you can have is a limit on the number of files you can have in a single directory on the filesystem...

Note The code may not be perfect from a syntax point of view. It's just to give you the general idea / concept.

Code Snippets

public function process($command, $parameters) {
      $method = 'MinteCommand_' . $command; 
      $methodFile = dirname(__FILE__) . 'commands/' . $method . ".php";
      if (file_exists($methodFile)) {
            require_once($methodFile);
            if (function_exists($method)) { 
                 return $method($parameters);  
            } else { 
                 throw "invalid function";
            }
      } else {
            throw "invalid function file";
      }
 }

Context

StackExchange Code Review Q#3389, answer score: 3

Revisions (0)

No revisions yet.