Fix position of class after non-reachable stmts (#2299)

* class cannot be under non-reachable stmts

* static fixes in bin/rector.php
This commit is contained in:
Tomas Votruba 2022-05-12 11:05:03 +02:00 committed by GitHub
parent 3bae3b9e75
commit 33cd52e703
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 46 deletions

View File

@ -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());

View File

@ -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