Variables::checkMissing()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 31
rs 9.2568
cc 5
nc 12
nop 0
1
<?php
2
3
namespace Drupal\qa\I18n;
4
5
use Drupal\qa\Pass;
6
use Drupal\qa\Plugin\Qa\Control\BaseControl;
7
8
/**
9
 * Find inconsistencies in {i18nvariables} and {languages}
10
 */
11
class Variables extends BaseControl {
12
13
  /**
14
   * @var string
15
   */
16
  protected $package_name;
17
18
  /**
19
   * @var \Drupal\Core\StringTranslation\TranslatableMarkup
20
   */
21
  protected $description;
22
23
  /**
24
   * @var \Drupal\Core\StringTranslation\TranslatableMarkup
25
   */
26
  protected $title;
27
28
  /**
29
   * {@inheritdoc]
30
   */
31
  public function init() {
32
    $this->package_name = __NAMESPACE__;
33
    $this->title = t('Inconsistent variables translation');
34
    $this->description = t('In most scenarios, when a variable is translated at least once, it ought to be translated in every language on the site, not more, not less.');
35
  }
36
37
  /**
38
   * Identify variables translations for languages not currently on site
39
   */
40
  public function checkExtra() {
41
    $languages = array_keys(language_list());
0 ignored issues
show
Bug introduced by
The function language_list 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

41
    $languages = array_keys(/** @scrutinizer ignore-call */ language_list());
Loading history...
42
    $ph = db_placeholders($languages, 'char');
0 ignored issues
show
Bug introduced by
The function db_placeholders 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

42
    $ph = /** @scrutinizer ignore-call */ db_placeholders($languages, 'char');
Loading history...
43
    $sq = <<<sql
44
SELECT v.name, v.language
45
FROM {i18n_variable} v
46
WHERE v.language NOT IN ($ph)
47
ORDER BY 1, 2
48
sql;
49
    $q = db_query($sq, $languages);
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

49
    $q = /** @scrutinizer ignore-call */ db_query($sq, $languages);
Loading history...
50
    // No db_rewrite_sql: this needs to be unhindered by any access control
51
    $vars = [];
52
    while ($o = db_fetch_object($q)) {
0 ignored issues
show
Bug introduced by
The function db_fetch_object 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

52
    while ($o = /** @scrutinizer ignore-call */ db_fetch_object($q)) {
Loading history...
53
      $vars[$o->name][] = $o->language;
54
    }
55
56
    $items = [];
57
    foreach ($vars as $name => $var_languages) {
58
      $items[] = t('@var: @languages',
59
        ['@var' => $name, '@languages' => implode(', ', $var_languages)]);
60
    }
61
    $ret = [
62
      'name' => 'extra',
63
      'status' => empty($vars) ? 1 : 0,
64
      'result' => ['extra' => $items],
65
    ];
66
    return $ret;
67
  }
68
69
  /**
70
   * Identify variables for which at least one translation is missing
71
   */
72
  public function checkMissing() {
73
    $languages = array_keys(language_list());
0 ignored issues
show
Bug introduced by
The function language_list 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
    $languages = array_keys(/** @scrutinizer ignore-call */ language_list());
Loading history...
74
    $ph = db_placeholders($languages, 'char');
0 ignored issues
show
Bug introduced by
The function db_placeholders 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

74
    $ph = /** @scrutinizer ignore-call */ db_placeholders($languages, 'char');
Loading history...
75
    $sq = <<<sql
76
SELECT v.name, v.language
77
FROM {i18n_variable} v
78
WHERE v.language IN ($ph)
79
ORDER BY 1, 2
80
sql;
81
    $q = db_query($sq, $languages);
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

81
    $q = /** @scrutinizer ignore-call */ db_query($sq, $languages);
Loading history...
82
    // No db_rewrite_sql: this needs to be unhindered by any access control
83
    $vars = [];
84
    while ($o = db_fetch_object($q, $ph)) {
0 ignored issues
show
Bug introduced by
The function db_fetch_object 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

84
    while ($o = /** @scrutinizer ignore-call */ db_fetch_object($q, $ph)) {
Loading history...
85
      $vars[$o->name][] = $o->language;
86
    }
87
88
    $items = [];
89
    foreach ($vars as $name => $var_languages) {
90
      $missing = array_diff($languages, $var_languages);
91
      if (!empty($missing)) {
92
        $items[] = t('@var: @languages',
93
          ['@var' => $name, '@languages' => implode(', ', $missing)]);
94
      }
95
    }
96
97
    $ret = [
98
      'name' => 'missing',
99
      'status' => empty($items) ? 1 : 0,
100
      'result' => ['missing' => $items],
101
    ];
102
    return $ret;
103
  }
104
105
  public static function getDependencies(): array {
106
    $ret = ['i18n']; // introduces {i18n_variable}
107
    return $ret;
108
  }
109
110
  public function run(): Pass {
111
    $pass = parent::run();
112
    $pass->record($this->checkExtra());
113
    $pass->life->modify();
114
    $pass->record($this->checkMissing());
115
    $pass->life->end();
116
117
    $extra_row = empty($pass->result['extra']['extra'])
118
      ? [['data' => t('No extra translation found'), 'colspan' => 2,]]
119
      : [
120
        t('Extra translations'),
121
        theme('item_list', $pass->result['extra']['extra']),
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

121
        /** @scrutinizer ignore-call */ 
122
        theme('item_list', $pass->result['extra']['extra']),
Loading history...
122
      ];
123
124
    $missing_row = empty($pass->result['missing']['missing'])
125
      ? [
126
        [
127
          'data' => t('No missing translation found'),
128
          'colspan' => 2,
129
        ],
130
      ]
131
      : [
132
        t('Missing translations'),
133
        theme('item_list', $pass->result['missing']['missing']),
134
      ];
135
136
    $rows = [$extra_row, $missing_row];
137
    $pass->result = theme('table', NULL, $rows);
138
    return $pass;
139
  }
140
}
141