Skip to content

Commit 4a583b6

Browse files
[phpstan-extensions] Provide phpstan container to bootstrap file from extensions (rectorphp#3401)
Co-authored-by: Abdul Malik Ikhsan <[email protected]>
1 parent b1b48bc commit 4a583b6

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

packages/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php

+5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public function __construct(
5757
$filesystem->remove($purifiedConfigFiles);
5858
}
5959

60+
public function provideContainer(): Container
61+
{
62+
return $this->container;
63+
}
64+
6065
/**
6166
* @api
6267
*/

phpstan.neon

+8
Original file line numberDiff line numberDiff line change
@@ -812,3 +812,11 @@ parameters:
812812
- '#Call to deprecated method getDirectClassNames\(\) of class PHPStan\\Type\\TypeUtils.*#'
813813
- '#Parameter 3 should use "PHPStan\\Reflection\\ParameterReflectionWithPhpDocs" type as the only type passed to this method#'
814814
- '#Parameters should use "PHPStan\\Reflection\\ParameterReflectionWithPhpDocs\|PhpParser\\Node\\Param" types as the only types passed to this method#'
815+
816+
# various usage
817+
- '#Parameters should use "PHPStan\\DependencyInjection\\Container" types as the only types passed to this method#'
818+
819+
# actually used in global scope
820+
-
821+
message: '#Anonymous function has an unused use \$container#'
822+
path: src/Autoloading/BootstrapFilesIncluder.php

src/Autoloading/BootstrapFilesIncluder.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\Core\Autoloading;
66

77
use Nette\Neon\Neon;
8+
use PHPStan\DependencyInjection\Container;
89
use Rector\Core\Configuration\Option;
910
use Rector\Core\Configuration\Parameter\ParameterProvider;
1011
use Rector\Core\Exception\ShouldNotHappenException;
@@ -26,14 +27,14 @@ public function __construct(
2627
) {
2728
}
2829

29-
public function includePHPStanExtensionsBoostrapFiles(): void
30+
public function includePHPStanExtensionsBoostrapFiles(?Container $container = null): void
3031
{
3132
$extensionConfigFiles = $this->phpStanExtensionsConfigResolver->resolve();
3233

3334
$absoluteBootstrapFilePaths = $this->resolveAbsoluteBootstrapFilePaths($extensionConfigFiles);
3435

3536
foreach ($absoluteBootstrapFilePaths as $absoluteBootstrapFilePath) {
36-
$this->tryRequireFile($absoluteBootstrapFilePath);
37+
$this->tryRequireFile($absoluteBootstrapFilePath, $container);
3738
}
3839
}
3940

@@ -87,10 +88,16 @@ private function resolveAbsoluteBootstrapFilePaths(array $extensionConfigFiles):
8788
return $absoluteBootstrapFilePaths;
8889
}
8990

90-
private function tryRequireFile(string $bootstrapFile): void
91+
/**
92+
* PHPStan container mimics:
93+
* https://github.com/phpstan/phpstan-src/blob/34881e682e36e30917dcfa8dc69c70e857143436/src/Command/CommandHelper.php#L513-L515
94+
*/
95+
private function tryRequireFile(string $bootstrapFile, ?Container $container = null): void
9196
{
9297
try {
93-
require_once $bootstrapFile;
98+
(static function (string $bootstrapFile) use ($container): void {
99+
require_once $bootstrapFile;
100+
})($bootstrapFile);
94101
} catch (Throwable $throwable) {
95102
$errorMessage = sprintf(
96103
'"%s" thrown in "%s" on line %d while loading bootstrap file %s: %s',

src/DependencyInjection/RectorContainerFactory.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Rector\Core\Autoloading\BootstrapFilesIncluder;
1010
use Rector\Core\Kernel\RectorKernel;
1111
use Rector\Core\ValueObject\Bootstrap\BootstrapConfigs;
12+
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;
1213

1314
final class RectorContainerFactory
1415
{
@@ -27,7 +28,12 @@ public function createFromBootstrapConfigs(BootstrapConfigs $bootstrapConfigs):
2728
/** @var BootstrapFilesIncluder $bootstrapFilesIncluder */
2829
$bootstrapFilesIncluder = $container->get(BootstrapFilesIncluder::class);
2930
$bootstrapFilesIncluder->includeBootstrapFiles();
30-
$bootstrapFilesIncluder->includePHPStanExtensionsBoostrapFiles();
31+
32+
$phpStanServicesFactory = $container->get(PHPStanServicesFactory::class);
33+
34+
/** @var PHPStanServicesFactory $phpStanServicesFactory */
35+
$phpStanContainer = $phpStanServicesFactory->provideContainer();
36+
$bootstrapFilesIncluder->includePHPStanExtensionsBoostrapFiles($phpStanContainer);
3137

3238
return $container;
3339
}

0 commit comments

Comments
 (0)