Completed
Push — 7.x-1.x ( 2f9e3c...1070be )
by Frédéric G.
01:36
created

Freetagging   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
B checkTags() 0 28 3
B run() 0 26 5
1
<?php
2
3
namespace Drupal\qa\Plugin\Qa\Control\Taxonomy;
4
5
/**
6
 * Find views containing PHP code
7
 */
8
class Freetagging extends Taxonomy {
9
10
  /**
11
   * {@inheritdoc]
12
   */
13
  public function init() {
14
    $this->package_name = __NAMESPACE__;
15
    $this->title = t('Unused freetagging terms');
16
    $this->description = t('Unused freetagging terms mean useless volume. Removing them helps making term autocompletion more relevant.');
17
  }
18
19
  /**
20
   * List unused tags.
21
   *
22
   * @param object $vocabulary
23
   *
24
   * @return array
25
   */
26
  function checkTags($vocabulary) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
    $sq = <<<sql
28
SELECT td.tid
29
FROM {taxonomy_term_data} td
30
  LEFT JOIN {taxonomy_index} tn ON td.tid = tn.tid
31
WHERE
32
  td.vid = :vid AND tn.nid IS NULL
33
sql;
34
    // no db_rewrite_sql(): we are checking the whole database
35
    $q = db_query($sq, array(':vid' => $vocabulary->vid));
36
    $result = array(
37
      'vocabulary' => $vocabulary,
38
      'terms' => array(),
39
    );
40
41
    foreach ($q->fetchAll() as $o) {
42
      $term = taxonomy_term_load($o->tid); // has an internal cache, so we may loop
43
      $result['terms'][$term->tid] = l($term->name, 'admin/content/taxonomy/edit/term/'. $term->tid, array(
44
        'query' => array('destination' => 'admin/reports/qa/result'),
45
      ));
46
    }
47
    $ret = array(
48
      'name'   => $vocabulary->name,
49
      'status' => empty($result['terms']) ? 1 : 0,
50
      'result' => $result,
51
    );
52
    return $ret;
53
  }
54
55
  function run() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
56
    $pass = parent::run();
57
    $vocabularies = taxonomy_get_vocabularies();
58
    foreach ($vocabularies as $vocabulary) {
59
      if (!empty($vocabulary->tags)) {
60
        $pass->record($this->checkTags($vocabulary));
61
      }
62
    }
63
    $pass->life->end();
64
65
    // Prepare for theming
66
    $result = '';
67
    uksort($pass->result, 'strcasecmp'); // @XXX May be inconsistent with non-BMP strings ?
68
    foreach ($pass->result as $vocabulary_name => $info) {
69
      $vocabulary_link = l($vocabulary_name, 'admin/content/taxonomy/'. $info['vocabulary']->vid);
70
      $result[] = t('!link: !terms', array(
71
        '!link' => $vocabulary_link,
72
        '!terms' => implode(', ', $info['terms']),
73
      ));
74
    }
75
    $result = empty($result)
76
      ? t('All tags are in use')
77
      : theme('item_list', $result);
78
    $pass->result = $result;
79
    return $pass;
80
  }
81
}
82