Skip to content

Utilize composer min-php version to narrow PHP_VERSION_ID #3584

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

Merged
merged 21 commits into from
Nov 6, 2024
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
45 changes: 45 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,48 @@ jobs:
- script: |
cd e2e/bug-11819
../../bin/phpstan
- script: |
cd e2e/composer-max-version
composer install
../../bin/phpstan analyze test.php --level=0
- script: |
cd e2e/composer-min-max-version
composer install
../../bin/phpstan analyze test.php --level=0
- script: |
cd e2e/composer-min-open-end-version
composer install
../../bin/phpstan analyze test.php --level=0
- script: |
cd e2e/composer-min-version-v5
composer install --ignore-platform-reqs
../../bin/phpstan analyze test.php --level=0
- script: |
cd e2e/composer-min-version-v7
composer install --ignore-platform-reqs
../../bin/phpstan analyze test.php --level=0
- script: |
cd e2e/composer-min-version
composer install
../../bin/phpstan analyze test.php --level=0
- script: |
cd e2e/composer-no-versions
composer install
../../bin/phpstan analyze test.php --level=0
- script: |
cd e2e/composer-version-config-invalid
OUTPUT=$(../bashunit -a exit_code "1" ../../bin/phpstan)
echo "$OUTPUT"
../bashunit -a contains 'Invalid configuration' "$OUTPUT"
../bashunit -a contains 'Invalid PHP version range: phpVersion.max should be greater or equal to phpVersion.min.' "$OUTPUT"
- script: |
cd e2e/composer-version-config-patch
composer install --ignore-platform-reqs
../../bin/phpstan analyze test.php --level=0
- script: |
cd e2e/composer-version-config
composer install
../../bin/phpstan analyze test.php --level=0

steps:
- name: "Checkout"
Expand All @@ -308,5 +350,8 @@ jobs:
- name: "Install dependencies"
run: "composer install --no-interaction --no-progress"

- name: "Install bashunit"
run: "curl -s https://bashunit.typeddevs.com/install.sh | bash -s e2e/ 0.17.0"

- name: "Test"
run: ${{ matrix.script }}
8 changes: 7 additions & 1 deletion conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,13 @@ services:
-
class: PHPStan\Php\PhpVersionFactoryFactory
arguments:
versionId: %phpVersion%
phpVersion: %phpVersion%
composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths%

-
class: PHPStan\Php\ComposerPhpVersionFactory
arguments:
phpVersion: %phpVersion%
composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths%

-
Expand Down
8 changes: 7 additions & 1 deletion conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ parametersSchema:
minimumNumberOfJobsPerProcess: int(),
buffer: int()
])
phpVersion: schema(anyOf(schema(int(), min(70100), max(80499))), nullable())
phpVersion: schema(anyOf(
schema(int(), min(70100), max(80499)),
structure([
min: schema(int(), min(70100), max(80499)),
max: schema(int(), min(70100), max(80499))
])
), nullable())
polluteScopeWithLoopInitialAssignments: bool()
polluteScopeWithAlwaysIterableForeach: bool()
polluteScopeWithBlock: bool()
Expand Down
2 changes: 2 additions & 0 deletions e2e/composer-max-version/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
5 changes: 5 additions & 0 deletions e2e/composer-max-version/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": "<=8.3"
}
}
10 changes: 10 additions & 0 deletions e2e/composer-max-version/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

\PHPStan\Testing\assertType('int<50207, 80300>', PHP_VERSION_ID);
\PHPStan\Testing\assertType('int<5, 8>', PHP_MAJOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', PHP_MINOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', PHP_RELEASE_VERSION);

\PHPStan\Testing\assertType('-1|0|1', version_compare(PHP_VERSION, '7.0.0'));
\PHPStan\Testing\assertType('bool', version_compare(PHP_VERSION, '7.0.0', '<'));
\PHPStan\Testing\assertType('bool', version_compare(PHP_VERSION, '7.0.0', '>'));
2 changes: 2 additions & 0 deletions e2e/composer-min-max-version/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
5 changes: 5 additions & 0 deletions e2e/composer-min-max-version/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": ">=8.1, <=8.2.99"
}
}
10 changes: 10 additions & 0 deletions e2e/composer-min-max-version/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

\PHPStan\Testing\assertType('int<80100, 80299>', PHP_VERSION_ID);
\PHPStan\Testing\assertType('8', PHP_MAJOR_VERSION);
\PHPStan\Testing\assertType('int<1, 2>', PHP_MINOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', PHP_RELEASE_VERSION);

\PHPStan\Testing\assertType('1', version_compare(PHP_VERSION, '7.0.0'));
\PHPStan\Testing\assertType('false', version_compare(PHP_VERSION, '7.0.0', '<'));
\PHPStan\Testing\assertType('true', version_compare(PHP_VERSION, '7.0.0', '>'));
2 changes: 2 additions & 0 deletions e2e/composer-min-open-end-version/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
5 changes: 5 additions & 0 deletions e2e/composer-min-open-end-version/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": ">= 8.1"
}
}
6 changes: 6 additions & 0 deletions e2e/composer-min-open-end-version/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

\PHPStan\Testing\assertType('int<80100, 80499>', PHP_VERSION_ID);
\PHPStan\Testing\assertType('8', PHP_MAJOR_VERSION);
\PHPStan\Testing\assertType('int<1, 4>', PHP_MINOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', PHP_RELEASE_VERSION);
2 changes: 2 additions & 0 deletions e2e/composer-min-version-v5/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
5 changes: 5 additions & 0 deletions e2e/composer-min-version-v5/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": "^5.6"
}
}
6 changes: 6 additions & 0 deletions e2e/composer-min-version-v5/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

\PHPStan\Testing\assertType('int<50600, 50699>', PHP_VERSION_ID);
\PHPStan\Testing\assertType('5', PHP_MAJOR_VERSION);
\PHPStan\Testing\assertType('6', PHP_MINOR_VERSION);
\PHPStan\Testing\assertType('int<0, 99>', PHP_RELEASE_VERSION);
2 changes: 2 additions & 0 deletions e2e/composer-min-version-v7/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
5 changes: 5 additions & 0 deletions e2e/composer-min-version-v7/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": "^7"
}
}
6 changes: 6 additions & 0 deletions e2e/composer-min-version-v7/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

\PHPStan\Testing\assertType('int<70000, 70499>', PHP_VERSION_ID);
\PHPStan\Testing\assertType('7', PHP_MAJOR_VERSION);
\PHPStan\Testing\assertType('int<0, 4>', PHP_MINOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', PHP_RELEASE_VERSION);
2 changes: 2 additions & 0 deletions e2e/composer-min-version/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
5 changes: 5 additions & 0 deletions e2e/composer-min-version/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": "^8.1"
}
}
6 changes: 6 additions & 0 deletions e2e/composer-min-version/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

\PHPStan\Testing\assertType('int<80100, 80499>', PHP_VERSION_ID);
\PHPStan\Testing\assertType('8', PHP_MAJOR_VERSION);
\PHPStan\Testing\assertType('int<1, 4>', PHP_MINOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', PHP_RELEASE_VERSION);
2 changes: 2 additions & 0 deletions e2e/composer-no-versions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
2 changes: 2 additions & 0 deletions e2e/composer-no-versions/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
6 changes: 6 additions & 0 deletions e2e/composer-no-versions/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

\PHPStan\Testing\assertType('int<50207, 80499>', PHP_VERSION_ID);
\PHPStan\Testing\assertType('int<5, 8>', PHP_MAJOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', PHP_MINOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', PHP_RELEASE_VERSION);
5 changes: 5 additions & 0 deletions e2e/composer-version-config-invalid/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
phpVersion:
min: 80303
max: 80104

2 changes: 2 additions & 0 deletions e2e/composer-version-config-patch/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
5 changes: 5 additions & 0 deletions e2e/composer-version-config-patch/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": ">=8.0.2, <8.0.15"
}
}
7 changes: 7 additions & 0 deletions e2e/composer-version-config-patch/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// constraint from composer.json is overruled by phpstan.neon config
\PHPStan\Testing\assertType('int<80002, 80015>', PHP_VERSION_ID);
\PHPStan\Testing\assertType('8', PHP_MAJOR_VERSION);
\PHPStan\Testing\assertType('0', PHP_MINOR_VERSION);
\PHPStan\Testing\assertType('int<2, 15>', PHP_RELEASE_VERSION);
2 changes: 2 additions & 0 deletions e2e/composer-version-config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
5 changes: 5 additions & 0 deletions e2e/composer-version-config/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": "^8.0"
}
}
4 changes: 4 additions & 0 deletions e2e/composer-version-config/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
phpVersion:
min: 80103
max: 80304
11 changes: 11 additions & 0 deletions e2e/composer-version-config/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

// constraint from composer.json is overruled by phpstan.neon config
\PHPStan\Testing\assertType('int<80103, 80304>', PHP_VERSION_ID);
\PHPStan\Testing\assertType('8', PHP_MAJOR_VERSION);
\PHPStan\Testing\assertType('int<1, 3>', PHP_MINOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', PHP_RELEASE_VERSION);

\PHPStan\Testing\assertType('1', version_compare(PHP_VERSION, '7.0.0'));
\PHPStan\Testing\assertType('false', version_compare(PHP_VERSION, '7.0.0', '<'));
\PHPStan\Testing\assertType('true', version_compare(PHP_VERSION, '7.0.0', '>'));
69 changes: 64 additions & 5 deletions src/Analyser/ConstantResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Analyser;

use PhpParser\Node\Name;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\NamespaceAnswerer;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Reflection\ReflectionProvider\ReflectionProviderProvider;
Expand All @@ -20,6 +21,7 @@
use PHPStan\Type\UnionType;
use function array_key_exists;
use function in_array;
use function max;
use function sprintf;
use const INF;
use const NAN;
Expand All @@ -34,7 +36,12 @@ final class ConstantResolver
/**
* @param string[] $dynamicConstantNames
*/
public function __construct(private ReflectionProviderProvider $reflectionProviderProvider, private array $dynamicConstantNames)
public function __construct(
private ReflectionProviderProvider $reflectionProviderProvider,
private array $dynamicConstantNames,
private ?PhpVersion $composerMinPhpVersion,
private ?PhpVersion $composerMaxPhpVersion,
)
{
}

Expand Down Expand Up @@ -77,16 +84,60 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
]);
}
if ($resolvedConstantName === 'PHP_MAJOR_VERSION') {
return IntegerRangeType::fromInterval(5, null);
$minMajor = 5;
$maxMajor = null;

if ($this->composerMinPhpVersion !== null) {
$minMajor = max($minMajor, $this->composerMinPhpVersion->getMajorVersionId());
}
if ($this->composerMaxPhpVersion !== null) {
$maxMajor = $this->composerMaxPhpVersion->getMajorVersionId();
}

return $this->createInteger($minMajor, $maxMajor);
}
if ($resolvedConstantName === 'PHP_MINOR_VERSION') {
return IntegerRangeType::fromInterval(0, null);
$minMinor = 0;
$maxMinor = null;

if (
$this->composerMinPhpVersion !== null
&& $this->composerMaxPhpVersion !== null
&& $this->composerMaxPhpVersion->getMajorVersionId() === $this->composerMinPhpVersion->getMajorVersionId()
) {
$minMinor = $this->composerMinPhpVersion->getMinorVersionId();
$maxMinor = $this->composerMaxPhpVersion->getMinorVersionId();
}

return $this->createInteger($minMinor, $maxMinor);
}
if ($resolvedConstantName === 'PHP_RELEASE_VERSION') {
return IntegerRangeType::fromInterval(0, null);
$minRelease = 0;
$maxRelease = null;

if (
$this->composerMinPhpVersion !== null
&& $this->composerMaxPhpVersion !== null
&& $this->composerMaxPhpVersion->getMajorVersionId() === $this->composerMinPhpVersion->getMajorVersionId()
&& $this->composerMaxPhpVersion->getMinorVersionId() === $this->composerMinPhpVersion->getMinorVersionId()
) {
$minRelease = $this->composerMinPhpVersion->getPatchVersionId();
$maxRelease = $this->composerMaxPhpVersion->getPatchVersionId();
}

return $this->createInteger($minRelease, $maxRelease);
}
if ($resolvedConstantName === 'PHP_VERSION_ID') {
return IntegerRangeType::fromInterval(50207, null);
$minVersion = 50207;
$maxVersion = null;
if ($this->composerMinPhpVersion !== null) {
$minVersion = max($minVersion, $this->composerMinPhpVersion->getVersionId());
}
if ($this->composerMaxPhpVersion !== null) {
$maxVersion = $this->composerMaxPhpVersion->getVersionId();
}

return $this->createInteger($minVersion, $maxVersion);
}
if ($resolvedConstantName === 'PHP_ZTS') {
return new UnionType([
Expand Down Expand Up @@ -325,6 +376,14 @@ public function resolveClassConstantType(string $className, string $constantName
return $constantType;
}

private function createInteger(?int $min, ?int $max): Type
{
if ($min !== null && $min === $max) {
return new ConstantIntegerType($min);
}
return IntegerRangeType::fromInterval($min, $max);
}

private function getReflectionProvider(): ReflectionProvider
{
return $this->reflectionProviderProvider->getReflectionProvider();
Expand Down
5 changes: 5 additions & 0 deletions src/Analyser/ConstantResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Analyser;

use PHPStan\DependencyInjection\Container;
use PHPStan\Php\ComposerPhpVersionFactory;
use PHPStan\Reflection\ReflectionProvider\ReflectionProviderProvider;

final class ConstantResolverFactory
Expand All @@ -17,9 +18,13 @@ public function __construct(

public function create(): ConstantResolver
{
$composerFactory = $this->container->getByType(ComposerPhpVersionFactory::class);

return new ConstantResolver(
$this->reflectionProviderProvider,
$this->container->getParameter('dynamicConstantNames'),
$composerFactory->getMinVersion(),
$composerFactory->getMaxVersion(),
);
}

Expand Down
Loading
Loading