2013-05-30 72 views
1

http://plugins.trac.wordpress.org/browser/seo-content-helper/tags/1.1/get-data.phpPHP - 不要重複自己?

不要重複自己

我所知道的 「不要重複自己」。儘管如此,它在一些地方變得很混亂。下面的代碼包含三個代碼塊。

它們在某些方面類似,在其他方面也不相同。它在各方面都不遵循相同的模式。

它做什麼

它創建了一個數組,我環出的前端。

問題

是否有更好的方法可以做到這樣的東西?雜亂,更好的結構?

$message_start = '<strong>h2 tags</strong> - '; 
    $message_end = '<span class="counter">' . $this->count_h2 . '</span>'; 
    if($this->count_h2 == 0) { 
     $message = 'No tags found. Add some!'; 
     $array['content_editor']['count_h2']['status'] = 2; 
    } elseif($this->count_h2 == 1) { 
     $message = 'Some found. Too few!'; 
     $array['content_editor']['count_h2']['status'] = 1; 
    } else { 
     $message = 'Many found. Great!'; 
     $array['content_editor']['count_h2']['status'] = 0; 
    } 
    $array['content_editor']['count_h2']['message'] = $message_start . $message . $message_end; 
    $array['content_editor']['count_h2']['count'] = $this->count_h2; 

    $message_start = '<strong>h3-h6 tags</strong> - '; 
    $h2_h6 = $this->count_h3 + $this->count_h4 + $this->count_h5 + $this->count_h6; 
    $counter = ($h2_h6 == 0) ? '' : $h2_h6; 
    $message_end = '<span class="counter">' . $counter . '</span>'; 
    if($h2_h6 == 0) { 
     $message = 'No found. Add some!'; 
     $array['content_editor']['count_h3_h6']['status'] = 1; 
    } else { 
     $message = 'Found, great!'; 
     $array['content_editor']['count_h3_h6']['status'] = 0; 
    } 
    $array['content_editor']['count_h3_h6']['message'] = $message_start . $message . $message_end; 
    $array['content_editor']['count_h3_h6']['count'] = $this->h2_h6; 

    $message_start = '<strong>Title keywords</strong> - '; 
    $counter = ($this->found_keywords1_post_title == 0) ? '' : $this->found_keywords1_post_title; 
    $message_end = '<span class="counter">' . $counter . '</span>'; 
    if(count($this->keywords1) == 0) { 
     $message = 'No primary added.'; 
     $array['content_editor']['missing_keywords1_post_title']['status'] = 2; 
    } elseif($this->found_keywords1_post_title == 0) { 
     $message = 'No primary found.'; 
     $array['content_editor']['missing_keywords1_post_title']['status'] = 2; 
    } else { 
     $s = ($this->found_keywords1_post_title != 1) ? 's' : ''; 
     $message = 'Primary found.'; 
     $array['content_editor']['missing_keywords1_post_title']['status'] = 0; 
    } 
    $array['content_editor']['missing_keywords1_post_title']['message'] = $message_start . $message . $message_end; 
    $array['content_editor']['missing_keywords1_post_title']['count'] = $this->found_keywords1_post_title; 
+4

我認爲這個問題更適合http://codereview.stackexchange.com/ – Mene

+0

他們都有共同的模式。你只需要弄明白。 –

+1

整體大師級持有泛型結構等,然後擴展該類爲您正在尋找的每個特定的項目可能會使它更容易遵循/閱讀 – Dave

回答

0

我最後用乾淨的陣列和之前準備的變量。它使它更具可讀性。

$array = array(
     'content_editor' => array(
      'count_h2' => array(
       'message_start' => 'h2 tags', 
       'messages' => array(
        0 => 'Many found. Great!', 
        1 => 'Some found. Too few!', 
        2 => 'No tags found. Add some!' 
       ), 
       'count' => $count_h2['count'] 
      ), 
      'count_h3' => array(
       'message_start' => 'h3-h6 tags', 
       'messages' => array(
        0 => 'Found, great!', 
        1 => 'No tags found. Add some!' 
       ), 
       'count' => $count_h3['count'] 
      ), 
      'missing_keywords1_post_title' => array(
       'status' => $missing_keywords1_post_title['status'], 
       'message_start' => 'Title keywords', 
       'messages' => array(
        0 => 'Primary found!', 
        2 => 'No primary found!' 
       ), 
       'count' => $missing_keywords1_post_title['count'] 
      ), 
2

這裏是清潔你的代碼示例:

function pstrong($txt) { 
    return "<strong>$txt</strong>"; 
} 

function pcounter($txt) { 
    return '<span class="counter">' . $txt. '</span>'; 
} 

$this->count_h1 = 1; 
$this->h2_h6 = $this->count_h3 + $this->count_h4 + $this->count_h5 + $this->count_h6; 
$array = array(
    'count_h2' => array(

     'title' => 'h2 tags', 

     0 => array(
      'message' => 'No tags found. Add some!', 
      'status' => 2 
     ), 

     1 => array(
      'message' => 'Some found. Too few!', 
      'status' => 1 
     ), 

     'else' => array(
      'message' => 'Some found. Too few!', 
      'status' => 0 
     ) 
    ), 

    'h2_h6' => array(
     'title' => 'h3-h6 tags', 

     0 => array(
      'message' => 'No found. Add some!', 
      'status' => 1 
     ), 


     'else' => array(
      'message' => 'Found, great!', 
      'status' => 0 
     ) 
    ) 
); 

foreach($array as $key => $value) { 

    $message = (!empty($value['title'][$index]) ? $value['title'][$index]['message'] : $value['title']['else']['message']); 
    $array['content_editor'][$key][$index]['message'] = pstrong($value['title']) . $value['title'][$index]['message'] . pcounter($key); 
    $array['content_editor'][$key][$index]['count'] = $this->$key; 
} 
+0

這給了我一些想法。投了! –