Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Rules/Drupal/LoadIncludes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace mglaman\PHPStanDrupal\Rules\Drupal;

use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
use Drupal\Core\Extension\ModuleHandlerInterface;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
Expand Down Expand Up @@ -72,6 +73,12 @@ public function processNode(Node $node, Scope $scope): array
// include must not crash the analysis, but PHPStan's own
// exceptions elsewhere must propagate.
require_once $file;
} catch (ContainerNotInitializedException $e) {
// Top-level code in the include needs a container, which does
// not exist during analysis. Its top-level functions are still
// declared. Reporting this would also be unreliable: PHP won't
// re-run the file, so only the first call in a process throws.
return [];
} catch (Throwable $e) {
return [
RuleErrorBuilder::message(sprintf(
Expand Down
7 changes: 7 additions & 0 deletions src/Rules/Drupal/ModuleLoadInclude.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace mglaman\PHPStanDrupal\Rules\Drupal;

use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
use PhpParser\Node;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
Expand Down Expand Up @@ -69,6 +70,12 @@ public function processNode(Node $node, Scope $scope): array
// include must not crash the analysis, but PHPStan's own
// exceptions elsewhere must propagate.
require_once $file;
} catch (ContainerNotInitializedException $e) {
// Top-level code in the include needs a container, which does
// not exist during analysis. Its top-level functions are still
// declared. Reporting this would also be unreliable: PHP won't
// re-run the file, so only the first call in a process throws.
return [];
} catch (Throwable $e) {
return [
RuleErrorBuilder::message('A file could not be loaded from module_load_include')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/**
* @file
* Calls the container at the top level, like core's entity_test.install.
*/

declare(strict_types=1);

\Drupal::moduleHandler();
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/**
* @file
* Calls the container at the top level, like core's entity_test.install.
*/

declare(strict_types=1);

\Drupal::moduleHandler();
5 changes: 5 additions & 0 deletions tests/src/Rules/LoadIncludesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public static function cases(): \Generator
[]
];

yield 'bug-1062-load-include.php' => [
[__DIR__.'/data/bug-1062-load-include.php'],
[]
];

yield 'bug-587.php' => [
[__DIR__.'/data/bug-587.php'],
[
Expand Down
5 changes: 5 additions & 0 deletions tests/src/Rules/ModuleLoadIncludeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public function testRule(): void
]);
}

public function testContainerNotInitialized(): void
{
$this->analyse([__DIR__ . '/data/bug-1062-module-load-include.php'], []);
}

}
7 changes: 7 additions & 0 deletions tests/src/Rules/data/bug-1062-load-include.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Bug1062LoadInclude;

function test(): void {
\Drupal::moduleHandler()->loadInclude('contained_not_initialized', 'inc', 'contained_not_initialized.load_include');
}
7 changes: 7 additions & 0 deletions tests/src/Rules/data/bug-1062-module-load-include.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Bug1062ModuleLoadInclude;

function test(): void {
module_load_include('inc', 'contained_not_initialized', 'contained_not_initialized.module_load_include');
}
Loading