From 414a656ab9435acc6256df22a44a7ff8fa133119 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 25 Apr 2021 11:23:15 +0200 Subject: [PATCH] Fix AUTOLOAD_PATHS and BOOTSTRAP_FILES in tests (#6239) --- .../PHPUnit/AbstractRectorTestCase.php | 10 +++++++ .../Switch_/ChangeSwitchToMatchRector.php | 5 ++-- src/Autoloading/AdditionalAutoloader.php | 26 +++++++++---------- src/Console/Command/ProcessCommand.php | 4 ++- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/Testing/PHPUnit/AbstractRectorTestCase.php b/packages/Testing/PHPUnit/AbstractRectorTestCase.php index 57fbd87180c..31441157671 100644 --- a/packages/Testing/PHPUnit/AbstractRectorTestCase.php +++ b/packages/Testing/PHPUnit/AbstractRectorTestCase.php @@ -11,6 +11,8 @@ use PHPUnit\Framework\ExpectationFailedException; use Psr\Container\ContainerInterface; use Rector\Core\Application\ApplicationFileProcessor; use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector; +use Rector\Core\Autoloading\AdditionalAutoloader; +use Rector\Core\Autoloading\BootstrapFilesIncluder; use Rector\Core\Bootstrap\RectorConfigsResolver; use Rector\Core\Configuration\Configuration; use Rector\Core\Configuration\Option; @@ -76,6 +78,14 @@ abstract class AbstractRectorTestCase extends AbstractTestCase implements Rector $this->removedAndAddedFilesCollector = $this->getService(RemovedAndAddedFilesCollector::class); $this->removedAndAddedFilesCollector->reset(); + /** @var AdditionalAutoloader $additionalAutoloader */ + $additionalAutoloader = $this->getService(AdditionalAutoloader::class); + $additionalAutoloader->autoloadPaths(); + + /** @var BootstrapFilesIncluder $bootstrapFilesIncluder */ + $bootstrapFilesIncluder = $this->getService(BootstrapFilesIncluder::class); + $bootstrapFilesIncluder->includeBootstrapFiles(); + /** @var Configuration $configuration */ $configuration = $this->getService(Configuration::class); $configuration->setIsDryRun(true); diff --git a/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php b/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php index 1603ba0caf5..659191d37cf 100644 --- a/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php +++ b/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php @@ -9,7 +9,6 @@ use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Match_; use PhpParser\Node\MatchArm; -use PhpParser\Node\Name; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; @@ -147,7 +146,9 @@ CODE_SAMPLE private function changeToAssign(Switch_ $switch, Match_ $match, Expr $assignExpr): Assign { - $prevInitializedAssign = $this->betterNodeFinder->findFirstPreviousOfNode($switch, function (Node $node) use ($assignExpr): bool { + $prevInitializedAssign = $this->betterNodeFinder->findFirstPreviousOfNode($switch, function (Node $node) use ( + $assignExpr + ): bool { return $node instanceof Assign && $this->nodeComparator->areNodesEqual($node->var, $assignExpr); }); diff --git a/src/Autoloading/AdditionalAutoloader.php b/src/Autoloading/AdditionalAutoloader.php index ad69cc411c0..21819b9d38b 100644 --- a/src/Autoloading/AdditionalAutoloader.php +++ b/src/Autoloading/AdditionalAutoloader.php @@ -40,22 +40,12 @@ final class AdditionalAutoloader $this->dynamicSourceLocatorDecorator = $dynamicSourceLocatorDecorator; } - public function autoloadWithInputAndSource(InputInterface $input): void + public function autoloadInput(InputInterface $input): void { - if ($input->hasOption(Option::OPTION_AUTOLOAD_FILE)) { - $this->autoloadInputAutoloadFile($input); - } - - $autoloadPaths = $this->parameterProvider->provideArrayParameter(Option::AUTOLOAD_PATHS); - if ($autoloadPaths === []) { + if (! $input->hasOption(Option::OPTION_AUTOLOAD_FILE)) { return; } - $this->dynamicSourceLocatorDecorator->addPaths($autoloadPaths); - } - - private function autoloadInputAutoloadFile(InputInterface $input): void - { /** @var string|null $autoloadFile */ $autoloadFile = $input->getOption(Option::OPTION_AUTOLOAD_FILE); if ($autoloadFile === null) { @@ -63,6 +53,16 @@ final class AdditionalAutoloader } $this->fileSystemGuard->ensureFileExists($autoloadFile, 'Extra autoload'); - $this->dynamicSourceLocatorDecorator->addPaths([$autoloadFile]); + require_once $autoloadFile; + } + + public function autoloadPaths(): void + { + $autoloadPaths = $this->parameterProvider->provideArrayParameter(Option::AUTOLOAD_PATHS); + if ($autoloadPaths === []) { + return; + } + + $this->dynamicSourceLocatorDecorator->addPaths($autoloadPaths); } } diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index 23d935fa779..011771a32b0 100644 --- a/src/Console/Command/ProcessCommand.php +++ b/src/Console/Command/ProcessCommand.php @@ -202,7 +202,9 @@ final class ProcessCommand extends Command // register autoloaded and included files $this->bootstrapFilesIncluder->includeBootstrapFiles(); - $this->additionalAutoloader->autoloadWithInputAndSource($input); + + $this->additionalAutoloader->autoloadInput($input); + $this->additionalAutoloader->autoloadPaths(); // PHPStan has to know about all files! $this->configurePHPStanNodeScopeResolver($phpFileInfos);