Skip to content
Draft
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
90 changes: 90 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
use PHPStan\Php\PhpVersion;
use PHPStan\Php\PhpVersionFactory;
use PHPStan\Php\PhpVersions;
use PHPStan\Php\SimplePhpVersionParser;
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\Reflection\Assertions;
use PHPStan\Reflection\AttributeReflection;
Expand Down Expand Up @@ -124,6 +125,7 @@
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Php\VersionCompareFunctionDynamicReturnTypeExtension;
use PHPStan\Type\StaticType;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\StringType;
Expand Down Expand Up @@ -254,6 +256,51 @@ public function __construct(
$this->namespace = $namespace;
}

private function getComparePhpVersionType(string $value, ?Expr $operator): ?Type
{
$parsedVersion = SimplePhpVersionParser::parseVersion($value);
if ($parsedVersion === null) {
return null;
}

if ($operator !== null) {
$operators = $this->getType($operator)->getConstantStrings();
if (count($operators) !== 1) {
return null;
}

$operatorString = $operators[0]->getValue();
} else {
$operatorString = '<';
}

if (!in_array($operatorString, VersionCompareFunctionDynamicReturnTypeExtension::VALID_OPERATORS, true)) {
return null;
}

if (in_array($operatorString, ['<', 'lt'], true)) {
return IntegerRangeType::fromInterval(null, $parsedVersion->getVersionId() - 1);
}
if (in_array($operatorString, ['<=', 'le'], true)) {
return IntegerRangeType::fromInterval(null, $parsedVersion->getVersionId());
}

if (in_array($operatorString, ['>', 'gt'], true)) {
return IntegerRangeType::fromInterval($parsedVersion->getVersionId() + 1, null);
}
if (in_array($operatorString, ['>=', 'ge'], true)) {
return IntegerRangeType::fromInterval($parsedVersion->getVersionId(), null);
}

if (
in_array($operatorString, ['==', '=', 'eq'], true)
) {
return new ConstantIntegerType($parsedVersion->getVersionId());
}

return TypeCombinator::remove(new IntegerType(), new ConstantIntegerType($parsedVersion->getVersionId()));
}

public function toFiberScope(): self
{
if (PHP_VERSION_ID < 80100) {
Expand Down Expand Up @@ -3359,6 +3406,49 @@ public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType,
}

$scope = $this;
if ($expr instanceof FuncCall && $expr->name instanceof Name) {
$args = $expr->getArgs();
if (count($args) >= 2) {
$functionName = $this->reflectionProvider->resolveFunctionName($expr->name, $this);
if ($functionName === 'version_compare') {
$version1 = $args[0]->value;
$version2 = $args[1]->value;

if (
$version1 instanceof ConstFetch
&& $version1->name->name === 'PHP_VERSION'
&& $version2 instanceof String_
) {
$integerVersionRange = $this->getComparePhpVersionType($version2->value, isset($args[2]) ? $args[2]->value : null);
if ($integerVersionRange !== null) {
$scope = $scope->specifyExpressionType(
new ConstFetch(new Name('\\PHP_VERSION_ID')),
$integerVersionRange,
$integerVersionRange,
TrinaryLogic::createYes(),
);
}
}

if (
$version2 instanceof ConstFetch
&& $version2->name->name === 'PHP_VERSION'
&& $version1 instanceof String_
) {
$integerVersionRange = $this->getComparePhpVersionType($version1->value, isset($args[2]) ? $args[2]->value : null);
if ($integerVersionRange !== null) {
$scope = $scope->specifyExpressionType(
new ConstFetch(new Name('\\PHP_VERSION_ID')),
$integerVersionRange,
$integerVersionRange,
TrinaryLogic::createYes(),
);
}
}
}
}
}

if (
$expr instanceof Expr\ArrayDimFetch
&& $expr->dim !== null
Expand Down
26 changes: 26 additions & 0 deletions src/Php/SimplePhpVersionParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace PHPStan\Php;

use Nette\Utils\Strings;
use function sprintf;

final class SimplePhpVersionParser
{

public static function parseVersion(string $version): ?PhpVersion
{
$matches = Strings::match($version, '#^(\d+)\.(\d+)(?:\.(\d+))?#');
if ($matches === null) {
return null;
}

$major = $matches[1];
$minor = $matches[2] ?? 0;
$patch = $matches[3] ?? 0;
$versionId = (int) sprintf('%d%02d%02d', $major, $minor, $patch);

return new PhpVersion($versionId);
}

}
142 changes: 142 additions & 0 deletions tests/PHPStan/Analyser/nsrt/narrow-phpversionid-on-version-compare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace Bug162;

use function PHPStan\Testing\assertType;
use const PHP_VERSION;
use const PHP_VERSION_ID;

function lower(): void
{
if (
version_compare( PHP_VERSION, '8.4', '<' )
) {
$x = PHP_VERSION_ID;
assertType('int<min, 80399>', $x);
}

if (
version_compare( PHP_VERSION, '8.4', 'lt' )
) {
$x = PHP_VERSION_ID;
assertType('int<min, 80399>', $x);
}

if (
version_compare( PHP_VERSION, '8.4', '<=' )
) {
$x = PHP_VERSION_ID;
assertType('int<min, 80400>', $x);
}

if (
version_compare( PHP_VERSION, '8.4', 'le' )
) {
$x = PHP_VERSION_ID;
assertType('int<min, 80400>', $x);
}
}

function greater(): void
{
if (
version_compare( PHP_VERSION, '8.4', '>' )
) {
$x = PHP_VERSION_ID;
assertType('int<80401, max>', $x);
}

if (
version_compare( PHP_VERSION, '8.4', 'gt' )
) {
$x = PHP_VERSION_ID;
assertType('int<80401, max>', $x);
}

if (
version_compare( PHP_VERSION, '8.4', '>=' )
) {
$x = PHP_VERSION_ID;
assertType('int<80400, max>', $x);
}
if (
version_compare( PHP_VERSION, '8.4', 'ge' )
) {
$x = PHP_VERSION_ID;
assertType('int<80400, max>', $x);
}
}

function equal(): void
{
if (
version_compare( PHP_VERSION, '8.4', '=' )
) {
$x = PHP_VERSION_ID;
assertType('80400', $x);
}

if (
version_compare( PHP_VERSION, '8.4', '==' )
) {
$x = PHP_VERSION_ID;
assertType('80400', $x);
}

if (
version_compare( PHP_VERSION, '8.4', 'eq' )
) {
$x = PHP_VERSION_ID;
assertType('80400', $x);
}
}


function not(): void
{
if (
version_compare( PHP_VERSION, '8.4', '!=' )
) {
$x = PHP_VERSION_ID;
assertType('int<min, 80399>|int<80401, max>', $x);
}

if (
version_compare( PHP_VERSION, '8.4', '<>' )
) {
$x = PHP_VERSION_ID;
assertType('int<min, 80399>|int<80401, max>', $x);
}

if (
version_compare( PHP_VERSION, '8.4', 'ne' )
) {
$x = PHP_VERSION_ID;
assertType('int<min, 80399>|int<80401, max>', $x);
}
}

function inverseOperandLower(): void
{
if (
version_compare( '8.3.12', PHP_VERSION, '<' )
) {
$x = PHP_VERSION_ID;
assertType('int<min, 80311>', $x);
}

if (
version_compare( '8.3.12', PHP_VERSION, '>=' )
) {
$x = PHP_VERSION_ID;
assertType('int<80312, max>', $x);
}

if (
version_compare( '8.3.12', PHP_VERSION, '>' )
) {
$x = PHP_VERSION_ID;
assertType('int<80313, max>', $x);
}

}
Loading