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

Variables::checkExtra()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 8
nop 0
dl 0
loc 27
rs 8.5806
c 0
b 0
f 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());
26
    $ph = db_placeholders($languages, 'char');
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);
34
    // No db_rewrite_sql: this needs to be unhindered by any access control
35
    $vars = array();
36
    while ($o = db_fetch_object($q)) {
37
      $vars[$o->name][] = $o->language;
38
    }
39
40
    $items = array();
41
    foreach ($vars as $name => $var_languages) {
42
      $items[] = t('@var: @languages', array('@var' => $name, '@languages' => implode(', ', $var_languages)));
43
    }
44
    $ret = array(
45
      'name'   => 'extra',
46
      'status' => empty($vars) ? 1 : 0,
47
      'result' => array('extra' => $items)
48
    );
49
    return $ret;
50
  }
51
52
  /**
53
   * Identify variables for which at least one translation is missing
54
   */
55
  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...
56
    $languages = array_keys(language_list());
57
    $ph = db_placeholders($languages, 'char');
58
    $sq = <<<sql
59
SELECT v.name, v.language
60
FROM {i18n_variable} v
61
WHERE v.language IN ($ph)
62
ORDER BY 1, 2
63
sql;
64
    $q = db_query($sq, $languages);
65
    // No db_rewrite_sql: this needs to be unhindered by any access control
66
    $vars = array();
67
    while ($o = db_fetch_object($q, $ph)) {
68
      $vars[$o->name][] = $o->language;
69
    }
70
71
    $items = array();
72
    foreach ($vars as $name => $var_languages) {
73
      $missing = array_diff($languages, $var_languages);
74
      if (!empty($missing)) {
75
        $items[] = t('@var: @languages', array('@var' => $name, '@languages' => implode(', ', $missing)));
76
      }
77
    }
78
79
    $ret = array(
80
      'name'   => 'missing',
81
      'status' => empty($items) ? 1 : 0,
82
      'result' => array('missing' => $items),
83
    );
84
    return $ret;
85
  }
86
87
  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...
88
    $ret = parent::getDependencies();
89
    $ret = array_merge($ret, array('i18n')); // introduces {i18n_variable}
90
    return $ret;
91
  }
92
93
  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...
94
    $pass = parent::run();
95
    $pass->record($this->checkExtra());
96
    $pass->life->modify();
97
    $pass->record($this->checkMissing());
98
    $pass->life->end();
99
100
    $extra_row = empty($pass->result['extra']['extra'])
101
      ? array(array(
102
          'data' => t('No extra translation found'),
103
          'colspan' => 2,
104
          ),
105
        )
106
      : array(
107
          t('Extra translations'),
108
          theme('item_list', $pass->result['extra']['extra'])
109
        );
110
111
    $missing_row = empty($pass->result['missing']['missing'])
112
      ? array(array(
113
          'data' => t('No missing translation found'),
114
          'colspan' => 2,
115
          ),
116
        )
117
      : array(
118
          t('Missing translations'),
119
          theme('item_list', $pass->result['missing']['missing'])
120
        );
121
122
    $rows = array($extra_row, $missing_row);
123
    $pass->result = theme('table', NULL, $rows);
124
    return $pass;
125
  }
126
}
127