ConstantCanBeUsed
info on by default quick-fixGroup: Language level migration · PhpStorm name: ConstantCanBeUsedInspection
Some function calls only return a value that PHP already exposes as a constant (PHP_VERSION, PHP_SAPI, M_PI, …), and comparisons of PHP_VERSION via version_compare() or OS sniffing on PHP_OS have cheaper, clearer constant-based equivalents (PHP_VERSION_ID, PHP_OS_FAMILY).
Example
php
<?php
use function pi;
$sapi = php_sapi_name();
$ver = \phpversion();
$self = get_class();
$area = 2 * pi() * $r;
$ext = phpversion('intl');
$host = php_uname('n');
$a = version_compare(PHP_VERSION, '8', 'lt');
$b = version_compare(PHP_VERSION, "7.4.33", 'ge');
$c = version_compare(PHP_VERSION, '8.2.0', 'eq');
$d = version_compare(PHP_VERSION, '5.6.4', '<>');
$e = version_compare(PHP_VERSION, '8.2.0-RC1', '>=');
$f = version_compare(PHP_VERSION, '10.1', '>=');
$g = version_compare(PHP_VERSION, '8.2');
$h = version_compare($other, '8.2', '>=');
$i = version_compare(PHP_VERSION, '8.2', 'GE');
$j = version_compare(PHP_VERSION, '8.2', '==');
class Widget {
public function name() {
return get_class();
}
}php
<?php
use function pi;
$sapi = PHP_SAPI;
$ver = PHP_VERSION;
$self = get_class();
$area = 2 * M_PI * $r;
$ext = phpversion('intl');
$host = php_uname('n');
$a = PHP_VERSION_ID < 80000;
$b = PHP_VERSION_ID >= 70433;
$c = PHP_VERSION_ID === 80200;
$d = PHP_VERSION_ID !== 50604;
$e = version_compare(PHP_VERSION, '8.2.0-RC1', '>=');
$f = version_compare(PHP_VERSION, '10.1', '>=');
$g = version_compare(PHP_VERSION, '8.2');
$h = version_compare($other, '8.2', '>=');
$i = version_compare(PHP_VERSION, '8.2', 'GE');
$j = version_compare(PHP_VERSION, '8.2', '==');
class Widget {
public function name() {
return __CLASS__;
}
}Reported:
- line 4: Use the PHP_SAPI constant instead of this call.
- line 5: Use the PHP_VERSION constant instead of this call.
- line 7: Use the M_PI constant instead of this call.
- line 12: Replace with 'PHP_VERSION_ID < 80000'.
- line 13: Replace with 'PHP_VERSION_ID >= 70433'.
- line 14: Replace with 'PHP_VERSION_ID === 80200'.
- line 15: Replace with 'PHP_VERSION_ID !== 50604'.
- line 26: Use the __CLASS__ constant instead of this call.
Configure
In custos.json:
json
{
"rules": {
"ConstantCanBeUsed": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore ConstantCanBeUsed
/**
* @noinspection ConstantCanBeUsedInspection
*/