Completed
Push — 8.x-1.x ( 626d2d...5d8f74 )
by Frédéric G.
26s queued 12s
created

Freetagging::getDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Drupal\qa\Taxonomy;
4
5
use Drupal\qa\BaseControl;
6
7
/**
8
 * Find views containing PHP code
9
 */
10
class Freetagging extends BaseControl {
11
12
  /**
13
   * {@inheritdoc]
14
   */
15
  public function __construct() {
16
    parent::__construct();
17
    $this->package_name = __NAMESPACE__;
18
  }
19
20
  public static function getDependencies() {
21
    $ret = BaseControl::getDependencies();
22
    $ret = array_merge($ret, ['taxonomy']);
23
    return $ret;
24
  }
25
26
  /**
27
   * {@inheritdoc]
28
   */
29
  public function init() {
30
    $this->package_name = __NAMESPACE__;
31
    $this->title = t('Unused freetagging terms');
32
    $this->description = t('Unused freetagging terms mean useless volume. Removing them helps making term autocompletion more relevant.');
33
  }
34
35
  /**
36
   * List unused tags.
37
   *
38
   * @param object $vocabulary
39
   *
40
   * @return array
41
   */
42
  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...
43
    $sq = <<<sql
44
SELECT td.tid
45
FROM {taxonomy_term_data} td
46
  LEFT JOIN {taxonomy_index} tn ON td.tid = tn.tid
47
WHERE
48
  td.vid = :vid AND tn.nid IS NULL
49
sql;
50
    // no db_rewrite_sql(): we are checking the whole database
51
    $q = db_query($sq, [':vid' => $vocabulary->vid]);
0 ignored issues
show
Bug introduced by
The function db_query was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
    $q = /** @scrutinizer ignore-call */ db_query($sq, [':vid' => $vocabulary->vid]);
Loading history...
52
    $result = [
53
      'vocabulary' => $vocabulary,
54
      'terms' => [],
55
    ];
56
57
    foreach ($q->fetchAll() as $o) {
58
      $term = taxonomy_term_load($o->tid); // has an internal cache, so we may loop
0 ignored issues
show
Bug introduced by
The function taxonomy_term_load was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
      $term = /** @scrutinizer ignore-call */ taxonomy_term_load($o->tid); // has an internal cache, so we may loop
Loading history...
59
      $result['terms'][$term->tid] = l($term->name, 'admin/content/taxonomy/edit/term/'. $term->tid, [
0 ignored issues
show
Bug introduced by
The function l was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
      $result['terms'][$term->tid] = /** @scrutinizer ignore-call */ l($term->name, 'admin/content/taxonomy/edit/term/'. $term->tid, [
Loading history...
60
        'query' => ['destination' => 'admin/reports/qa/result'],
61
      ]);
62
    }
63
    $ret = [
64
      'name'   => $vocabulary->name,
65
      'status' => empty($result['terms']) ? 1 : 0,
66
      'result' => $result,
67
    ];
68
    return $ret;
69
  }
70
71
  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...
72
    $pass = parent::run();
73
    $vocabularies = taxonomy_get_vocabularies();
0 ignored issues
show
Bug introduced by
The function taxonomy_get_vocabularies was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
    $vocabularies = /** @scrutinizer ignore-call */ taxonomy_get_vocabularies();
Loading history...
74
    foreach ($vocabularies as $vocabulary) {
75
      if (!empty($vocabulary->tags)) {
76
        $pass->record($this->checkTags($vocabulary));
77
      }
78
    }
79
    $pass->life->end();
80
81
    // Prepare for theming
82
    $result = '';
83
    uksort($pass->result, 'strcasecmp'); // @XXX May be inconsistent with non-BMP strings ?
84
    foreach ($pass->result as $vocabulary_name => $info) {
85
      $vocabulary_link = l($vocabulary_name, 'admin/content/taxonomy/'. $info['vocabulary']->vid);
0 ignored issues
show
Bug introduced by
The function l was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
      $vocabulary_link = /** @scrutinizer ignore-call */ l($vocabulary_name, 'admin/content/taxonomy/'. $info['vocabulary']->vid);
Loading history...
86
      $result[] = t('!link: !terms', [
87
        '!link' => $vocabulary_link,
88
        '!terms' => implode(', ', $info['terms']),
89
      ]);
90
    }
91
    $result = empty($result)
0 ignored issues
show
introduced by
The condition empty($result) is always true.
Loading history...
92
      ? t('All tags are in use')
93
      : theme('item_list', $result);
0 ignored issues
show
Bug introduced by
The function theme was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
      : /** @scrutinizer ignore-call */ theme('item_list', $result);
Loading history...
94
    $pass->result = $result;
0 ignored issues
show
Documentation Bug introduced by
It seems like $result of type Drupal\Core\StringTranslation\TranslatableMarkup is incompatible with the declared type array of property $result.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
95
    return $pass;
96
  }
97
}
98