Skip to content

Commit

Permalink
Fix position of class after non-reachable stmts (#2299)
Browse files Browse the repository at this point in the history
* class cannot be under non-reachable stmts

* static fixes in bin/rector.php
  • Loading branch information
TomasVotruba committed May 12, 2022
1 parent 3bae3b9 commit 33cd52e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 46 deletions.
102 changes: 56 additions & 46 deletions bin/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}

Expand All @@ -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());
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 33cd52e

Please sign in to comment.