From 33cd52e703d44690b91106f4752b5786420fa2db Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 12 May 2022 11:05:03 +0200 Subject: [PATCH] Fix position of class after non-reachable stmts (#2299) * class cannot be under non-reachable stmts * static fixes in bin/rector.php --- bin/rector.php | 102 +++++++++++++++++++++++++++---------------------- phpstan.neon | 9 +++++ 2 files changed, 65 insertions(+), 46 deletions(-) diff --git a/bin/rector.php b/bin/rector.php index 0ce45cf7999..027eb6acdf5 100755 --- a/bin/rector.php +++ b/bin/rector.php @@ -29,54 +29,12 @@ $autoloadIncluder = new AutoloadIncluder(); $autoloadIncluder->includeDependencyOrRepositoryVendorAutoloadIfExists(); - -if (file_exists(__DIR__ . '/../preload.php') && is_dir(__DIR__ . '/../vendor')) { - require_once __DIR__ . '/../preload.php'; -} - -require_once __DIR__ . '/../src/constants.php'; - -$autoloadIncluder->loadIfExistsAndNotLoadedYet(__DIR__ . '/../vendor/scoper-autoload.php'); -$autoloadIncluder->autoloadProjectAutoloaderFile(); -$autoloadIncluder->autoloadRectorInstalledAsGlobalDependency(); -$autoloadIncluder->autoloadFromCommandLine(); - -$rectorConfigsResolver = new RectorConfigsResolver(); - -try { - $bootstrapConfigs = $rectorConfigsResolver->provide(); - $rectorContainerFactory = new RectorContainerFactory(); - $container = $rectorContainerFactory->createFromBootstrapConfigs($bootstrapConfigs); -} catch (Throwable $throwable) { - // for json output - $argvInput = new ArgvInput(); - $outputFormat = $argvInput->getParameterOption('--' . Option::OUTPUT_FORMAT); - - // report fatal error in json format - if ($outputFormat === JsonOutputFormatter::NAME) { - echo Json::encode([ - 'fatal_errors' => [$throwable->getMessage()], - ]); - } else { - // report fatal errors in console format - $rectorConsoleOutputStyleFactory = new RectorConsoleOutputStyleFactory(new PrivatesCaller()); - $rectorConsoleOutputStyle = $rectorConsoleOutputStyleFactory->create(); - $rectorConsoleOutputStyle->error($throwable->getMessage()); - } - - exit(Command::FAILURE); -} - -/** @var ConsoleApplication $application */ -$application = $container->get(ConsoleApplication::class); -exit($application->run()); - final class AutoloadIncluder { /** * @var string[] */ - private $alreadyLoadedAutoloadFiles = []; + private array $alreadyLoadedAutoloadFiles = []; public function includeDependencyOrRepositoryVendorAutoloadIfExists(): void { @@ -118,8 +76,14 @@ public function autoloadFromCommandLine(): void { $cliArgs = $_SERVER['argv']; - $autoloadOptionPosition = array_search('-a', $cliArgs, true) ?: array_search('--autoload-file', $cliArgs, true); - if (! $autoloadOptionPosition) { + $aOptionPosition = array_search('-a', $cliArgs, true); + $autoloadFileOptionPosition = array_search('--autoload-file', $cliArgs, true); + + if (is_int($aOptionPosition)) { + $autoloadOptionPosition = $aOptionPosition; + } elseif (is_int($autoloadFileOptionPosition)) { + $autoloadOptionPosition = $autoloadFileOptionPosition; + } else { return; } @@ -142,8 +106,54 @@ public function loadIfExistsAndNotLoadedYet(string $filePath): void return; } - $this->alreadyLoadedAutoloadFiles[] = realpath($filePath); + $realPath = realpath($filePath); + if (! is_string($realPath)) { + return; + } + + $this->alreadyLoadedAutoloadFiles[] = $realPath; require_once $filePath; } } + +if (file_exists(__DIR__ . '/../preload.php') && is_dir(__DIR__ . '/../vendor')) { + require_once __DIR__ . '/../preload.php'; +} + +require_once __DIR__ . '/../src/constants.php'; + +$autoloadIncluder->loadIfExistsAndNotLoadedYet(__DIR__ . '/../vendor/scoper-autoload.php'); +$autoloadIncluder->autoloadProjectAutoloaderFile(); +$autoloadIncluder->autoloadRectorInstalledAsGlobalDependency(); +$autoloadIncluder->autoloadFromCommandLine(); + +$rectorConfigsResolver = new RectorConfigsResolver(); + +try { + $bootstrapConfigs = $rectorConfigsResolver->provide(); + $rectorContainerFactory = new RectorContainerFactory(); + $container = $rectorContainerFactory->createFromBootstrapConfigs($bootstrapConfigs); +} catch (Throwable $throwable) { + // for json output + $argvInput = new ArgvInput(); + $outputFormat = $argvInput->getParameterOption('--' . Option::OUTPUT_FORMAT); + + // report fatal error in json format + if ($outputFormat === JsonOutputFormatter::NAME) { + echo Json::encode([ + 'fatal_errors' => [$throwable->getMessage()], + ]); + } else { + // report fatal errors in console format + $rectorConsoleOutputStyleFactory = new RectorConsoleOutputStyleFactory(new PrivatesCaller()); + $rectorConsoleOutputStyle = $rectorConsoleOutputStyleFactory->create(); + $rectorConsoleOutputStyle->error($throwable->getMessage()); + } + + exit(Command::FAILURE); +} + +/** @var ConsoleApplication $application */ +$application = $container->get(ConsoleApplication::class); +exit($application->run()); diff --git a/phpstan.neon b/phpstan.neon index 5b08520bd98..add986e6a80 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -670,3 +670,12 @@ parameters: path: packages/FileFormatter/ValueObject/Indent.php - '#Parameter \#1 \$indentStyle of method Rector\\FileFormatter\\ValueObjectFactory\\EditorConfigConfigurationBuilder\:\:withIndentStyle\(\) expects (.*?), string given#' + + # autoload check in bin file + - + message: '#Function "class_exists\(\)" cannot be used/left in the code\: use ReflectionProvider\->has\*\(\) instead#' + path: bin/rector.php + + - + message: '#Do not compare call directly, use a variable assign#' + path: bin/rector.php