I have many IF sentences that each start a function.
Is there an obvious way to write this code much simpler?
Every IF starts different function, but it still looks like an overkill.
if ($this->machine == '' AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like == '' AND $this->article_or_tool == '') {
$this->AllTime();
}
if ($this->machine <> 0 AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like == '' AND $this->article_or_tool == '') {
$this->ByMachine();
}
if ($this->machine == '' AND $this->date_from <> 0 AND $this->date_to <> 0 AND $this->date_like == '' AND $this->article_or_tool == '') {
$this->ByDate();
}
if ($this->machine <> 0 AND $this->date_from <> 0 AND $this->date_to <> 0 AND $this->date_like == '' AND $this->article_or_tool == '') {
$this->ByMachineByDate();
}
if ($this->machine == '' AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like <> 0 AND $this->article_or_tool == '') {
$this->ByDateLike();
}
if ($this->machine <> 0 AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like <> 0 AND $this->article_or_tool == '') {
$this->ByMachineByDateLike();
}
if ($this->machine == '' AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like == '' AND $this->article_or_tool <> 0) {
$this->ByArticle();
}
if ($this->machine <> 0 AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like == '' AND $this->article_or_tool <> 0) {
$this->ByMachineByArticle();
}
if ($this->machine == '' AND $this->date_from <> 0 AND $this->date_to <> 0 AND $this->date_like == '' AND $this->article_or_tool <> 0) {
$this->ByDateByArticle();
}
if ($this->machine == '' AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like <> 0 AND $this->article_or_tool <> 0) {
$this->ByDateLikeByArticle();
}
if ($this->machine <> 0 AND $this->date_from <> 0 AND $this->date_to <> 0 AND $this->date_like == '' AND $this->article_or_tool <> 0) {
$this->ByMachineByDateByArticle();
}
if ($this->machine <> 0 AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like <> 0 AND $this->article_or_tool <> 0) {
$this->ByMachineByDateLikeByArticle();
}
SOLUTION
Here is my code after refactoring it:
function MethodPicker() {
$machine = $this->machine <> 0;
$date_from = $this->date_from <> 0;
$date_to = $this->date_to <> 0;
$date_like = $this->date_like <> 0;
$article_or_tool = $this->article_or_tool <> 0;
$decision = array($machine, $date_from, $date_to, $date_like, $article_or_tool);
$decisions = array(
'AllTime' => array(false, false, false, false, false ),
'ByMachine' => array(true, false, false, false, false ),
'ByDate' => array(false, true, true, false, false ),
'ByMachineByDate' => array(true, true, true, false, false ),
'ByDateLike' => array(false, false, false, true, false ),
'ByMachineByDateLike' => array(true, false, false, true, false ),
'ByArticle' => array(false, false, false, false, true ),
'ByMachineByArticle' => array(true, false, false, false, true ),
'ByDateByArticle' => array(false, true, true, false, true ),
'ByDateLikeByArticle' => array(false, false, false, true, true ),
'ByMachineByDateByArticle' => array(true, true, true, false, true ),
'ByMachineByDateLikeByArticle' => array(true, false, false, true, true ),
);
$method = array_keys($decisions, $decision, true);
$method && list($method) = $method;
$method && $this->$method();
}
First of all I would do some standard refactorings. No idea why I do it that way, but here is what:
Replace the properties with local variables, like
Same would work for the conditions however looking closer to the conditions it becomes clear that you have two states only for each variable, so this is actually one condition per variable only (see Type Juggling) which results in
trueorfalse. Assign the condition, instead:(Credits go to martinstoeckli for the correct condition)
This would be a start. The if clauses until now would have already changed and would be more compact. However, why stop here? There is a decision that is current:
And there is a set of decisions to choose from:
So all that needs to be done is to find the decision and execute the method:
The if block has been turned into a matrix. The function has been mapped to one state of it.
You loose the names on the fields, however, you could solve that with a comment:
At a glance:
If the class this is in represent a value object, I suggest you move the decisions into a type of it’s own and then just use that decision type as a single method object. Will enable you later on to do different sets of decisions more easily.