質問

これは、私が持っている1つの方法の単純化されたバージョンです。
それでも非常に複雑に見えます
この狂気をどのようにリファクタリングしますか?

protected function isTextValid()
{
    if( $this->data['allow_num'] ){
        if( $this->data['allow_space'] ){
            if( preg_match( '#^[a-zA-Z0-9\s]$#', $this->val ) ){
                return true;
            }
            else{
                $this->messages = foo ? foo : bar;
                return false;
            }
        }
        else{
            if( preg_match( '#^[a-zA-Z0-9]$#', $this->val ) ){
                return true;
            }
            else{
                $this->messages = foo? foor : bar;
                return false;
            }
        }
    }
    else{
        if( $this->data['allow_space'] ){
            if( preg_match( '#^[a-zA-Z\s]$#', $this->val ) ){
                return true;
            }
            else{
                $this->messages = foo ? foor : bar;
                return false;
            }
        }
        else{
            if( preg_match( '#^[a-zA-Z]$#', $this->val  ) ){
                return true;
            }
            else{
                $this->messages =  foo ? foo: bar;
                return false;
            }
        }
    }
}

私は状態パターンを使用してリファクタリングしてみますが、パターンに精通していないため、最大限に役立ちません。
これは私がやったことですが、それを素早くアボンドンします。

interface ValidatorState{
  public function isValid();
}

class AllowNumberAndSpace implement ValidatorState{
   protected $context;
   public function __construct(Context $context){$this->context = $context}

    public function isValid(){
       if( preg_match( .. ) ){
            return true;
        }
        else{
            $this->messages = foo ? foo : bar;
            return false;
        }
      $this->context->setState(OtherState);
    }
}

Class Context{
    protected $state;
    protected $allow_num_space_state;

    public function __construct(){
        $this->allow_num_space_state = new AllowNumberAndSpace($this);
       $this->state = $this->allow_num_space_state;
    }

   public function isValid(){
       return $this->state->isValid();
   }

  public function setState($state){$this->state = $state}
}

明らかに、これは最初にのみテストされます if ブランチ、他のブランチも自動的にチェックするにはどうすればよいですか?
私のアプローチに何か問題があると確信しています。
この状態パターンを修正してすべてをテストする方法はありますか if ブランチ?

編集
この方法が行うことは、それが確認されるかどうかを確認します $this->value 格納されている構成属性に基づく期待値が含まれています $this->data

$this->data = array('allow_num'=>true), 、 もしも $this->value='a1' 有効な例を考慮してください $this->data = array('allow_num'=>false), 、 もしも $this->value='a1' 無効と見なされます

この方法を簡素化する方法はありますか?

役に立ちましたか?

解決

まず第一に、これを過度に複雑にしないようにしてください。私の意見では、コードはオブジェクト指向の設計パターンの使用を正当化するほど複雑ではありません。

私が見るように、あなたのコードは基本的に異なる正規表現で入力を検証することに要約します(これは、ユーザー指定のフラグに依存します。 allow_numallow_space.

したがって、私の提案は次のとおりです(基本的なリファクタリングは、以前の構成に基づいて正規表現の一部を構築しています 使用 検証ロジックにおけるこの式):

protected function isTextValid() {
    $allowedCharacters = 'A-Za-z';

    if ($this->data['allow_spaces']) {
        $allowedCharacters .= '\s';
    }
    if ($this->data['allow_num']) {
        $allowedCharacters .= '\d';
    }

    if (!preg_match("#^[{$allowedCharacters}]+\$#", $this->value)) {
        $this->messages = foo ? foor : bar;
        return false;
    }
    return true;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top