Skip to content

Commit 1d005ef

Browse files
committed
Read and enforce PHPStan version constraints from extension-installer v1.4
1 parent 04b8403 commit 1d005ef

File tree

4 files changed

+126
-2
lines changed

4 files changed

+126
-2
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"composer-runtime-api": "^2.0",
1010
"clue/ndjson-react": "^1.0",
1111
"composer/ca-bundle": "^1.2",
12+
"composer/semver": "^3.4",
1213
"composer/xdebug-handler": "^3.0.3",
1314
"fidry/cpu-core-counter": "^0.5.0",
1415
"hoa/compiler": "3.17.08.08",

composer.lock

Lines changed: 82 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Command/CommandHelper.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Command;
44

5+
use Composer\Semver\Semver;
56
use Composer\XdebugHandler\XdebugHandler;
67
use Nette\DI\Helpers;
78
use Nette\DI\InvalidConfigurationException;
@@ -23,6 +24,8 @@
2324
use PHPStan\File\FileExcluder;
2425
use PHPStan\File\FileFinder;
2526
use PHPStan\File\FileHelper;
27+
use PHPStan\File\SimpleRelativePathHelper;
28+
use PHPStan\Internal\ComposerHelper;
2629
use PHPStan\Internal\DirectoryCreator;
2730
use PHPStan\Internal\DirectoryCreatorException;
2831
use PHPStan\PhpDoc\StubFilesProvider;
@@ -279,6 +282,43 @@ public static function begin(
279282
$additionalConfigFiles[] = $includedFilePath;
280283
}
281284
}
285+
286+
if (
287+
count($additionalConfigFiles) > 0
288+
&& $generatedConfigReflection->hasConstant('PHPSTAN_VERSION_CONSTRAINT')
289+
) {
290+
$generatedConfigPhpStanVersionConstraint = $generatedConfigReflection->getConstant('PHPSTAN_VERSION_CONSTRAINT');
291+
if ($generatedConfigPhpStanVersionConstraint !== null) {
292+
$phpstanSemverVersion = ComposerHelper::getPhpStanVersion();
293+
if (
294+
$phpstanSemverVersion !== ComposerHelper::UNKNOWN_VERSION
295+
&& !str_contains($phpstanSemverVersion, '@')
296+
&& !Semver::satisfies($phpstanSemverVersion, $generatedConfigPhpStanVersionConstraint)
297+
) {
298+
$errorOutput->writeLineFormatted('<error>Running PHPStan with incompatible extensions</error>');
299+
$errorOutput->writeLineFormatted('You\'re running PHPStan from a different Composer project');
300+
$errorOutput->writeLineFormatted('than the one where you installed extensions.');
301+
$errorOutput->writeLineFormatted('');
302+
$errorOutput->writeLineFormatted(sprintf('Your PHPStan version is: <fg=red>%s</>', $phpstanSemverVersion));
303+
$errorOutput->writeLineFormatted(sprintf('Installed PHPStan extensions support: %s', $generatedConfigPhpStanVersionConstraint));
304+
305+
$errorOutput->writeLineFormatted('');
306+
if (isset($_SERVER['argv'][0]) && is_file($_SERVER['argv'][0])) {
307+
$mainScript = $_SERVER['argv'][0];
308+
$errorOutput->writeLineFormatted(sprintf('PHPStan is running from: %s', $currentWorkingDirectoryFileHelper->absolutizePath(dirname($mainScript))));
309+
}
310+
311+
$errorOutput->writeLineFormatted(sprintf('Extensions were installed in: %s', dirname($generatedConfigDirectory, 3)));
312+
$errorOutput->writeLineFormatted('');
313+
314+
$simpleRelativePathHelper = new SimpleRelativePathHelper($currentWorkingDirectory);
315+
$errorOutput->writeLineFormatted(sprintf('Run PHPStan with <fg=green>%s</> to fix this problem.', $simpleRelativePathHelper->getRelativePath(dirname($generatedConfigDirectory, 3) . '/bin/phpstan')));
316+
317+
$errorOutput->writeLineFormatted('');
318+
throw new InceptionNotSuccessfulException();
319+
}
320+
}
321+
}
282322
}
283323

284324
if (

src/Internal/ComposerHelper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
final class ComposerHelper
1818
{
1919

20+
public const UNKNOWN_VERSION = 'Unknown version';
21+
2022
private static ?string $phpstanVersion = null;
2123

2224
/** @return array<string, mixed>|null */
@@ -75,7 +77,7 @@ public static function getPhpStanVersion(): string
7577
$installed = require __DIR__ . '/../../vendor/composer/installed.php';
7678
$rootPackage = $installed['root'] ?? null;
7779
if ($rootPackage === null) {
78-
return self::$phpstanVersion = 'Unknown version';
80+
return self::$phpstanVersion = self::UNKNOWN_VERSION;
7981
}
8082

8183
if (preg_match('/[^v\d.]/', $rootPackage['pretty_version']) === 0) {

0 commit comments

Comments
 (0)