2016-04-13 32 views
-1

我想簡化這個功能。我還發表它的一小部分,但它每次都相同的原則:在PHP函數中縮短了類似的命令

if(in_array($infinitiveVerb, 
    IrregularExceptionGroup::$name_in_lowercase)) { 
     $exceptionmodel = ExceptionModel::NAME_IN_UPPERCASE; 
} 

PHP函數

function finding_exception_model(InfinitiveVerb $infinitiveVerb) 
{ 
    $exceptionmodel = ExceptionModel::NO_EXCEPTIONS; 
    if (in_array($infinitiveVerb, IrregularExceptionGroup::$aller)) { 
     $exceptionmodel = ExceptionModel::ALLER; 
    } 
    if (in_array($infinitiveVerb, IrregularExceptionGroup::$avoir_irr)) { 
     $exceptionmodel = ExceptionModel::AVOIR_IRR; 
    } 
    if (in_array($infinitiveVerb, IrregularExceptionGroup::$etre_irr)) { 
     $exceptionmodel = ExceptionModel::ETRE_IRR; 
    } 
    return new ExceptionModel($exceptionmodel); 
} 

ExceptionModel.php

class ExceptionModel extends Enum 
{  
    const NO_EXCEPTIONS = 'no exceptions'; 
    const ALLER = 'aller'; 
    const AVOIR_IRR = 'avoir_irr'; 
    const ETRE_IRR = 'etre_irr'; 
} 

如何這可能嗎?

+0

究竟是什麼問題?我不知道你在問什麼。 – DevDonkey

+0

你知道你在每個if語句中覆蓋變量,如果它進入它?你確定你不想要if/elseif嗎? – Rizier123

+0

我不想有類似條件,因爲它們非常相似。如何爲所有'ExceptionModel'使用條件。 – Grischa

回答

1

我可以看到,並會改變這裏的唯一事情,就是隻要把每一個irregularExceptionGroup到一個數組,像這樣:

function finding_exception_model(InfinitiveVerb $infinitiveVerb) 
{ 

    $exceptionmodel = ExceptionModel::NO_EXCEPTIONS; 

    $irregularExceptionGroupArray = [ 
      ExceptionModel::ALLER => IrregularExceptionGroup::$aller, 
      ExceptionModel::AVOIR_IRR => IrregularExceptionGroup::$avoir_irr, 
      ExceptionModel::ETRE_IRR => IrregularExceptionGroup::$etre_irr, 
     ]; 

    foreach($irregularExceptionGroupArray as $exceptionModel => $irregularExceptionGroup){ 
     if(in_array($infinitiveVerb, $irregularExceptionGroup)){ 
      $exceptionmodel = $exceptionModel; 
      //break; //If you don't want to overwrite the variable, just uncomment this 
     } 
    } 

    return new ExceptionModel($exceptionmodel); 
} 
0

您都可以例外合併成一個查詢,而不是此in_array檢查所有時間。 (注:如果關閉過程中InfinitVerb具有某種類型的__toString)

例如IrregularExceptionGroup::$aller包含:['aller', 'allez', 'je suis', 'paris']IrregularExceptionGroup::$avoir_irr包含['some', 'more', 'stuff']

將其更改爲:

IrregularExceptionGroup::$allExceptions = [ 
    'aller' => ExceptionModel::ALLER, 
    'allez' => ExceptionModel::ALLER, 
    'je suis' => ExceptionModel::ALLER, 
    'paris' => ExceptionModel::ALLER, 
    'some' => ExceptionModel::AVOIR_IRR, 
    'more' => ExceptionModel::AVOIR_IRR, 
    'stuff' => ExceptionModel::AVOIR_IRR 
]; 


function finding_exception_model(InfinitiveVerb $infinitiveVerb) 
{ 
    $ex = ExceptionModel::NO_EXCEPTIONS; 
    if (array_key_exists($infinitiveVerb, IrregularExceptionGroup::$allExceptions)) { 
     $ex = IrregularExceptionGroup::$allExceptions[$infinitiveVerb]; 
    } 

    return new ExceptionModel($ex); 
} 

你甚至可以把它通過使用三元運算符「更短」:

function finding_exception_model(InfinitiveVerb $infinitiveVerb) 
{ 
    return new ExceptionModel(isset(IrregularExceptionGroup::$allExceptions[$infinitiveVerb]) ? IrregularExceptionGroup::$allExceptions[$infinitiveVerb] : ExceptionModel::NO_EXCEPTIONS); 
} 

或PHP 7:

function finding_exception_model(InfinitiveVerb $infinitiveVerb) 
{ 
    return new ExceptionModel(IrregularExceptionGroup::$allExceptions[$infinitiveVerb] ?? ExceptionModel::NO_EXCEPTIONS); 
}