Skip to content

Commit 752507b

Browse files
committed
Revert back to a bootstrap file.
Reverts work done in #7 and fixes #8 caching problems.
1 parent a9506d3 commit 752507b

File tree

4 files changed

+237
-179
lines changed

4 files changed

+237
-179
lines changed

extension.neon

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
parameters:
22
excludes_analyse:
33
- *.api.php
4+
bootstrap: %rootDir%/../../mglaman/phpstan-drupal/phpstan-bootstrap.php
45
fileExtensions:
5-
- php
66
- module
77
- theme
88
- inc
99
extensions:
10-
rulesOverride: PHPStan\DependencyInjection\RulesOverrideExtension
1110
drupal: PHPStan\DependencyInjection\DrupalExtension
1211
rules:
1312
- PHPStan\Rules\Classes\PluginManagerInspectionRule

src/DependencyInjection/DrupalExtension.php

Lines changed: 9 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
namespace PHPStan\DependencyInjection;
44

5-
use Composer\Autoload\ClassLoader;
65
use Nette\DI\CompilerExtension;
76
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;
119

1210
class DrupalExtension extends CompilerExtension
1311
{
@@ -24,11 +22,6 @@ class DrupalExtension extends CompilerExtension
2422
*/
2523
private $autoloaderPath;
2624

27-
/**
28-
* @var \Composer\Autoload\ClassLoader
29-
*/
30-
private $autoloader;
31-
3225
/**
3326
* @var string
3427
*/
@@ -58,20 +51,10 @@ class DrupalExtension extends CompilerExtension
5851
*/
5952
private $themes = [];
6053

61-
/**
62-
* @var \PHPStan\Drupal\ExtensionDiscovery
63-
*/
64-
private $extensionDiscovery;
65-
6654
public function loadConfiguration(): void
6755
{
6856

6957
$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-
}
7558
$realpath = realpath($this->autoloaderPath);
7659
if ($realpath === false) {
7760
throw new \InvalidArgumentException('Cannot determine the realpath of the autoloader.');
@@ -97,143 +80,15 @@ public function loadConfiguration(): void
9780
$this->modules = $config['modules'] ?? [];
9881
$this->themes = $config['themes'] ?? [];
9982

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;
20588
}
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);
22691
}
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.
23792
}
23893
}
23994
}

src/DependencyInjection/RulesOverrideExtension.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)