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

Fix wrong PHP_INT_MAX/MIN and PHP_DEBUG PHP 8.4 type #11190

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
use function implode;
use function strtolower;

use const PHP_INT_MAX;
use const PHP_INT_MIN;

/**
* @internal
*/
Expand Down Expand Up @@ -164,13 +167,22 @@ public static function getGlobalConstType(
case 'PHP_MAJOR_VERSION':
case 'PHP_MINOR_VERSION':
case 'PHP_RELEASE_VERSION':
case 'PHP_DEBUG':
case 'PHP_FLOAT_DIG':
return Type::getInt();

case 'PHP_INT_MIN':
return Type::getInt(false, PHP_INT_MIN);

case 'PHP_DEBUG':
case 'PHP_ZTS':
return Type::getInt();
if ($codebase->analysis_php_version_id >= 8_04_00) {
return Type::getBool();
}
return Type::getIntRange(0, 1);

case 'PHP_INT_MAX':
return Type::getInt(false, PHP_INT_MAX);
Copy link
Collaborator

@danog danog Dec 23, 2024

Choose a reason for hiding this comment

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

This and PHP_INT_MIN should not be a literal, instead it should be a generic positive/negative integer, because i.e. running psalm on 32-bit and 64-bit systems will return different results.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

running psalm on 32-bit and 64-bit systems will return different results

It's pointless to support 32-bit in this day and age I think? Windows 11 doesn't have support for 32-bit, PhpStorm isn't available in 32-bit.

Could you pinpoint me on how?
The int value is converted to a float in CI when using the "_" syntax (maybe even without it)

I initially used the int value 37820f0 and this caused things to not work

Copy link
Collaborator

Choose a reason for hiding this comment

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

PHP_INT_MAX is already correct at least on master, PHP_INT_MIN needs Type::getIntRange(null, -1)


case 'PHP_INT_SIZE':
case 'PHP_MAXPATHLEN':
case 'PHP_VERSION_ID':
Expand Down
55 changes: 55 additions & 0 deletions tests/ConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,19 @@ interface BarInterface extends MainInterface {}
class FooBar implements FooInterface, BarInterface {}
PHP,
],
'phpDebugValid' => [
'code' => '<?php
if (PHP_DEBUG === 0) {}
',
],
/*'phpDebugValid84' => [
'code' => '<?php
if (PHP_DEBUG === false) {}
',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.4',
],*/
];
}

Expand Down Expand Up @@ -2724,6 +2737,48 @@ class C {
'error_levels' => [],
'php_version' => '8.2',
],
'phpIntMaxValue' => [
'code' => '<?php
$max = PHP_INT_MAX;
if ($max < 100) {}
',
'error_message' => 'TypeDoesNotContainType',
],
'phpIntMinValue' => [
'code' => '<?php
$min = PHP_INT_MIN;
if ($min > -100) {}
',
'error_message' => 'TypeDoesNotContainType',
],
'phpIntMaxValueRedundant' => [
'code' => '<?php
$max = PHP_INT_MAX;
if ($max > 100) {}
',
'error_message' => 'RedundantCondition',
],
'phpIntMinValueRedundant' => [
'code' => '<?php
$min = PHP_INT_MIN;
if ($min < -100) {}
',
'error_message' => 'RedundantCondition',
],
'phpDebug' => [
'code' => '<?php
if (PHP_DEBUG === false) {}
',
'error_message' => 'TypeDoesNotContainType',
],
/*'phpDebug84' => [
'code' => '<?php
if (PHP_DEBUG === 0) {}
',
'error_message' => 'TypeDoesNotContainType',
'error_levels' => [],
'php_version' => '8.4',
],*/
];
}
}
Loading