Freetagging::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

53
    $q = /** @scrutinizer ignore-call */ db_query($sq, [':vid' => $vocabulary->vid]);
Loading history...
54
    $result = [
55
      'vocabulary' => $vocabulary,
56
      'terms' => [],
57
    ];
58
59
    foreach ($q->fetchAll() as $o) {
60
      $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

60
      $term = /** @scrutinizer ignore-call */ taxonomy_term_load($o->tid); // has an internal cache, so we may loop
Loading history...
61
      $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

61
      $result['terms'][$term->tid] = /** @scrutinizer ignore-call */ l($term->name, 'admin/content/taxonomy/edit/term/'. $term->tid, [
Loading history...
62
        'query' => ['destination' => 'admin/reports/qa/result'],
63
      ]);
64
    }
65
    $ret = [
66
      'name'   => $vocabulary->name,
67
      'status' => empty($result['terms']) ? 1 : 0,
68
      'result' => $result,
69
    ];
70
    return $ret;
71
  }
72
73
  public function run(): Pass {
74
    $pass = parent::run();
75
    $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

75
    $vocabularies = /** @scrutinizer ignore-call */ taxonomy_get_vocabularies();
Loading history...
76
    foreach ($vocabularies as $vocabulary) {
77
      if (!empty($vocabulary->tags)) {
78
        $pass->record($this->checkTags($vocabulary));
79
      }
80
    }
81
    $pass->life->end();
82
83
    // Prepare for theming
84
    $result = '';
85
    uksort($pass->result, 'strcasecmp'); // @XXX May be inconsistent with non-BMP strings ?
86
    foreach ($pass->result as $vocabulary_name => $info) {
87
      $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

87
      $vocabulary_link = /** @scrutinizer ignore-call */ l($vocabulary_name, 'admin/content/taxonomy/'. $info['vocabulary']->vid);
Loading history...
88
      $result[] = t('!link: !terms', [
89
        '!link' => $vocabulary_link,
90
        '!terms' => implode(', ', $info['terms']),
91
      ]);
92
    }
93
    $result = empty($result)
0 ignored issues
show
introduced by
The condition empty($result) is always true.
Loading history...
94
      ? t('All tags are in use')
95
      : 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

95
      : /** @scrutinizer ignore-call */ theme('item_list', $result);
Loading history...
96
    $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...
97
    return $pass;
98
  }
99
}
100