Skip to content

Commit

Permalink
[phpstan-extensions] Provide phpstan container to bootstrap file from…
Browse files Browse the repository at this point in the history
… extensions (#3401)

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
TomasVotruba and samsonasik committed Feb 23, 2023
1 parent b1b48bc commit 4a583b6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public function __construct(
$filesystem->remove($purifiedConfigFiles);
}

public function provideContainer(): Container
{
return $this->container;
}

/**
* @api
*/
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -812,3 +812,11 @@ parameters:
- '#Call to deprecated method getDirectClassNames\(\) of class PHPStan\\Type\\TypeUtils.*#'
- '#Parameter 3 should use "PHPStan\\Reflection\\ParameterReflectionWithPhpDocs" type as the only type passed to this method#'
- '#Parameters should use "PHPStan\\Reflection\\ParameterReflectionWithPhpDocs\|PhpParser\\Node\\Param" types as the only types passed to this method#'

# various usage
- '#Parameters should use "PHPStan\\DependencyInjection\\Container" types as the only types passed to this method#'

# actually used in global scope
-
message: '#Anonymous function has an unused use \$container#'
path: src/Autoloading/BootstrapFilesIncluder.php
15 changes: 11 additions & 4 deletions src/Autoloading/BootstrapFilesIncluder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Core\Autoloading;

use Nette\Neon\Neon;
use PHPStan\DependencyInjection\Container;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\ParameterProvider;
use Rector\Core\Exception\ShouldNotHappenException;
Expand All @@ -26,14 +27,14 @@ public function __construct(
) {
}

public function includePHPStanExtensionsBoostrapFiles(): void
public function includePHPStanExtensionsBoostrapFiles(?Container $container = null): void
{
$extensionConfigFiles = $this->phpStanExtensionsConfigResolver->resolve();

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

foreach ($absoluteBootstrapFilePaths as $absoluteBootstrapFilePath) {
$this->tryRequireFile($absoluteBootstrapFilePath);
$this->tryRequireFile($absoluteBootstrapFilePath, $container);
}
}

Expand Down Expand Up @@ -87,10 +88,16 @@ private function resolveAbsoluteBootstrapFilePaths(array $extensionConfigFiles):
return $absoluteBootstrapFilePaths;
}

private function tryRequireFile(string $bootstrapFile): void
/**
* PHPStan container mimics:
* https://github.com/phpstan/phpstan-src/blob/34881e682e36e30917dcfa8dc69c70e857143436/src/Command/CommandHelper.php#L513-L515
*/
private function tryRequireFile(string $bootstrapFile, ?Container $container = null): void
{
try {
require_once $bootstrapFile;
(static function (string $bootstrapFile) use ($container): void {
require_once $bootstrapFile;
})($bootstrapFile);
} catch (Throwable $throwable) {
$errorMessage = sprintf(
'"%s" thrown in "%s" on line %d while loading bootstrap file %s: %s',
Expand Down
8 changes: 7 additions & 1 deletion src/DependencyInjection/RectorContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Rector\Core\Autoloading\BootstrapFilesIncluder;
use Rector\Core\Kernel\RectorKernel;
use Rector\Core\ValueObject\Bootstrap\BootstrapConfigs;
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;

final class RectorContainerFactory
{
Expand All @@ -27,7 +28,12 @@ public function createFromBootstrapConfigs(BootstrapConfigs $bootstrapConfigs):
/** @var BootstrapFilesIncluder $bootstrapFilesIncluder */
$bootstrapFilesIncluder = $container->get(BootstrapFilesIncluder::class);
$bootstrapFilesIncluder->includeBootstrapFiles();
$bootstrapFilesIncluder->includePHPStanExtensionsBoostrapFiles();

$phpStanServicesFactory = $container->get(PHPStanServicesFactory::class);

/** @var PHPStanServicesFactory $phpStanServicesFactory */
$phpStanContainer = $phpStanServicesFactory->provideContainer();
$bootstrapFilesIncluder->includePHPStanExtensionsBoostrapFiles($phpStanContainer);

return $container;
}
Expand Down

0 comments on commit 4a583b6

Please sign in to comment.