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 @@ define('__RECTOR_RUNNING__', true); $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 @@ final class AutoloadIncluder { $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 @@ final class AutoloadIncluder 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