How to make this code clean / readable?

The code below is part of a long line of if...elseif...else statment block where I am parsing a command as entered via a PHP CLI.

I hate the formatting. It follows the PSR, but it's still no fun to read.

Can anyone offer suggestions on how to clean this up so it is hyper-readable? Code runs perfectly, but it is hard to visually distguish where one set of code for a command ends and the next one starts.

$cmd = an object, which has methods for parsing and cleaning up commands.

LVL 32
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Blank lines are appropriate, and so are comments.  Indentation of four spaces is, in my opinion, sufficient to make control structures readable.

But that aside, something like this makes my CRAP antennae twitch:
if($cmd->startswith("user roles show")) {

Open in new window

CRAP is an acronym meaning Change Risk AntiPattern, and it generally refers to the amount of change that might be required to refactor the complexity out of an application.  It's often measured by levels of if() nesting, and a CRAP index higher than the minimum is considered ungood.

One way to lower the number of if() statements is to dispatch the commands directly to the modules that perform the work.  A RESTful design like what Laravel uses to dispatch requests can help here.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DrDamnitAuthor Commented:
This shows my python side coming through where strings as objects are quite handy.

Do you have a link our example of an appropriate RESTful interface here?
Ray PaseurCommented:
Yeah, Ruby on Rails treats everything as an object with its own sets of properties -- very handy.  But in PHP we have to call functions to discern the characteristics like strlen().

See if this helps:

Here is my (years old) take on APIs
DrDamnitAuthor Commented:
Somehow, I think we're talking past each other.

Based on your answers (and my understanding) an API is generally accessed over HTTP. We use SOAP (*shudder*) or REST as the structure / style.

But my question is related to cleaning up code that can be typed into a CLI, and which uses direct interactions with PHP and the database.

Once such CLI command might be:

users find byemail

This is processed by the Command class.

The "startsWith" is how I am figuring out what class of command has been given. Because these are all valid, but do different things:

users find byemail
users find byfirst ray
users find bylast paseur

Thus, if it starts with "users find", I know I am going to output the same thing, but the only difference is the WHERE statement in the query that finds these users.

Based on your feedback, and reading about CRAP, I ended up refactoring a lot of the code into their own functions, which makes this read better and got rid of the nested if statements:

But I am just not making the connection on how a RESTful style API would be applicable or relevant. Am I missing something?
DrDamnitAuthor Commented:
For reference, this is the Command class. (Posted because I think it is causing some confusion. I am well aware that PHP requires strlen() to get a string length).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.