Skip to content

Commit

Permalink
Use local version resolver (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 14, 2021
1 parent c7700cf commit fa8e597
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 41 deletions.
8 changes: 0 additions & 8 deletions bin/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@
exit(ShellCode::ERROR);
}


// preload local InstalledVersions.php - to fix incorrect version by same-named class in phpstan
$currentlyInstalledVersions = __DIR__ . '/../../../../vendor/composer/InstalledVersions.php';
if (file_exists($currentlyInstalledVersions)) {
require_once $currentlyInstalledVersions;
}


/** @var ConsoleApplication $application */
$application = $container->get(ConsoleApplication::class);
exit($application->run());
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"doctrine/inflector": "^2.0",
"ergebnis/json-printer": "^3.1",
"idiosyncratic/editorconfig": "^0.1.0",
"jean85/pretty-package-versions": "^1.6",
"nette/caching": "^3.1",
"nette/utils": "^3.2",
"nikic/php-parser": "4.10.5",
Expand Down
1 change: 0 additions & 1 deletion packages/ChangesReporting/Output/JsonOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function report(ProcessResult $processResult): void
{
$errorsArray = [
'meta' => [
'version' => $this->configuration->getPrettyVersion(),
'config' => $this->configuration->getMainConfigFilePath(),
],
'totals' => [
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,5 @@ parameters:
- '#Method Rector\\BetterPhpDocParser\\PhpDocParser\\BetterPhpDocParser\:\:parseChildAndStoreItsPositions\(\) should return PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocTagNode\|PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocTextNode but returns PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocChildNode#'

- '#Parameter \#1 \$type of method Rector\\BetterPhpDocParser\\PhpDocInfo\\PhpDocInfo<PHPStan\\PhpDocParser\\Ast\\Node\>\:\:removeByType\(\) expects class\-string<PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocTagValueNode\>, class\-string<PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocTagValueNode\>\|PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocTagValueNode given#'

- '#Method Rector\\Core\\Application\\VersionResolver\:\:resolvePackageData\(\) return type has no value type specified in iterable type array#'
13 changes: 0 additions & 13 deletions scoper-php70.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,6 @@ function (string $filePath, string $prefix, string $content) use ($filePathsToRe
return $content;
},

function (string $filePath, string $prefix, string $content): string {
if (! Strings::endsWith($filePath, 'vendor/composer/package-versions-deprecated/src/PackageVersions/Versions.php')) {
return $content;
}

// see https://regex101.com/r/v8zRMm/1
return Strings::replace(
$content, '
#' . $prefix . '\\\\Composer\\\\InstalledVersions#',
'Composer\InstalledVersions'
);
},

// unprefixed SmartFileInfo
function (string $filePath, string $prefix, string $content): string {
return Strings::replace(
Expand Down
2 changes: 0 additions & 2 deletions scoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@

// unprefixed ComposerJson as part of public API in ComposerRectorInterface
'rules/Composer/Contract/Rector/ComposerRectorInterface.php' => ['Symplify\ComposerJsonManipulator\ValueObject\ComposerJson'],

'vendor/composer/package-versions-deprecated/src/PackageVersions/Versions.php' => ['Composer\InstalledVersions'],
'packages/Testing/PHPUnit/AbstractTestCase.php' => ['PHPUnit\Framework\TestCase'],
];

Expand Down
55 changes: 55 additions & 0 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Application;

use Composer\InstalledVersions;
use Nette\Utils\Strings;

/**
* Inspired by https://github.com/symplify/symplify/pull/3179/files
* Local resolver is needed, because PHPStan is unprefixing its InstalledVersion classes and the API is changing way too often.
* This makes sure it works without dependency on external conditions.
*/
final class VersionResolver
{
public function resolve(): string
{
// give local IntalledVersions a priority above anything else
$intalledVersionsFilepath = __DIR__ . '/../../vendor/composer/InstalledVersions.php';
if (file_exists($intalledVersionsFilepath)) {
require_once $intalledVersionsFilepath;
}

$installedRawData = InstalledVersions::getRawData();

$rectorPackageData = $this->resolvePackageData($installedRawData);
if ($rectorPackageData === null) {
return 'Unknown';
}

if (isset($rectorPackageData['replaced'])) {
return 'replaced@' . $rectorPackageData['replaced'][0];
}

if ($rectorPackageData['version'] === 'dev-main') {
$reference = $rectorPackageData['reference'] ?? null;
if ($reference === null) {
return 'dev-main';
}

return 'dev-main@' . Strings::substring($rectorPackageData['reference'], 0, 7);
}

return $rectorPackageData['version'];
}

/**
* @param mixed[] $installedRawData
*/
private function resolvePackageData(array $installedRawData): ?array
{
return $installedRawData['versions']['rector/rector-src'] ?? $installedRawData['versions']['rector/rector'] ?? $installedRawData['root'] ?? null;
}
}
7 changes: 0 additions & 7 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Core\Configuration;

use Jean85\PrettyVersions;
use Nette\Utils\Strings;
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
use Rector\Core\Exception\Configuration\InvalidConfigurationException;
Expand Down Expand Up @@ -80,12 +79,6 @@ public function resolveFromInput(InputInterface $input): void
}
}

public function getPrettyVersion(): string
{
$version = PrettyVersions::getVersion('rector/rector');
return $version->getPrettyVersion();
}

/**
* @forTests
*/
Expand Down
12 changes: 3 additions & 9 deletions src/Console/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
namespace Rector\Core\Console;

use Composer\XdebugHandler\XdebugHandler;
use OutOfBoundsException;
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
use Rector\Core\Configuration\Configuration;
use Rector\Core\Application\VersionResolver;
use Rector\Core\Configuration\Option;
use Rector\Core\Console\Command\ProcessCommand;
use Rector\Core\Exception\Configuration\InvalidConfigurationException;
Expand All @@ -29,14 +28,9 @@ final class ConsoleApplication extends Application
/**
* @param Command[] $commands
*/
public function __construct(Configuration $configuration, CommandNaming $commandNaming, array $commands = [])
public function __construct(VersionResolver $versionResolver, CommandNaming $commandNaming, array $commands = [])
{
try {
$version = $configuration->getPrettyVersion();
} catch (OutOfBoundsException) {
$version = 'Unknown';
}

$version = $versionResolver->resolve();
parent::__construct(self::NAME, $version);

foreach ($commands as $command) {
Expand Down

0 comments on commit fa8e597

Please sign in to comment.