2
2
3
3
namespace PHPStan \DependencyInjection ;
4
4
5
- use Composer \Autoload \ClassLoader ;
6
5
use Nette \DI \CompilerExtension ;
7
6
use Nette \DI \Config \Helpers ;
8
- use Nette \Utils \Finder ;
9
- use PHPStan \Drupal \Extension ;
10
- use PHPStan \Drupal \ExtensionDiscovery ;
7
+ use PHPStan \Rules \Classes \EnhancedRequireParentConstructCallRule ;
8
+ use PHPStan \Rules \Classes \RequireParentConstructCallRule ;
11
9
12
10
class DrupalExtension extends CompilerExtension
13
11
{
@@ -24,11 +22,6 @@ class DrupalExtension extends CompilerExtension
24
22
*/
25
23
private $ autoloaderPath ;
26
24
27
- /**
28
- * @var \Composer\Autoload\ClassLoader
29
- */
30
- private $ autoloader ;
31
-
32
25
/**
33
26
* @var string
34
27
*/
@@ -58,20 +51,10 @@ class DrupalExtension extends CompilerExtension
58
51
*/
59
52
private $ themes = [];
60
53
61
- /**
62
- * @var \PHPStan\Drupal\ExtensionDiscovery
63
- */
64
- private $ extensionDiscovery ;
65
-
66
54
public function loadConfiguration (): void
67
55
{
68
56
69
57
$ this ->autoloaderPath = $ GLOBALS ['autoloaderInWorkingDirectory ' ];
70
- /** @noinspection PhpIncludeInspection */
71
- $ this ->autoloader = require $ this ->autoloaderPath ;
72
- if (!$ this ->autoloader instanceof ClassLoader) {
73
- throw new \InvalidArgumentException ('Unable to determine the Composer class loader for Drupal ' );
74
- }
75
58
$ realpath = realpath ($ this ->autoloaderPath );
76
59
if ($ realpath === false ) {
77
60
throw new \InvalidArgumentException ('Cannot determine the realpath of the autoloader. ' );
@@ -97,143 +80,15 @@ public function loadConfiguration(): void
97
80
$ this ->modules = $ config ['modules ' ] ?? [];
98
81
$ this ->themes = $ config ['themes ' ] ?? [];
99
82
100
- $ this ->extensionDiscovery = new ExtensionDiscovery ($ this ->drupalRoot );
101
- $ this ->extensionDiscovery ->setProfileDirectories ([]);
102
- $ profiles = $ this ->extensionDiscovery ->scan ('profile ' );
103
- $ profile_directories = array_map (function ($ profile ) {
104
- return $ profile ->getPath ();
105
- }, $ profiles );
106
- $ this ->extensionDiscovery ->setProfileDirectories ($ profile_directories );
107
-
108
- $ this ->moduleData = $ this ->extensionDiscovery ->scan ('module ' );
109
- $ this ->themeData = $ this ->extensionDiscovery ->scan ('theme ' );
110
-
111
- $ this ->loadLegacyIncludes ();
112
-
113
- $ this ->addCoreNamespaces ();
114
- $ this ->addModuleNamespaces ();
115
- $ this ->addThemeNamespaces ();
116
-
117
- foreach ($ this ->moduleData as $ extension ) {
118
- $ this ->loadExtension ($ extension );
119
-
120
- $ module_name = $ extension ->getName ();
121
- $ module_dir = $ this ->drupalRoot . '/ ' . $ extension ->getPath ();
122
- // Add .post_update.php
123
- if (file_exists ($ module_dir . '/ ' . $ module_name . '.post_update.php ' )) {
124
- require $ module_dir . '/ ' . $ module_name . '.post_update.php ' ;
125
- }
126
- // Add misc .inc that are magically allowed via hook_hook_info.
127
- $ magic_hook_info_includes = [
128
- 'views ' ,
129
- 'views_execution ' ,
130
- 'tokens ' ,
131
- 'search_api ' ,
132
- 'pathauto ' ,
133
- ];
134
- foreach ($ magic_hook_info_includes as $ hook_info_include ) {
135
- if (file_exists ($ module_dir . "/ $ module_name. $ hook_info_include.inc " )) {
136
- require $ module_dir . "/ $ module_name. $ hook_info_include.inc " ;
137
- }
138
- }
139
- }
140
- foreach ($ this ->themeData as $ extension ) {
141
- $ this ->loadExtension ($ extension );
142
- }
143
- }
144
-
145
- protected function loadLegacyIncludes (): void
146
- {
147
- /** @var \SplFileInfo $file */
148
- foreach (Finder::findFiles ('*.inc ' )->in ($ this ->drupalRoot . '/core/includes ' ) as $ file ) {
149
- require $ file ->getPathname ();
150
- }
151
- }
152
-
153
- protected function addCoreNamespaces (): void
154
- {
155
- $ namespaces = [];
156
- foreach (['Core ' , 'Component ' ] as $ parent_directory ) {
157
- $ path = $ this ->drupalRoot . '/core/lib/Drupal/ ' . $ parent_directory ;
158
- $ parent_namespace = 'Drupal \\' . $ parent_directory ;
159
- foreach (new \DirectoryIterator ($ path ) as $ component ) {
160
- $ pathname = $ component ->getPathname ();
161
- if (!$ component ->isDot () && $ component ->isDir () && (
162
- is_dir ($ pathname . '/Plugin ' ) ||
163
- is_dir ($ pathname . '/Entity ' ) ||
164
- is_dir ($ pathname . '/Element ' )
165
- )) {
166
- $ namespaces [$ parent_namespace . '\\' . $ component ->getFilename ()] = $ path . '/ ' . $ component ->getFilename ();
167
- }
168
- }
169
- }
170
- $ this ->registerPs4Namespaces ($ namespaces );
171
-
172
- // Add core test namespaces.
173
- $ core_tests_dir = $ this ->drupalRoot . '/core/tests ' ;
174
- $ this ->autoloader ->add ('Drupal \\Tests ' , $ core_tests_dir );
175
- $ this ->autoloader ->add ('Drupal \\TestSite ' , $ core_tests_dir );
176
- $ this ->autoloader ->add ('Drupal \\KernelTests ' , $ core_tests_dir );
177
- $ this ->autoloader ->add ('Drupal \\FunctionalTests ' , $ core_tests_dir );
178
- $ this ->autoloader ->add ('Drupal \\FunctionalJavascriptTests ' , $ core_tests_dir );
179
- }
180
- protected function addModuleNamespaces (): void
181
- {
182
- $ namespaces = [];
183
- foreach ($ this ->moduleData as $ module_name => $ module ) {
184
- $ module_dir = $ this ->drupalRoot . '/ ' . $ module ->getPath ();
185
- $ namespaces ["Drupal \\$ module_name " ] = $ module_dir . '/src ' ;
186
-
187
- // @see drupal_phpunit_get_extension_namespaces
188
- $ module_test_dir = $ module_dir . '/tests/src ' ;
189
- if (is_dir ($ module_test_dir )) {
190
- $ suite_names = ['Unit ' , 'Kernel ' , 'Functional ' , 'FunctionalJavascript ' ];
191
- foreach ($ suite_names as $ suite_name ) {
192
- $ suite_dir = $ module_test_dir . '/ ' . $ suite_name ;
193
- if (is_dir ($ suite_dir )) {
194
- // Register the PSR-4 directory for PHPUnit-based suites.
195
- $ namespaces ["Drupal \\Tests \\$ module_name \\$ suite_name " ] = $ suite_dir ;
196
- }
197
-
198
- // Extensions can have a \Drupal\extension\Traits namespace for
199
- // cross-suite trait code.
200
- $ trait_dir = $ module_test_dir . '/Traits ' ;
201
- if (is_dir ($ trait_dir )) {
202
- $ namespaces ["Drupal \\Tests \\$ module_name \\Traits " ] = $ trait_dir ;
203
- }
204
- }
83
+ $ builder = $ this ->getContainerBuilder ();
84
+ foreach ($ builder ->getDefinitions () as $ definition ) {
85
+ $ factory = $ definition ->getFactory ();
86
+ if ($ factory === null ) {
87
+ continue ;
205
88
}
206
- }
207
- $ this ->registerPs4Namespaces ($ namespaces );
208
- }
209
- protected function addThemeNamespaces (): void
210
- {
211
- $ namespaces = [];
212
- foreach ($ this ->themeData as $ theme_name => $ theme ) {
213
- $ theme_dir = $ this ->drupalRoot . '/ ' . $ theme ->getPath ();
214
- $ namespaces ["Drupal \\$ theme_name " ] = $ theme_dir . '/src ' ;
215
- }
216
- $ this ->registerPs4Namespaces ($ namespaces );
217
- }
218
-
219
- protected function registerPs4Namespaces (array $ namespaces ): void
220
- {
221
- foreach ($ namespaces as $ prefix => $ paths ) {
222
- if (is_array ($ paths )) {
223
- foreach ($ paths as $ key => $ value ) {
224
- $ paths [$ key ] = $ value ;
225
- }
89
+ if ($ factory ->entity === RequireParentConstructCallRule::class) {
90
+ $ definition ->setFactory (EnhancedRequireParentConstructCallRule::class);
226
91
}
227
- $ this ->autoloader ->addPsr4 ($ prefix . '\\' , $ paths );
228
- }
229
- }
230
- protected function loadExtension (Extension $ extension ): void
231
- {
232
- try {
233
- $ extension ->load ();
234
- } catch (\Throwable $ e ) {
235
- // Something prevented the extension file from loading.
236
- // This can happen when drupal_get_path or drupal_get_filename are used outside of the scope of a function.
237
92
}
238
93
}
239
94
}
0 commit comments