title: Refactor nested if statement
body: <p>Here is simplified version of one method i have.<br>
  Still look very complex<br>
  How would one refactor this insanity?  </p>
  
  <pre><code>protected function isTextValid()
  {
      if( $this-&gt;data['allow_num'] ){
          if( $this-&gt;data['allow_space'] ){
              if( preg_match( '#^[a-zA-Z0-9\s]$#', $this-&gt;val ) ){
                  return true;
              }
              else{
                  $this-&gt;messages = foo ? foo : bar;
                  return false;
              }
          }
          else{
              if( preg_match( '#^[a-zA-Z0-9]$#', $this-&gt;val ) ){
                  return true;
              }
              else{
                  $this-&gt;messages = foo? foor : bar;
                  return false;
              }
          }
      }
      else{
          if( $this-&gt;data['allow_space'] ){
              if( preg_match( '#^[a-zA-Z\s]$#', $this-&gt;val ) ){
                  return true;
              }
              else{
                  $this-&gt;messages = foo ? foor : bar;
                  return false;
              }
          }
          else{
              if( preg_match( '#^[a-zA-Z]$#', $this-&gt;val  ) ){
                  return true;
              }
              else{
                  $this-&gt;messages =  foo ? foo: bar;
                  return false;
              }
          }
      }
  }
  </code></pre>
  
  <p>I try refactor it using state pattern but up to no avail since im not quite familiar with the pattern.<br>
  This is what i done but quickyly abondon it.  </p>
  
  <pre><code>interface ValidatorState{
    public function isValid();
  }
  
  class AllowNumberAndSpace implement ValidatorState{
     protected $context;
     public function __construct(Context $context){$this-&gt;context = $context}
  
      public function isValid(){
         if( preg_match( .. ) ){
              return true;
          }
          else{
              $this-&gt;messages = foo ? foo : bar;
              return false;
          }
        $this-&gt;context-&gt;setState(OtherState);
      }
  }
  
  Class Context{
      protected $state;
      protected $allow_num_space_state;
  
      public function __construct(){
          $this-&gt;allow_num_space_state = new AllowNumberAndSpace($this);
         $this-&gt;state = $this-&gt;allow_num_space_state;
      }
  
     public function isValid(){
         return $this-&gt;state-&gt;isValid();
     }
  
    public function setState($state){$this-&gt;state = $state}
  }
  </code></pre>
  
  <p>Obviously this only test first <code>if</code> branch, how can i automatically check other branch too?<br>
  Im quite sure there is something wrong with my approach.<br>
  Is there a way to fix this state pattern to test for all <code>if</code> branch?</p>
  
  <p><strong>Edited</strong><br>
  What this method does is, it check whether <code>$this-&gt;value</code> contain expected value based on configuration attribute stored in <code>$this-&gt;data</code></p>
  
  <p>Example <code>$this-&gt;data = array('allow_num'=&gt;true)</code>, if <code>$this-&gt;value='a1'</code> it is consider valid
  Example <code>$this-&gt;data = array('allow_num'=&gt;false)</code>, if <code>$this-&gt;value='a1'</code> it is consider invalid</p>
  
  <p>Is there a way to simplified this method?</p>