Passed
Pull Request — 8.x-1.x (#14)
by Frédéric G.
01:17
created

TaxonomyIndex::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
dl 0
loc 8
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\qa\Plugin\QaCheck\References;
6
7
use Drupal\Core\Database\Connection;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Database\Connection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\qa\Pass;
9
use Drupal\qa\Plugin\QaCheckBase;
10
use Drupal\qa\Plugin\QaCheckInterface;
11
use Drupal\qa\Result;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...tion\ContainerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
/**
15
 * TaxonomyIndex checks for broken taxonomy_index references.
16
 *
17
 * It is not a generic reference integrity check, just a consistency chec for
18
 * the taxonomy_index table.
19
 *
20
 * @QaCheck(
21
 *   id = "references.taxonomy_index",
22
 *   label = @Translation("Taxonomy index"),
23
 *   details = @Translation("This check finds references in the taxonomy_index. These have to be repaired, as they can cause incorrect content displays."),
24
 *   usesBatch = false,
25
 *   steps = 1,
26
 * )
27
 */
28
class TaxonomyIndex extends QaCheckBase implements QaCheckInterface {
29
30
  const NAME = "references." . self::TABLE;
31
32
  const TABLE = 'taxonomy_index';
33
34
  const KEY_NODES = 'missingNodes';
35
  const KEY_TERMS = 'missingTerms';
36
37
  /**
38
   * The database service.
39
   *
40
   * @var \Drupal\Core\Database\Connection
41
   */
42
  protected $db;
43
44
  /**
45
   * ContentOrphans constructor.
46
   *
47
   * @param array $configuration
48
   *   The plugin configuration.
49
   * @param string $id
50
   *   The plugin ID.
51
   * @param array $definition
52
   *   The plugin definition.
53
   * @param \Drupal\qa\Plugin\QaCheck\References\Connection $db
0 ignored issues
show
Bug introduced by
The type Drupal\qa\Plugin\QaCheck\References\Connection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
54
   *   The database service.
55
   */
56
  public function __construct(
57
    array $configuration,
58
    string $id,
59
    array $definition,
60
    Connection $db
61
  ) {
62
    parent::__construct($configuration, $id, $definition);
63
    $this->db = $db;
64
  }
65
66
  /**
67
   * {@inheritdoc}
68
   */
69
  public static function create(
70
    ContainerInterface $container,
71
    array $configuration,
72
    $id,
73
    $definition
74
  ) {
75
    $db = $container->get('database');
76
    return new static($configuration, $id, $definition, $db);
77
  }
78
79
  /**
80
   * Locate {taxonomy_index} entries linking to a missing term or node.
81
   *
82
   * @return \Drupal\qa\Result
83
   */
84
  function checkIndex(): Result {
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...
85
    $sql = <<<SQL
86
SELECT ti.tid AS indexTid,ti.nid AS indexNid,
87
    nfd.nid,
88
    tfd.tid
89
FROM {taxonomy_index} ti
90
  LEFT JOIN {taxonomy_term_field_data} tfd ON ti.tid = tfd.tid
91
  LEFT JOIN {node_field_data} nfd ON ti.nid = nfd.nid
92
WHERE tfd.tid IS NULL
93
  OR nfd.nid IS NULL
94
SQL;
95
    // No node access: we are scanning the whole database for a fully privileged user.
96
    $q = $this->db->query($sql);
97
    $missing = [
98
      self::KEY_TERMS => [],
99
      self::KEY_NODES => [],
100
    ];
101
    foreach ($q->fetchAll() as $o) {
102
      if (is_null($o->tid)) {
103
        $missing[self::KEY_TERMS][] = [
104
          'nid' => (int) $o->indexNid,
105
          'missingTid' => (int) $o->indexTid,
106
        ];
107
      }
108
      if (is_null($o->nid)) {
109
        $missing[self::KEY_NODES][] = [
110
          'tid' => (int) $o->indexTid,
111
          'missingNid' => (int) $o->indexNid,
112
        ];
113
      }
114
    }
115
    sort($missing[self::KEY_TERMS]);
116
    sort($missing[self::KEY_NODES]);
117
    if (empty($missing[self::KEY_TERMS])) {
118
      unset($missing[self::KEY_TERMS]);
119
    }
120
    if (empty($missing[self::KEY_NODES])) {
121
      unset($missing[self::KEY_NODES]);
122
    }
123
    return new Result(self::TABLE, empty($missing), $missing);
124
  }
125
126
  /**
127
   * {@inheritdoc}
128
   */
129
  public function run(): Pass {
130
    $pass = parent::run();
131
    $pass->record($this->checkIndex());
132
    $pass->life->end();
133
    return $pass;
134
  }
135
136
}
137
138