Passed
Pull Request — 8.x-1.x (#18)
by Frédéric G.
05:46
created

Variables::checkMissing()   A

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\Plugin\Qa\Control\I18n;
4
5
use Drupal\qa\Plugin\Qa\Control\BaseControl;
6
7
/**
8
 * Find inconsistencies in {i18nvariables} and {languages}
9
 */
10
class Variables extends BaseControl {
11
12
  /**
13
   * {@inheritdoc]
14
   */
15
  public function init() {
16
    $this->package_name = __NAMESPACE__;
17
    $this->title = t('Inconsistent variables translation');
18
    $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.');
19
  }
20
21
  /**
22
   * Identify variables translations for languages not currently on site
23
   */
24
  function checkExtra() {
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...
25
    $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

25
    $languages = array_keys(/** @scrutinizer ignore-call */ language_list());
Loading history...
26
    $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

26
    $ph = /** @scrutinizer ignore-call */ db_placeholders($languages, 'char');
Loading history...
27
    $sq = <<<sql
28
SELECT v.name, v.language
29
FROM {i18n_variable} v
30
WHERE v.language NOT IN ($ph)
31
ORDER BY 1, 2
32
sql;
33
    $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

33
    $q = /** @scrutinizer ignore-call */ db_query($sq, $languages);
Loading history...
34
    // No db_rewrite_sql: this needs to be unhindered by any access control
35
    $vars = [];
36
    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

36
    while ($o = /** @scrutinizer ignore-call */ db_fetch_object($q)) {
Loading history...
37
      $vars[$o->name][] = $o->language;
38
    }
39
40
    $items = [];
41
    foreach ($vars as $name => $var_languages) {
42
      $items[] = t('@var: @languages',
43
        ['@var' => $name, '@languages' => implode(', ', $var_languages)]);
44
    }
45
    $ret = [
46
      'name' => 'extra',
47
      'status' => empty($vars) ? 1 : 0,
48
      'result' => ['extra' => $items],
49
    ];
50
    return $ret;
51
  }
52
53
  /**
54
   * Identify variables for which at least one translation is missing
55
   */
56
  function checkMissing() {
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...
57
    $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

57
    $languages = array_keys(/** @scrutinizer ignore-call */ language_list());
Loading history...
58
    $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

58
    $ph = /** @scrutinizer ignore-call */ db_placeholders($languages, 'char');
Loading history...
59
    $sq = <<<sql
60
SELECT v.name, v.language
61
FROM {i18n_variable} v
62
WHERE v.language IN ($ph)
63
ORDER BY 1, 2
64
sql;
65
    $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

65
    $q = /** @scrutinizer ignore-call */ db_query($sq, $languages);
Loading history...
66
    // No db_rewrite_sql: this needs to be unhindered by any access control
67
    $vars = [];
68
    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

68
    while ($o = /** @scrutinizer ignore-call */ db_fetch_object($q, $ph)) {
Loading history...
69
      $vars[$o->name][] = $o->language;
70
    }
71
72
    $items = [];
73
    foreach ($vars as $name => $var_languages) {
74
      $missing = array_diff($languages, $var_languages);
75
      if (!empty($missing)) {
76
        $items[] = t('@var: @languages',
77
          ['@var' => $name, '@languages' => implode(', ', $missing)]);
78
      }
79
    }
80
81
    $ret = [
82
      'name' => 'missing',
83
      'status' => empty($items) ? 1 : 0,
84
      'result' => ['missing' => $items],
85
    ];
86
    return $ret;
87
  }
88
89
  static function getDependencies() {
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...
90
    $ret = parent::getDependencies();
91
    $ret = array_merge($ret, ['i18n']); // introduces {i18n_variable}
92
    return $ret;
93
  }
94
95
  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...
96
    $pass = parent::run();
97
    $pass->record($this->checkExtra());
98
    $pass->life->modify();
99
    $pass->record($this->checkMissing());
100
    $pass->life->end();
101
102
    $extra_row = empty($pass->result['extra']['extra'])
103
      ? [['data' => t('No extra translation found'), 'colspan' => 2,]]
104
      : [
105
        t('Extra translations'),
106
        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

106
        /** @scrutinizer ignore-call */ 
107
        theme('item_list', $pass->result['extra']['extra']),
Loading history...
107
      ];
108
109
    $missing_row = empty($pass->result['missing']['missing'])
110
      ? [
111
        [
112
          'data' => t('No missing translation found'),
113
          'colspan' => 2,
114
        ],
115
      ]
116
      : [
117
        t('Missing translations'),
118
        theme('item_list', $pass->result['missing']['missing']),
119
      ];
120
121
    $rows = [$extra_row, $missing_row];
122
    $pass->result = theme('table', NULL, $rows);
123
    return $pass;
124
  }
125
}
126