Php 如何减少IF语句的数量?
我有许多IF语句,每一个都开始一个函数。Php 如何减少IF语句的数量?,php,class,if-statement,refactoring,dry,Php,Class,If Statement,Refactoring,Dry,我有许多IF语句,每一个都开始一个函数。 有没有一种明显的方法可以让代码编写得更简单? 每个IF启动不同的函数,但看起来还是有点过头了 if ($this->machine == '' AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like == '' AND $this->article_or_tool == '') { $this->AllTi
有没有一种明显的方法可以让代码编写得更简单?
每个IF启动不同的函数,但看起来还是有点过头了
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();
}
if($this->machine==''和$this->date\u from=''和$this->date\u to=''和$this->date\u like=''和$this->article\u或\u tool=''){
$this->AllTime();
}
如果($this->machine 0和$this->date\u from==''和$this->date\u to=''和$this->date\u like=''和$this->article\u或\u tool=''){
$this->ByMachine();
}
如果($this->machine==''和$this->date_从0和$this->date_到0和$this->date_like=''和$this->article_或_tool=''){
$this->ByDate();
}
如果($this->machine 0和$this->date_从0和$this->date_到0和$this->date_like=''和$this->article_或_tool=''){
$this->ByMachineByDate();
}
如果($this->machine==''和$this->date\u from=''和$this->date\u to=''和$this->date\u类似于0和$this->article\u或\u tool=''){
$this->ByDateLike();
}
如果($this->machine 0和$this->date\u from===''和$this->date\u to==''和$this->date\u像0和$this->article\u或\u tool==''){
$this->ByMachineByDateLike();
}
如果($this->machine==''和$this->date\u from=''和$this->date\u to=''和$this->date\u like=''和$this->article\u或工具0){
$this->ByArticle();
}
如果($this->machine 0和$this->date\u from==''和$this->date\u to==''和$this->date\u like==''和$this->article\u或工具0){
$this->ByMachineByArticle();
}
如果($this->machine==''和$this->date_从0和$this->date_到0和$this->date_like=''和$this->article_或_工具0){
$this->ByDateByArticle();
}
如果($this->machine==''和$this->date\u from==''和$this->date\u to==''和$this->date\u类似于0和$this->article\u或\u工具0){
$this->ByDateLikeByArticle();
}
如果($this->machine 0和$this->date_从0和$this->date_到0和$this->date_like==''和$this->article_或_工具0){
$this->ByMachineByDateByArticle();
}
如果($this->machine 0和$this->date\u from===''和$this->date\u to==''和$this->date\u像0和$this->article\u或工具0){
$this->ByMachineByDateLikeByArticle();
}
解决方案以下是重构后的代码:
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();
}
function MethodPicker(){
$machine=$this->machine 0;
$date\u from=$this->date\u from 0;
$date\u to=$this->date\u to 0;
$date\u like=$this->date\u like 0;
$article\u或\u tool=$this->article\u或\u tool 0;
$decision=array($machine、$date\u from、$date\u to、$date\u like、$article\u或\u tool);
$decisions=数组(
“AllTime”=>数组(假、假、假、假、假),
“ByMachine”=>数组(真、假、假、假、假),
“ByDate”=>数组(假、真、真、假、假),
“ByMachineByDate”=>数组(真、真、真、假、假),
'ByDateLike'=>数组(false,false,false,true,false),
'ByMachineByDateLike'=>数组(真、假、假、真、假),
'ByArticle'=>数组(false,false,false,false,true),
“ByMachineByArticle”=>数组(真、假、假、假、真),
'ByDateByArticle'=>数组(false,true,true,false,true),
'ByDateLikeByArticle'=>数组(false,false,false,true,true),
“ByMachineByDateByArticle”=>数组(真、真、真、假、真),
“ByMachineByDateLikeByArticle”=>数组(真、假、假、真、真),
);
$method=array_key($decision,$decision,true);
$method&&list($method)=$method;
$method&&$this->$method();
}
很多都是重复,因此可以嵌套条件以删除冗余,例如:
if ($this->machine == '') {
// do everything requiring empty 'machine' string
// remove condition from all subsequent ifs in this context
} else if ($this-> machine <> 0) {
}
if($this->machine==''){
//执行所有需要空“机器”字符串的操作
//在此上下文中,从所有后续ifs中删除条件
}else if($this->machine 0){
}
我没有时间也没有兴趣去阅读所有这些代码,并为您实际执行这些操作,但是这个想法应该为您提供足够的信息,以便您作为读者练习来实现。(:有时,如果您的条件不同,这是不可能的,但在这种情况下,您可以从分组您的
if()
s开始,因为许多人共享相同的条件。例如,不要有太多的
if( $this->machine <> 0 AND .... )
if($this->machine 0 AND…)
您可以将它们全部分组:
if( $this->machine <> 0 ) {
// all related ifs there
}
if($this->machine 0){
//所有相关的国际单项体育联合会都在那里
}
然后继续下一个“级别”的条件。虽然这可能不会减少
if()的总数
s,您的代码将比现在更具可读性。也许您真的需要所有这些决策,但您可以使其更易于阅读。您可以先将此值设置为一个有意义的变量,而不是在if语句中多次写入$this->machine=='
$machineIsEmpty = $this->machine == '' || $this->machine == 0;
$dateFromIsEmpty = $this->date_from == '' || $this->machine == 0;
...
if ($machineIsEmpty && $dateFromIsEmpty && $dateToIsEmpty && $dateLikeIsEmpty && $articleOrToolIsEmpty)
{
$this->AllTime();
}
else if (!$machineIsEmpty && $dateFromIsEmpty && $dateToIsEmpty && $dateLikeIsEmpty && $articleOrToolIsEmpty)
{
$this->ByMachine();
}
...
在本例中,我假设了两件事:首先,我怀疑您希望将值'
和0
处理为未设置。我不确定这一点,因为在任何情况下都不会对值0
执行任何操作
其次,我假设如果调用了一个函数,您不想调用更多的函数,因此我在下一个if之前添加了一个else
$myObject = new TestClass();
$myObject->DoAction($machineIsSet, $dateFromIsSet, $dateToIsSet, $dateLikeIsSet, $articleToolIsSet);
class TestClass
{
private $actionMatrix = array(
// machine, dateFrom, dateTo, dateLike, articleOrTool, action
array(false, false, false, false, false, 'AllTime'),
array(true, false, false, false, false, 'ByMachine')
);
public function DoAction($machine, $dateFrom, $dateTo, $dateLike, $articleOrTool)
{
foreach($this->actionMatrix as $action)
{
if (($action[0] == $machine) && ($action[1] == $dateFrom) && ($action[2] == $dateTo) && ($action[3] == $dateLike) && ($action[4] == $articleOrToolLike))
{
$functionName = $action[5];
$this->$functionName(); // call the function
break;
}
}
// no action found, maybe we want some error handling here?
}
public function AllTime()
{
echo('AllTime');
}
public function ByMachine()
{
echo('ByMachine');
}
}
$method = '';
if (this->machine <> 0) $method.="ByMachine";
if ($this->date_from <> 0 AND $this->date_to <> 0) $method.="ByDate"
.....
//here you have full $method name ByMachineByDateByArticle or ByMachineByDateByArticle etc
if($method){
call_user_func_array(array($this, $method));
}
$machine = $this->machine;
$machine = $this->machine == '' || $this->machine == 0;
$decision = [$machine, $date_from, $date_to, $date_like, $article_or_tool];
$decisions = [
'AllTime' => [true, true, true, true, true],
...
];
$method = array_keys($decisions, $decision, true);
$method && $this->$method();
$decisions = [
// machine from to like article
'AllTime' => [true , true, true, true, true],
...
];
$machine = $this->machine == '' || $this->machine == 0;
... # 4 more times
$decision = [$machine, $date_from, $date_to, $date_like, $article_or_tool];
$decisions = [
'AllTime' => [true, true, true, true, true],
... # 11 more times
];
$method = array_keys($decisions, $decision, true);
$method && $this->$method();