문제

다음은 내가 가진 하나의 방법의 단순화 된 버전입니다.
여전히 매우 복잡해 보입니다
이 광기를 어떻게 리팩터링할까요?

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;
            }
        }
    }
}

상태 패턴을 사용하여 리팩터를 시도하지만 패턴에 익숙하지 않기 때문에 소용이 없습니다.
이것은 내가 한 일이지만 빠르게 abond입니다.

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_num 그리고 allow_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