Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilize composer min-php version to narrow PHP_VERSION_ID #3584

Merged
merged 21 commits into from
Nov 6, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Oct 26, 2024

idea is to catch errors like Roave/BetterReflection#1406 (comment) in which bogus conditions like if (PHP_VERSION_ID >= 80100) {} have been used in tests.

it's a bug in the linked case, because the project itself only supports "php": "~8.2.0 || ~8.3.2",.
Since CI only is running against in composer.json declared supported versions this error was not spotted before.


rebased version of #2968 for 2.x

@staabm
Copy link
Contributor Author

staabm commented Oct 26, 2024

PHPStan 2.x seems the perfect point in time to re-vive this PR, as it found some dead tests in phpstan-extension repos :) (because of the raised min-php version)

try {
$composerJsonContents = FileReader::read($composerJsonPath);
$composer = Json::decode($composerJsonContents, Json::FORCE_ARRAY);
$requiredVersion = $composer['require']['php'] ?? null;
Copy link
Contributor

Choose a reason for hiding this comment

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

php-64bit should be supported as well - the minimal version should be used of these two if both are present

@staabm staabm marked this pull request as ready for review October 26, 2024 16:42
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

conf/bleedingEdge.neon Outdated Show resolved Hide resolved
Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

Now I can finally review it :) Hopefully it makes 2.0, otherwise we'll have to add the bleeding edge toggles again 😂

@@ -0,0 +1,6 @@
<?php

\PHPStan\Testing\assertType('int<80100, max>', PHP_VERSION_ID);
Copy link
Member

Choose a reason for hiding this comment

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

This could be until 80499 which is the max version allowed in the config. It'd help us prevent more typos and be consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it now takes the max into account.

I have also added the min-version

Copy link
Member

Choose a reason for hiding this comment

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

Do not change anything about the min version. It might seem inconsistent to you, but PHPStan can support analysing PHP 5 codebases without major issues and this would be too annoying.

Please also add E2E tests what would PHPStan think about the PHP_ constants with:

  • something like ^5.6 in composer.json. The result should be as precise as possible -meaning 50600-50699 (there was no 5.7)
  • something like ^7.0 in composer.json. The result should be as precise as possible -meaning 70000-70499 (there was no 7.5)

Copy link
Member

Choose a reason for hiding this comment

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

I want to limit the min key in phpstan.neon still to 70100 (because PHPStan doesn't offer any specific logic to lower versions), but I don't want to flag asking for PHP 5 as an error when there's nothing configured about a PHP version in phpstan.neon or composer.json. (Or it's configured in a way that allows for PHP 5, like <=8.3 or ^5.6)

Copy link
Member

Choose a reason for hiding this comment

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

Hope it's understandable.

Copy link
Member

Choose a reason for hiding this comment

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

Coming to thing of it, there's a 3rd E2E test proposal:

  • No version in composer.json, no version in phpstan.neon - assert the PHP_ constant values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I adressed everything

e2e/composer-min-version/test.php Outdated Show resolved Hide resolved
src/Php/PhpVersion.php Outdated Show resolved Hide resolved
src/Php/PhpVersion.php Show resolved Hide resolved
src/Php/PhpVersion.php Show resolved Hide resolved
src/Php/ComposerPhpVersionFactory.php Outdated Show resolved Hide resolved
@staabm
Copy link
Contributor Author

staabm commented Nov 6, 2024

@ondrejmirtes ondrejmirtes merged commit 6924e46 into phpstan:2.0.x Nov 6, 2024
414 checks passed
@ondrejmirtes
Copy link
Member

Thank you. I'd like a follow up PR - update PHPStanDiagnoseExtension and add information:

  • When the phpVersion in phpstan.neon is a min-max range
  • When there's php in require in composer.json - add it as a new row

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants