Skip to content

Utilize composer min-php version to narrow PHP_VERSION_ID #2968

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

Closed
wants to merge 13 commits into from
Closed
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
30 changes: 29 additions & 1 deletion .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,35 @@ jobs:
cd e2e/baseline-uninit-prop-trait
../../bin/phpstan analyse --configuration test-no-baseline.neon --generate-baseline test-baseline.neon
../../bin/phpstan analyse --configuration test.neon

- 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-bc
composer install
../../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-version-config
composer install
../../bin/phpstan analyze test.php --level=0
- script: |
cd e2e/composer-version-config-invalid
OUTPUT=$(../../bin/phpstan 2>&1 || true)
grep 'Invalid configuration' <<< "$OUTPUT"
grep 'Invalid PHP version range: phpVersion.max should be greater or equal to phpVersion.min.' <<< "$OUTPUT"
steps:
- name: "Checkout"
uses: actions/checkout@v4
Expand Down
10 changes: 9 additions & 1 deletion conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,16 @@ services:
-
class: PHPStan\Php\PhpVersionFactoryFactory
arguments:
versionId: %phpVersion%
phpVersion: %phpVersion%
composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths%
bleedingEdge: %featureToggles.bleedingEdge%

-
class: PHPStan\Php\ComposerPhpVersionFactory
arguments:
phpVersion: %phpVersion%
composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths%
bleedingEdge: %featureToggles.bleedingEdge%

-
class: PHPStan\PhpDocParser\Lexer\Lexer
Expand Down
8 changes: 7 additions & 1 deletion conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,13 @@ parametersSchema:
minimumNumberOfJobsPerProcess: int(),
buffer: int()
])
phpVersion: schema(anyOf(schema(int(), min(70100), max(80399))), nullable())
phpVersion: schema(anyOf(
schema(int(), min(70100), max(80399)),
structure([
min: schema(int(), min(70100), max(80399)),
max: schema(int(), min(70100), max(80399))
])
), nullable())
polluteScopeWithLoopInitialAssignments: bool()
polluteScopeWithAlwaysIterableForeach: bool()
propertyAlwaysWrittenTags: listOf(string())
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"
}
}
2 changes: 2 additions & 0 deletions e2e/composer-max-version/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
includes:
- ../../conf/bleedingEdge.neon
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"
}
}
2 changes: 2 additions & 0 deletions e2e/composer-min-max-version/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
includes:
- ../../conf/bleedingEdge.neon
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"
}
}
2 changes: 2 additions & 0 deletions e2e/composer-min-open-end-version/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
includes:
- ../../conf/bleedingEdge.neon
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, max>', PHP_VERSION_ID);
\PHPStan\Testing\assertType('int<8, max>', PHP_MAJOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', PHP_MINOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', PHP_RELEASE_VERSION);
2 changes: 2 additions & 0 deletions e2e/composer-min-version-bc/.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-bc/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": "^8.1"
}
}
7 changes: 7 additions & 0 deletions e2e/composer-min-version-bc/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// no bleeding edge enabled
\PHPStan\Testing\assertType('int<50207, max>', PHP_VERSION_ID);
\PHPStan\Testing\assertType('int<5, max>', PHP_MAJOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', 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"
}
}
2 changes: 2 additions & 0 deletions e2e/composer-min-version/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
includes:
- ../../conf/bleedingEdge.neon
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, 90000>', PHP_VERSION_ID);
\PHPStan\Testing\assertType('int<8, 9>', PHP_MAJOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', PHP_MINOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', PHP_RELEASE_VERSION);
7 changes: 7 additions & 0 deletions e2e/composer-version-config-invalid/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- ../../conf/bleedingEdge.neon
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"
}
}
2 changes: 2 additions & 0 deletions e2e/composer-version-config-patch/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
includes:
- ../../conf/bleedingEdge.neon
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<80001, 80015>', PHP_VERSION_ID);
\PHPStan\Testing\assertType('8', PHP_MAJOR_VERSION);
\PHPStan\Testing\assertType('0', PHP_MINOR_VERSION);
\PHPStan\Testing\assertType('int<1, 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"
}
}
6 changes: 6 additions & 0 deletions e2e/composer-version-config/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
includes:
- ../../conf/bleedingEdge.neon
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 @@ 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->getMajor());
}
if ($this->composerMaxPhpVersion !== null) {
$maxMajor = $this->composerMaxPhpVersion->getMajor();
}

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->getMajor() === $this->composerMinPhpVersion->getMajor()
) {
$minMinor = $this->composerMinPhpVersion->getMinor();
$maxMinor = $this->composerMaxPhpVersion->getMinor();
}

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->getMajor() === $this->composerMinPhpVersion->getMajor()
&& $this->composerMaxPhpVersion->getMinor() === $this->composerMinPhpVersion->getMinor()
) {
$minRelease = $this->composerMinPhpVersion->getPatch();
$maxRelease = $this->composerMaxPhpVersion->getPatch();
}

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;

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
13 changes: 13 additions & 0 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use PHPStan\Type\ObjectType;
use Symfony\Component\Finder\Finder;
use function array_diff_key;
use function array_key_exists;
use function array_map;
use function array_merge;
use function array_unique;
Expand Down Expand Up @@ -352,6 +353,18 @@ private function validateParameters(array $parameters, array $parametersSchema):
$context->path = ['parameters'];
};
$processor->process($schema, $parameters);

if (
!array_key_exists('phpVersion', $parameters)
|| !is_array($parameters['phpVersion'])) {
return;
}

$phpVersion = $parameters['phpVersion'];

if ($phpVersion['max'] < $phpVersion['min']) {
throw new InvalidPhpVersionException('Invalid PHP version range: phpVersion.max should be greater or equal to phpVersion.min.');
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/DependencyInjection/InvalidPhpVersionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

use Nette\DI\InvalidConfigurationException;

class InvalidPhpVersionException extends InvalidConfigurationException
{

}
Loading
Loading