Module::getInfo()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 8
nop 0
dl 0
loc 30
rs 9.3222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Drupal\qa\System;
6
7
use Drupal\Core\Extension\Extension;
8
9
/**
10
 * Class Module represents information about a module.
11
 */
12
class Module {
13
14
  /**
15
   * The name of the module file.
16
   *
17
   * @var string
18
   */
19
  public $filename;
20
21
  /**
22
   * Is the module hidden ?
23
   *
24
   * @var bool
25
   */
26
  public $hidden = FALSE;
27
28
  /**
29
   * The name of the module.
30
   *
31
   * @var string
32
   */
33
  public $name;
34
35
  /**
36
   * Misc information about the module.
37
   *
38
   * @var array
39
   */
40
  public $info;
41
42
  /**
43
   * The module extension type: "module" or "profile".
44
   *
45
   * @var string
46
   */
47
  public $type;
48
49
  /**
50
   * Is the module enabled (1 for true) ?
51
   *
52
   * @var int
53
   */
54
  public $status;
55
56
  /**
57
   * The current database schema version for the module.
58
   *
59
   * @var string
60
   */
61
  // phpcs:ignore
62
  public $schema_version;
63
64
  /**
65
   * The module weight, used in hooks.
66
   *
67
   * @var int
68
   */
69
  public $weight = 0;
70
71
  /**
72
   * The extensions depending on this module.
73
   *
74
   * @var array
75
   */
76
  // phpcs:ignore
77
  public $required_by = [];
78
79
  /**
80
   * The extensions this module depends on.
81
   *
82
   * @var array
83
   */
84
  public $requires = [];
85
86
  /**
87
   * The module sort order, not to be confused with weight.
88
   *
89
   * @var int
90
   */
91
  public $sort;
92
93
  /**
94
   * Create a Module description from a core Extension object.
95
   *
96
   * @param \Drupal\Core\Extension\Extension $object
97
   *   The module information, as provided by core.
98
   *
99
   * @return \Drupal\qa\System\Module
100
   *   The module information, ready to use.
101
   */
102
  public static function createFromCore(Extension $object): Module {
103
    $o = new Module();
104
    $rc = new \ReflectionClass(Extension::class);
105
106
    /** @var \ReflectionProperty $p */
107
    foreach ($rc->getProperties() as $p) {
108
      $name = $p->getName();
109
      $p->setAccessible(TRUE);
110
      $value = $p->getValue($object);
111
      $o->{$name} = $value;
112
    }
113
114
    return $o;
115
  }
116
117
  /**
118
   * Extract information from the modules and their projects.
119
   *
120
   * FIXME very incomplete: relies on the D7 info, missing most D8/9 properties.
121
   *
122
   * @return array
123
   *   A list of modules and theme descriptions.
124
   */
125
  public static function getInfo() {
126
    $module_data = system_rebuild_module_data();
0 ignored issues
show
Bug introduced by
The function system_rebuild_module_data 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

126
    $module_data = /** @scrutinizer ignore-call */ system_rebuild_module_data();
Loading history...
127
    /** @var Module[] $modules */
128
    $modules = [];
129
130
    /** @var Project[] $projects */
131
    $projects = [];
132
133
    foreach ($module_data as $name => $info) {
134
      $module = Module::createFromCore($info);
135
      if ($module->isHidden()) {
136
        continue;
137
      }
138
      $modules[$name] = $module;
139
      $project_name = $module->getProject();
140
      if (!isset($projects[$project_name])) {
141
        $projects[$project_name] = new Project($project_name);
142
      }
143
      $project = $projects[$project_name];
144
      $project->addModule($module);
145
    }
146
147
    foreach ($projects as &$project) {
148
      $project->sort();
149
    }
150
    ksort($projects);
151
152
    return [
153
      $modules,
154
      $projects,
155
    ];
156
  }
157
158
  /**
159
   * Builtin.
160
   *
161
   * @return string
162
   *   The name of the module.
163
   */
164
  public function __toString() {
165
    return $this->name;
166
  }
167
168
  /**
169
   * Obtain a valid project name for any module.
170
   *
171
   * @return string
172
   *   The name of the project associated to that module.
173
   */
174
  public function getProject() {
175
    if (!isset($this->info['project'])) {
176
      if (isset($this->info['package']) && $this->info['package'] == 'Core') {
177
        $project = 'drupal';
178
      }
179
      else {
180
        $project = 'unknown';
181
      }
182
    }
183
    else {
184
      $project = $this->info['project'];
185
    }
186
187
    return $project;
188
  }
189
190
  /**
191
   * Is the module hidden ?
192
   *
193
   * @return bool
194
   *   Is it ?
195
   */
196
  public function isHidden() {
197
    return !empty($this->info['hidden']);
198
  }
199
200
  /**
201
   * Is the module installed ? (enabled is D7 wording).
202
   *
203
   * @return bool
204
   *   Is it ?
205
   */
206
  public function isEnabled() {
207
    return !empty($this->status);
208
  }
209
210
}
211