Skip to content

Commit

Permalink
Improve types for predefined constants
Browse files Browse the repository at this point in the history
  • Loading branch information
zonuexe committed May 9, 2024
1 parent 4c8f3c6 commit 173587f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
34 changes: 17 additions & 17 deletions src/Analyser/ConstantResolver.php
Expand Up @@ -6,7 +6,7 @@
use PHPStan\Reflection\NamespaceAnswerer;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Reflection\ReflectionProvider\ReflectionProviderProvider;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
Expand Down Expand Up @@ -73,7 +73,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
if ($resolvedConstantName === 'PHP_VERSION') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_MAJOR_VERSION') {
Expand Down Expand Up @@ -106,7 +106,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
if ($resolvedConstantName === 'PHP_OS') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_OS_FAMILY') {
Expand All @@ -132,7 +132,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
new ConstantStringType('phpdbg'),
new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]),
]);
}
Expand Down Expand Up @@ -165,61 +165,61 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
if ($resolvedConstantName === 'PHP_EXTENSION_DIR') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_PREFIX') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_BINDIR') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_BINARY') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_MANDIR') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_LIBDIR') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_DATADIR') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_SYSCONFDIR') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_LOCALSTATEDIR') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_CONFIG_FILE_PATH') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_SHLIB_SUFFIX') {
Expand All @@ -232,7 +232,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
return IntegerRangeType::fromInterval(1, null);
}
if ($resolvedConstantName === '__COMPILER_HALT_OFFSET__') {
return IntegerRangeType::fromInterval(0, null);
return IntegerRangeType::fromInterval(1, null);
}
// core other, https://www.php.net/manual/en/info.constants.php
if ($resolvedConstantName === 'PHP_WINDOWS_VERSION_MAJOR') {
Expand Down Expand Up @@ -261,7 +261,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
if ($resolvedConstantName === 'ICONV_IMPL') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]);
}
// libxml, https://www.php.net/manual/en/libxml.constants.php
Expand All @@ -271,7 +271,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
if ($resolvedConstantName === 'LIBXML_DOTTED_VERSION') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryNonFalsyStringType(),
]);
}
// openssl, https://www.php.net/manual/en/openssl.constants.php
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/ScopeTest.php
Expand Up @@ -245,7 +245,7 @@ public function testGetConstantType(): void
$scope = $scopeFactory->create(ScopeContext::create(__DIR__ . '/data/compiler-halt-offset.php'));
$node = new ConstFetch(new FullyQualified('__COMPILER_HALT_OFFSET__'));
$type = $scope->getType($node);
$this->assertSame('int<0, max>', $type->describe(VerbosityLevel::precise()));
$this->assertSame('int<1, max>', $type->describe(VerbosityLevel::precise()));
}

}
32 changes: 16 additions & 16 deletions tests/PHPStan/Analyser/data/predefined-constants.php
Expand Up @@ -3,7 +3,7 @@
use function PHPStan\Testing\assertType;

// core, https://www.php.net/manual/en/reserved.constants.php
assertType('non-empty-string', PHP_VERSION);
assertType('non-falsy-string', PHP_VERSION);
assertType('int<5, max>', PHP_MAJOR_VERSION);
assertType('int<0, max>', PHP_MINOR_VERSION);
assertType('int<0, max>', PHP_RELEASE_VERSION);
Expand All @@ -12,23 +12,23 @@
assertType('0|1', PHP_ZTS);
assertType('0|1', PHP_DEBUG);
assertType('int<1, max>', PHP_MAXPATHLEN);
assertType('non-empty-string', PHP_OS);
assertType('\'apache\'|\'apache2handler\'|\'cgi\'|\'cli\'|\'cli-server\'|\'embed\'|\'fpm-fcgi\'|\'litespeed\'|\'phpdbg\'|non-empty-string', PHP_SAPI);
assertType('non-falsy-string', PHP_OS);
assertType('\'apache\'|\'apache2handler\'|\'cgi\'|\'cli\'|\'cli-server\'|\'embed\'|\'fpm-fcgi\'|\'litespeed\'|\'phpdbg\'|non-falsy-string', PHP_SAPI);
assertType('"\n"|"\r\n"', PHP_EOL);
assertType('4|8', PHP_INT_SIZE);
assertType('string', DEFAULT_INCLUDE_PATH);
assertType('string', PEAR_INSTALL_DIR);
assertType('string', PEAR_EXTENSION_DIR);
assertType('non-empty-string', PHP_EXTENSION_DIR);
assertType('non-empty-string', PHP_PREFIX);
assertType('non-empty-string', PHP_BINDIR);
assertType('non-empty-string', PHP_BINARY);
assertType('non-empty-string', PHP_MANDIR);
assertType('non-empty-string', PHP_LIBDIR);
assertType('non-empty-string', PHP_DATADIR);
assertType('non-empty-string', PHP_SYSCONFDIR);
assertType('non-empty-string', PHP_LOCALSTATEDIR);
assertType('non-empty-string', PHP_CONFIG_FILE_PATH);
assertType('non-falsy-string', PHP_EXTENSION_DIR);
assertType('non-falsy-string', PHP_PREFIX);
assertType('non-falsy-string', PHP_BINDIR);
assertType('non-falsy-string', PHP_BINARY);
assertType('non-falsy-string', PHP_MANDIR);
assertType('non-falsy-string', PHP_LIBDIR);
assertType('non-falsy-string', PHP_DATADIR);
assertType('non-falsy-string', PHP_SYSCONFDIR);
assertType('non-falsy-string', PHP_LOCALSTATEDIR);
assertType('non-falsy-string', PHP_CONFIG_FILE_PATH);
assertType('string', PHP_CONFIG_FILE_SCAN_DIR);
assertType('\'dll\'|\'so\'', PHP_SHLIB_SUFFIX);
assertType('1', E_ERROR);
Expand All @@ -47,7 +47,7 @@
assertType('16384', E_USER_DEPRECATED);
assertType('32767', E_ALL);
assertType('2048', E_STRICT);
assertType('int<0, max>', __COMPILER_HALT_OFFSET__);
assertType('int<1, max>', __COMPILER_HALT_OFFSET__);
assertType('true', true);
assertType('false', false);
assertType('null', null);
Expand All @@ -62,11 +62,11 @@
assertType('\':\'|\';\'', PATH_SEPARATOR);

// iconv, https://www.php.net/manual/en/iconv.constants.php
assertType('non-empty-string', ICONV_IMPL);
assertType('non-falsy-string', ICONV_IMPL);

// libxml, https://www.php.net/manual/en/libxml.constants.php
assertType('int<1, max>', LIBXML_VERSION);
assertType('non-empty-string', LIBXML_DOTTED_VERSION);
assertType('non-falsy-string', LIBXML_DOTTED_VERSION);

// openssl, https://www.php.net/manual/en/openssl.constants.php
assertType('int<1, max>', OPENSSL_VERSION_NUMBER);
Expand Down

0 comments on commit 173587f

Please sign in to comment.