Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions bin/add-phpstan-self-replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@

use Nette\Utils\FileSystem;
use Nette\Utils\Json;
use PackageVersions\Versions;
use Rector\Composer\InstalledPackageResolver;

require __DIR__ . '/../vendor/autoload.php';

if (! class_exists(Versions::class)) {
echo 'You need to run `composer require ocramius/package-versions` first' . PHP_EOL;
exit(1);
}

$composerJsonFileContents = FileSystem::read(__DIR__ . '/../composer.json');

$installedPackageResolver = new InstalledPackageResolver(__DIR__ . '/..');
$phpstanVersion = $installedPackageResolver->resolvePackageVersion('phpstan/phpstan');

$composerJson = Json::decode($composerJsonFileContents, forceArrays: true);
// result output is like: // 1.0.0@0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
[$phpstanVersion] = explode('@', Versions::getVersion('phpstan/phpstan'));
$composerJson['replace']['phpstan/phpstan'] = $phpstanVersion;

$modifiedComposerJsonFileContents = Json::encode($composerJson, pretty: true);
Expand Down
17 changes: 2 additions & 15 deletions build/build-rector-scoped.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,13 @@ note "Starts"
note "Downloading php-scoper.phar"
wget https://github.com/humbug/php-scoper/releases/download/0.18.17/php-scoper.phar -N --no-verbose

# make PackageVersions exists
# here while wait for PHP 8.5 compatible code
# see https://github.com/Ocramius/PackageVersions/pull/270
# to allow us on rector to create downgrade PHP 8.5 rules with define
#
# #[RequiresPhp('>= 8.5')]
#
# On Downgrade PHP 8.5 rule, see https://github.com/rectorphp/rector-downgrade-php/pull/337
#
composer require ocramius/package-versions --working-dir "$BUILD_DIRECTORY" --update-no-dev

php "$BUILD_DIRECTORY/bin/add-phpstan-self-replace.php"

# avoid phpstan/phpstan and ocramius/package-versions dependency duplicate
note "Remove PHPStan and PackageVersions to avoid duplicating it"
note "Remove PHPStan to avoid duplicating it"

composer remove phpstan/phpstan -W --update-no-dev --working-dir "$BUILD_DIRECTORY"
composer remove ocramius/package-versions -W --update-no-dev --working-dir "$BUILD_DIRECTORY"

note "PHPStan and PackageVersions now removed, safe to start php-scoper from here"
note "PHPStan now removed, safe to start php-scoper from here"

# Work around possible PHP memory limits
note "Running php-scoper on /bin, /config, /src, /rules and /vendor"
Expand Down
1 change: 0 additions & 1 deletion composer-dependency-analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
__DIR__ . '/stubs',
__DIR__ . '/tests',
__DIR__ . '/rules-tests',
__DIR__ . '/bin/add-phpstan-self-replace.php',
], [ErrorType::UNKNOWN_CLASS])

->disableExtensionsAnalysis();
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ parameters:
message: '#Function "var_dump\(\)" cannot be used/left in the code#'
path: src/functions/node_helper.php

-
message: '#Function "class_exists\(\)" cannot be used/left in the code#'
path: bin/add-phpstan-self-replace.php

# lack of generic array in nikic/php-parser
- '#Method (.*?) should return array<PhpParser\\Node\\(.*?)\> but returns array<PhpParser\\Node\>#'

Expand Down
17 changes: 16 additions & 1 deletion src/Composer/InstalledPackageResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function resolve(): array
return $this->resolvedInstalledPackages;
}

$installedPackagesFilePath = self::resolveVendorDir() . '/composer/installed.json';
$installedPackagesFilePath = $this->resolveVendorDir() . '/composer/installed.json';
if (! file_exists($installedPackagesFilePath)) {
throw new ShouldNotHappenException(
'The installed package json not found. Make sure you run `composer update` and the "vendor/composer/installed.json" file exists'
Expand All @@ -59,6 +59,20 @@ public function resolve(): array
return $installedPackages;
}

public function resolvePackageVersion(string $packageName): ?string
{
$installedPackages = $this->resolve();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may need unit test to extract tagged version; eg

1.0.0@0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33 to become 1.0.0 only

foreach ($installedPackages as $installedPackage) {
if ($installedPackage->getName() !== $packageName) {
continue;
}

return $installedPackage->getVersion();
}

return null;
}

/**
* @param mixed[] $packages
* @return InstalledPackage[]
Expand All @@ -77,6 +91,7 @@ private function createInstalledPackages(array $packages): array
private function resolveVendorDir(): string
{
$projectComposerJsonFilePath = $this->projectDirectory . '/composer.json';

if (\file_exists($projectComposerJsonFilePath)) {
$projectComposerContents = FileSystem::read($projectComposerJsonFilePath);
$projectComposerJson = Json::decode($projectComposerContents, true);
Expand Down