Skip to content

Commit 5b5c2cd

Browse files
authored
add simple script to resolve phpstan version (#7522)
1 parent a56a503 commit 5b5c2cd

File tree

5 files changed

+22
-29
lines changed

5 files changed

+22
-29
lines changed

bin/add-phpstan-self-replace.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@
66

77
use Nette\Utils\FileSystem;
88
use Nette\Utils\Json;
9-
use PackageVersions\Versions;
9+
use Rector\Composer\InstalledPackageResolver;
1010

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

13-
if (! class_exists(Versions::class)) {
14-
echo 'You need to run `composer require ocramius/package-versions` first' . PHP_EOL;
15-
exit(1);
16-
}
17-
1813
$composerJsonFileContents = FileSystem::read(__DIR__ . '/../composer.json');
1914

15+
$installedPackageResolver = new InstalledPackageResolver(__DIR__ . '/..');
16+
$phpstanVersion = $installedPackageResolver->resolvePackageVersion('phpstan/phpstan');
17+
2018
$composerJson = Json::decode($composerJsonFileContents, forceArrays: true);
21-
// result output is like: // 1.0.0@0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
22-
[$phpstanVersion] = explode('@', Versions::getVersion('phpstan/phpstan'));
2319
$composerJson['replace']['phpstan/phpstan'] = $phpstanVersion;
2420

2521
$modifiedComposerJsonFileContents = Json::encode($composerJson, pretty: true);

build/build-rector-scoped.sh

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,13 @@ note "Starts"
3030
note "Downloading php-scoper.phar"
3131
wget https://github.com/humbug/php-scoper/releases/download/0.18.17/php-scoper.phar -N --no-verbose
3232

33-
# make PackageVersions exists
34-
# here while wait for PHP 8.5 compatible code
35-
# see https://github.com/Ocramius/PackageVersions/pull/270
36-
# to allow us on rector to create downgrade PHP 8.5 rules with define
37-
#
38-
# #[RequiresPhp('>= 8.5')]
39-
#
40-
# On Downgrade PHP 8.5 rule, see https://github.com/rectorphp/rector-downgrade-php/pull/337
41-
#
42-
composer require ocramius/package-versions --working-dir "$BUILD_DIRECTORY" --update-no-dev
43-
4433
php "$BUILD_DIRECTORY/bin/add-phpstan-self-replace.php"
4534

46-
# avoid phpstan/phpstan and ocramius/package-versions dependency duplicate
47-
note "Remove PHPStan and PackageVersions to avoid duplicating it"
35+
note "Remove PHPStan to avoid duplicating it"
4836

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

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

5441
# Work around possible PHP memory limits
5542
note "Running php-scoper on /bin, /config, /src, /rules and /vendor"

composer-dependency-analyser.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
__DIR__ . '/stubs',
2222
__DIR__ . '/tests',
2323
__DIR__ . '/rules-tests',
24-
__DIR__ . '/bin/add-phpstan-self-replace.php',
2524
], [ErrorType::UNKNOWN_CLASS])
2625

2726
->disableExtensionsAnalysis();

phpstan.neon

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ parameters:
9292
message: '#Function "var_dump\(\)" cannot be used/left in the code#'
9393
path: src/functions/node_helper.php
9494

95-
-
96-
message: '#Function "class_exists\(\)" cannot be used/left in the code#'
97-
path: bin/add-phpstan-self-replace.php
98-
9995
# lack of generic array in nikic/php-parser
10096
- '#Method (.*?) should return array<PhpParser\\Node\\(.*?)\> but returns array<PhpParser\\Node\>#'
10197

src/Composer/InstalledPackageResolver.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function resolve(): array
4242
return $this->resolvedInstalledPackages;
4343
}
4444

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

62+
public function resolvePackageVersion(string $packageName): ?string
63+
{
64+
$installedPackages = $this->resolve();
65+
foreach ($installedPackages as $installedPackage) {
66+
if ($installedPackage->getName() !== $packageName) {
67+
continue;
68+
}
69+
70+
return $installedPackage->getVersion();
71+
}
72+
73+
return null;
74+
}
75+
6276
/**
6377
* @param mixed[] $packages
6478
* @return InstalledPackage[]
@@ -77,6 +91,7 @@ private function createInstalledPackages(array $packages): array
7791
private function resolveVendorDir(): string
7892
{
7993
$projectComposerJsonFilePath = $this->projectDirectory . '/composer.json';
94+
8095
if (\file_exists($projectComposerJsonFilePath)) {
8196
$projectComposerContents = FileSystem::read($projectComposerJsonFilePath);
8297
$projectComposerJson = Json::decode($projectComposerContents, true);

0 commit comments

Comments
 (0)