Skip to content

Commit

Permalink
[Config] Move from magic loading of all PHPStan extensions, their boo…
Browse files Browse the repository at this point in the history
…tstrap files etc. under control with explicit file listing (#4769)
  • Loading branch information
TomasVotruba committed Aug 11, 2023
1 parent 645190a commit 28a64fd
Show file tree
Hide file tree
Showing 9 changed files with 1 addition and 205 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"doctrine/inflector": "^2.0.6",
"fidry/cpu-core-counter": "^0.5.1",
"illuminate/container": "^10.13",
"nette/neon": "^3.4",
"nette/utils": "^3.2",
"nikic/php-parser": "^4.16.0",
"ondram/ci-detector": "^4.1",
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ final class PHPStanServicesFactory
private readonly Container $container;

public function __construct(
private readonly PHPStanExtensionsConfigResolver $phpStanExtensionsConfigResolver,
BleedingEdgeIncludePurifier $bleedingEdgeIncludePurifier,
) {
$additionalConfigFiles = $this->resolveAdditionalConfigFiles();
Expand Down Expand Up @@ -59,11 +58,6 @@ public function __construct(
$filesystem->remove($purifiedConfigFiles);
}

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

/**
* @api
*/
Expand Down Expand Up @@ -154,9 +148,6 @@ private function resolveAdditionalConfigFiles(): array
$additionalConfigFiles[] = __DIR__ . '/../../../config/phpstan/better-infer.neon';
$additionalConfigFiles[] = __DIR__ . '/../../../config/phpstan/parser.neon';

$extensionConfigFiles = $this->phpStanExtensionsConfigResolver->resolve();
$additionalConfigFiles = array_merge($additionalConfigFiles, $extensionConfigFiles);

return array_filter($additionalConfigFiles, 'file_exists');
}
}
1 change: 0 additions & 1 deletion packages/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ protected function setUp(): void
/** @var BootstrapFilesIncluder $bootstrapFilesIncluder */
$bootstrapFilesIncluder = $this->getService(BootstrapFilesIncluder::class);
$bootstrapFilesIncluder->includeBootstrapFiles();
$bootstrapFilesIncluder->includePHPStanExtensionsBoostrapFiles();

self::$cacheByRuleAndConfig[$cacheKey] = true;
}
Expand Down
9 changes: 0 additions & 9 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ parameters:
paths:
# autoload check in bin file
- bin/rector.php
- packages/NodeTypeResolver/DependencyInjection/PHPStanExtensionsConfigResolver.php
# for config class reflection
- src/Bootstrap/ExtensionConfigResolver.php
- config/config.php
Expand Down Expand Up @@ -371,7 +370,6 @@ parameters:
paths:
- bin/rector.php
- src/Bootstrap/RectorConfigsResolver.php
- src/NonPhpFile/NonPhpFileProcessor.php
- tests/FileSystem/FilesFinder/FilesFinderTest.php

# false postive, on trait use in multiple classes
Expand Down Expand Up @@ -454,7 +452,6 @@ parameters:
message: '#Offset (.*?)includes(.*?) always exists and is not nullable#'
paths:
- src/Bootstrap/ExtensionConfigResolver.php
- packages/NodeTypeResolver/DependencyInjection/PHPStanExtensionsConfigResolver.php

# returns bool for notifications
- '#Method "renamePropertyPromotion\(\)" returns bool type, so the name should start with is/has/was#'
Expand Down Expand Up @@ -668,9 +665,6 @@ parameters:
# iterable mix
- '#Property Rector\\Core\\Configuration\\ConfigInitializer\:\:\$rectors \(array<Rector\\Core\\Contract\\Rector\\RectorInterface>\) does not accept array\|iterable<Rector\\Core\\Contract\\Rector\\RectorInterface>#'

# false positive
- '#Parameter \#2 \$colors of method Tracy\\Dumper\\Renderer\:\:renderAsText\(\) expects array, array\|null given#'

# chicken/egg
-
message: '#Function "(d|dd)\(\)" cannot be used/left in the code#'
Expand All @@ -685,9 +679,6 @@ parameters:
message: '#Do not name "d", shorter than 2 chars#'
path: tests/debug_functions.php

# not relevant
- '#Do not use static property#'

# deprecated
-
message: '#Fetching class constant class of deprecated class Rector\\Core\\Contract\\Rector\\NonPhpRectorInterface#'
Expand Down
85 changes: 1 addition & 84 deletions src/Autoloading/BootstrapFilesIncluder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,19 @@

namespace Rector\Core\Autoloading;

use Nette\Neon\Neon;
use PHPStan\DependencyInjection\Container;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\DependencyInjection\PHPStanExtensionsConfigResolver;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
use Throwable;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Core\Tests\Autoloading\BootstrapFilesIncluderTest
*/
final class BootstrapFilesIncluder
{
/**
* @var array<string, mixed>
*/
private array $configCache = [];

public function __construct(
private readonly PHPStanExtensionsConfigResolver $phpStanExtensionsConfigResolver,
) {
}

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

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

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

/**
* Inspired by
* @see https://github.com/phpstan/phpstan-src/commit/aad1bf888ab7b5808898ee5fe2228bb8bb4e4cf1
Expand All @@ -58,70 +33,12 @@ public function includeBootstrapFiles(): void
throw new ShouldNotHappenException(sprintf('Bootstrap file "%s" does not exist.', $bootstrapFile));
}

$this->tryRequireFile($bootstrapFile);
require $bootstrapFile;
}

$this->requireRectorStubs();
}

/**
* @param string[] $extensionConfigFiles
* @return string[]
*/
private function resolveAbsoluteBootstrapFilePaths(array $extensionConfigFiles): array
{
$absoluteBootstrapFilePaths = [];

foreach ($extensionConfigFiles as $extensionConfigFile) {
if (! array_key_exists($extensionConfigFile, $this->configCache)) {
$extensionConfigContents = Neon::decodeFile($extensionConfigFile);

$this->configCache[$extensionConfigFile] = $extensionConfigContents;
} else {
$extensionConfigContents = $this->configCache[$extensionConfigFile];
}

$configDirectory = dirname($extensionConfigFile);

$bootstrapFiles = $extensionConfigContents['parameters']['bootstrapFiles'] ?? [];

foreach ($bootstrapFiles as $bootstrapFile) {
$absoluteBootstrapFilePath = realpath($configDirectory . '/' . $bootstrapFile);
if (! is_string($absoluteBootstrapFilePath)) {
continue;
}

$absoluteBootstrapFilePaths[] = $absoluteBootstrapFilePath;
}
}

return $absoluteBootstrapFilePaths;
}

/**
* 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 {
(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',
$throwable::class,
$throwable->getFile(),
$throwable->getLine(),
$bootstrapFile,
$throwable->getMessage()
);

throw new ShouldNotHappenException($errorMessage, $throwable->getCode(), $throwable);
}
}

private function requireRectorStubs(): void
{
$stubsRectorDirectory = realpath(__DIR__ . '/../../stubs-rector');
Expand Down
7 changes: 0 additions & 7 deletions src/DependencyInjection/RectorContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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 @@ -29,12 +28,6 @@ public function createFromBootstrapConfigs(BootstrapConfigs $bootstrapConfigs):
$bootstrapFilesIncluder = $container->get(BootstrapFilesIncluder::class);
$bootstrapFilesIncluder->includeBootstrapFiles();

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

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

return $container;
}

Expand Down
2 changes: 0 additions & 2 deletions tests/Autoloading/BootstrapFilesIncluderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ final class BootstrapFilesIncluderTest extends AbstractLazyTestCase
public function test(): void
{
$bootstrapFilesIncluder = $this->make(BootstrapFilesIncluder::class);

$bootstrapFilesIncluder->includeBootstrapFiles();
$bootstrapFilesIncluder->includePHPStanExtensionsBoostrapFiles();
}
}

0 comments on commit 28a64fd

Please sign in to comment.