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
4 changes: 4 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,10 @@ jobs:
cd e2e/composer-version-config
composer install
../../bin/phpstan analyze test.php --level=0
- script: |
cd e2e/version-compare-phpversionid
composer install
../../bin/phpstan analyze narrow-phpversionid-on-version-compare.php --level=0
- script: |
cd e2e/bug13425
timeout 15 ../bashunit assert exit_code "1" "../../bin/phpstan analyze src/ plugins/"
Expand Down
2 changes: 2 additions & 0 deletions e2e/version-compare-phpversionid/.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/version-compare-phpversionid/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": "^8"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

// intentional without namespace

use function PHPStan\Testing\assertType;

function narrowPhpVersionIdViaVersionComapre(): void {
if (PHP_VERSION_ID < 80000) {
return;
}
$x = PHP_VERSION_ID;
assertType('int<80000, max>', $x);

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php

namespace Bug162;

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

function lower(): void
{
// add a upper bound, so we don't need to adjust
// the test when PHPStan adds support for PHP8.6+
if (PHP_VERSION_ID > 80599) {
return;
}

// lower limit inferred from composer.json
$x = PHP_VERSION_ID;
assertType('int<80000, 80599>', $x);

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

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

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

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

function greater(): void
{
// add a upper bound, so we don't need to adjust
// the test when PHPStan adds support for PHP8.6+
if (PHP_VERSION_ID > 80599) {
return;
}

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

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

if (
version_compare( PHP_VERSION, '8.4', '>=' )
) {
$x = PHP_VERSION_ID;
assertType('int<80400, 80599>', $x);
}
if (
version_compare( PHP_VERSION, '8.4', 'ge' )
) {
$x = PHP_VERSION_ID;
assertType('int<80400, 80599>', $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
{
// add a upper bound, so we don't need to adjust
// the test when PHPStan adds support for PHP8.6+
if (PHP_VERSION_ID > 80599) {
return;
}

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

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

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

function inverseOperandLower(): void
{
// add a upper bound, so we don't need to adjust
// the test when PHPStan adds support for PHP8.6+
if (PHP_VERSION_ID > 80599) {
return;
}

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

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

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

function narrow(): void {
if (PHP_VERSION_ID < 80000) {
return;
}

if (
version_compare( PHP_VERSION, '8.4', '<' )
) {
$x = PHP_VERSION_ID;
assertType('int<80000, 80399>', $x);
}
}
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 = (int) $matches[1];
$minor = (int) $matches[2];
$patch = (int) ($matches[3] ?? 0);
$versionId = (int) sprintf('%d%02d%02d', $major, $minor, $patch);

return new PhpVersion($versionId);
}

}
Loading
Loading