Skip to content

Commit

Permalink
Read and enforce PHPStan version constraints from extension-installer…
Browse files Browse the repository at this point in the history
… v1.4
  • Loading branch information
ondrejmirtes committed Jun 6, 2024
1 parent 04b8403 commit 1d005ef
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"composer-runtime-api": "^2.0",
"clue/ndjson-react": "^1.0",
"composer/ca-bundle": "^1.2",
"composer/semver": "^3.4",
"composer/xdebug-handler": "^3.0.3",
"fidry/cpu-core-counter": "^0.5.0",
"hoa/compiler": "3.17.08.08",
Expand Down
83 changes: 82 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Command;

use Composer\Semver\Semver;
use Composer\XdebugHandler\XdebugHandler;
use Nette\DI\Helpers;
use Nette\DI\InvalidConfigurationException;
Expand All @@ -23,6 +24,8 @@
use PHPStan\File\FileExcluder;
use PHPStan\File\FileFinder;
use PHPStan\File\FileHelper;
use PHPStan\File\SimpleRelativePathHelper;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Internal\DirectoryCreator;
use PHPStan\Internal\DirectoryCreatorException;
use PHPStan\PhpDoc\StubFilesProvider;
Expand Down Expand Up @@ -279,6 +282,43 @@ public static function begin(
$additionalConfigFiles[] = $includedFilePath;
}
}

if (
count($additionalConfigFiles) > 0
&& $generatedConfigReflection->hasConstant('PHPSTAN_VERSION_CONSTRAINT')
) {
$generatedConfigPhpStanVersionConstraint = $generatedConfigReflection->getConstant('PHPSTAN_VERSION_CONSTRAINT');
if ($generatedConfigPhpStanVersionConstraint !== null) {
$phpstanSemverVersion = ComposerHelper::getPhpStanVersion();
if (
$phpstanSemverVersion !== ComposerHelper::UNKNOWN_VERSION
&& !str_contains($phpstanSemverVersion, '@')
&& !Semver::satisfies($phpstanSemverVersion, $generatedConfigPhpStanVersionConstraint)
) {
$errorOutput->writeLineFormatted('<error>Running PHPStan with incompatible extensions</error>');
$errorOutput->writeLineFormatted('You\'re running PHPStan from a different Composer project');
$errorOutput->writeLineFormatted('than the one where you installed extensions.');
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted(sprintf('Your PHPStan version is: <fg=red>%s</>', $phpstanSemverVersion));
$errorOutput->writeLineFormatted(sprintf('Installed PHPStan extensions support: %s', $generatedConfigPhpStanVersionConstraint));

$errorOutput->writeLineFormatted('');
if (isset($_SERVER['argv'][0]) && is_file($_SERVER['argv'][0])) {
$mainScript = $_SERVER['argv'][0];
$errorOutput->writeLineFormatted(sprintf('PHPStan is running from: %s', $currentWorkingDirectoryFileHelper->absolutizePath(dirname($mainScript))));
}

$errorOutput->writeLineFormatted(sprintf('Extensions were installed in: %s', dirname($generatedConfigDirectory, 3)));
$errorOutput->writeLineFormatted('');

$simpleRelativePathHelper = new SimpleRelativePathHelper($currentWorkingDirectory);
$errorOutput->writeLineFormatted(sprintf('Run PHPStan with <fg=green>%s</> to fix this problem.', $simpleRelativePathHelper->getRelativePath(dirname($generatedConfigDirectory, 3) . '/bin/phpstan')));

$errorOutput->writeLineFormatted('');
throw new InceptionNotSuccessfulException();
}
}
}
}

if (
Expand Down
4 changes: 3 additions & 1 deletion src/Internal/ComposerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
final class ComposerHelper
{

public const UNKNOWN_VERSION = 'Unknown version';

private static ?string $phpstanVersion = null;

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

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

0 comments on commit 1d005ef

Please # to comment.