Skip to content

Commit 173587f

Browse files
authored
Improve types for predefined constants
1 parent 4c8f3c6 commit 173587f

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

src/Analyser/ConstantResolver.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use PHPStan\Reflection\NamespaceAnswerer;
77
use PHPStan\Reflection\ReflectionProvider;
88
use PHPStan\Reflection\ReflectionProvider\ReflectionProviderProvider;
9-
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
9+
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
1010
use PHPStan\Type\Constant\ConstantFloatType;
1111
use PHPStan\Type\Constant\ConstantIntegerType;
1212
use PHPStan\Type\Constant\ConstantStringType;
@@ -73,7 +73,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
7373
if ($resolvedConstantName === 'PHP_VERSION') {
7474
return new IntersectionType([
7575
new StringType(),
76-
new AccessoryNonEmptyStringType(),
76+
new AccessoryNonFalsyStringType(),
7777
]);
7878
}
7979
if ($resolvedConstantName === 'PHP_MAJOR_VERSION') {
@@ -106,7 +106,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
106106
if ($resolvedConstantName === 'PHP_OS') {
107107
return new IntersectionType([
108108
new StringType(),
109-
new AccessoryNonEmptyStringType(),
109+
new AccessoryNonFalsyStringType(),
110110
]);
111111
}
112112
if ($resolvedConstantName === 'PHP_OS_FAMILY') {
@@ -132,7 +132,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
132132
new ConstantStringType('phpdbg'),
133133
new IntersectionType([
134134
new StringType(),
135-
new AccessoryNonEmptyStringType(),
135+
new AccessoryNonFalsyStringType(),
136136
]),
137137
]);
138138
}
@@ -165,61 +165,61 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
165165
if ($resolvedConstantName === 'PHP_EXTENSION_DIR') {
166166
return new IntersectionType([
167167
new StringType(),
168-
new AccessoryNonEmptyStringType(),
168+
new AccessoryNonFalsyStringType(),
169169
]);
170170
}
171171
if ($resolvedConstantName === 'PHP_PREFIX') {
172172
return new IntersectionType([
173173
new StringType(),
174-
new AccessoryNonEmptyStringType(),
174+
new AccessoryNonFalsyStringType(),
175175
]);
176176
}
177177
if ($resolvedConstantName === 'PHP_BINDIR') {
178178
return new IntersectionType([
179179
new StringType(),
180-
new AccessoryNonEmptyStringType(),
180+
new AccessoryNonFalsyStringType(),
181181
]);
182182
}
183183
if ($resolvedConstantName === 'PHP_BINARY') {
184184
return new IntersectionType([
185185
new StringType(),
186-
new AccessoryNonEmptyStringType(),
186+
new AccessoryNonFalsyStringType(),
187187
]);
188188
}
189189
if ($resolvedConstantName === 'PHP_MANDIR') {
190190
return new IntersectionType([
191191
new StringType(),
192-
new AccessoryNonEmptyStringType(),
192+
new AccessoryNonFalsyStringType(),
193193
]);
194194
}
195195
if ($resolvedConstantName === 'PHP_LIBDIR') {
196196
return new IntersectionType([
197197
new StringType(),
198-
new AccessoryNonEmptyStringType(),
198+
new AccessoryNonFalsyStringType(),
199199
]);
200200
}
201201
if ($resolvedConstantName === 'PHP_DATADIR') {
202202
return new IntersectionType([
203203
new StringType(),
204-
new AccessoryNonEmptyStringType(),
204+
new AccessoryNonFalsyStringType(),
205205
]);
206206
}
207207
if ($resolvedConstantName === 'PHP_SYSCONFDIR') {
208208
return new IntersectionType([
209209
new StringType(),
210-
new AccessoryNonEmptyStringType(),
210+
new AccessoryNonFalsyStringType(),
211211
]);
212212
}
213213
if ($resolvedConstantName === 'PHP_LOCALSTATEDIR') {
214214
return new IntersectionType([
215215
new StringType(),
216-
new AccessoryNonEmptyStringType(),
216+
new AccessoryNonFalsyStringType(),
217217
]);
218218
}
219219
if ($resolvedConstantName === 'PHP_CONFIG_FILE_PATH') {
220220
return new IntersectionType([
221221
new StringType(),
222-
new AccessoryNonEmptyStringType(),
222+
new AccessoryNonFalsyStringType(),
223223
]);
224224
}
225225
if ($resolvedConstantName === 'PHP_SHLIB_SUFFIX') {
@@ -232,7 +232,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
232232
return IntegerRangeType::fromInterval(1, null);
233233
}
234234
if ($resolvedConstantName === '__COMPILER_HALT_OFFSET__') {
235-
return IntegerRangeType::fromInterval(0, null);
235+
return IntegerRangeType::fromInterval(1, null);
236236
}
237237
// core other, https://www.php.net/manual/en/info.constants.php
238238
if ($resolvedConstantName === 'PHP_WINDOWS_VERSION_MAJOR') {
@@ -261,7 +261,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
261261
if ($resolvedConstantName === 'ICONV_IMPL') {
262262
return new IntersectionType([
263263
new StringType(),
264-
new AccessoryNonEmptyStringType(),
264+
new AccessoryNonFalsyStringType(),
265265
]);
266266
}
267267
// libxml, https://www.php.net/manual/en/libxml.constants.php
@@ -271,7 +271,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
271271
if ($resolvedConstantName === 'LIBXML_DOTTED_VERSION') {
272272
return new IntersectionType([
273273
new StringType(),
274-
new AccessoryNonEmptyStringType(),
274+
new AccessoryNonFalsyStringType(),
275275
]);
276276
}
277277
// openssl, https://www.php.net/manual/en/openssl.constants.php

tests/PHPStan/Analyser/ScopeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public function testGetConstantType(): void
245245
$scope = $scopeFactory->create(ScopeContext::create(__DIR__ . '/data/compiler-halt-offset.php'));
246246
$node = new ConstFetch(new FullyQualified('__COMPILER_HALT_OFFSET__'));
247247
$type = $scope->getType($node);
248-
$this->assertSame('int<0, max>', $type->describe(VerbosityLevel::precise()));
248+
$this->assertSame('int<1, max>', $type->describe(VerbosityLevel::precise()));
249249
}
250250

251251
}

tests/PHPStan/Analyser/data/predefined-constants.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use function PHPStan\Testing\assertType;
44

55
// core, https://www.php.net/manual/en/reserved.constants.php
6-
assertType('non-empty-string', PHP_VERSION);
6+
assertType('non-falsy-string', PHP_VERSION);
77
assertType('int<5, max>', PHP_MAJOR_VERSION);
88
assertType('int<0, max>', PHP_MINOR_VERSION);
99
assertType('int<0, max>', PHP_RELEASE_VERSION);
@@ -12,23 +12,23 @@
1212
assertType('0|1', PHP_ZTS);
1313
assertType('0|1', PHP_DEBUG);
1414
assertType('int<1, max>', PHP_MAXPATHLEN);
15-
assertType('non-empty-string', PHP_OS);
16-
assertType('\'apache\'|\'apache2handler\'|\'cgi\'|\'cli\'|\'cli-server\'|\'embed\'|\'fpm-fcgi\'|\'litespeed\'|\'phpdbg\'|non-empty-string', PHP_SAPI);
15+
assertType('non-falsy-string', PHP_OS);
16+
assertType('\'apache\'|\'apache2handler\'|\'cgi\'|\'cli\'|\'cli-server\'|\'embed\'|\'fpm-fcgi\'|\'litespeed\'|\'phpdbg\'|non-falsy-string', PHP_SAPI);
1717
assertType('"\n"|"\r\n"', PHP_EOL);
1818
assertType('4|8', PHP_INT_SIZE);
1919
assertType('string', DEFAULT_INCLUDE_PATH);
2020
assertType('string', PEAR_INSTALL_DIR);
2121
assertType('string', PEAR_EXTENSION_DIR);
22-
assertType('non-empty-string', PHP_EXTENSION_DIR);
23-
assertType('non-empty-string', PHP_PREFIX);
24-
assertType('non-empty-string', PHP_BINDIR);
25-
assertType('non-empty-string', PHP_BINARY);
26-
assertType('non-empty-string', PHP_MANDIR);
27-
assertType('non-empty-string', PHP_LIBDIR);
28-
assertType('non-empty-string', PHP_DATADIR);
29-
assertType('non-empty-string', PHP_SYSCONFDIR);
30-
assertType('non-empty-string', PHP_LOCALSTATEDIR);
31-
assertType('non-empty-string', PHP_CONFIG_FILE_PATH);
22+
assertType('non-falsy-string', PHP_EXTENSION_DIR);
23+
assertType('non-falsy-string', PHP_PREFIX);
24+
assertType('non-falsy-string', PHP_BINDIR);
25+
assertType('non-falsy-string', PHP_BINARY);
26+
assertType('non-falsy-string', PHP_MANDIR);
27+
assertType('non-falsy-string', PHP_LIBDIR);
28+
assertType('non-falsy-string', PHP_DATADIR);
29+
assertType('non-falsy-string', PHP_SYSCONFDIR);
30+
assertType('non-falsy-string', PHP_LOCALSTATEDIR);
31+
assertType('non-falsy-string', PHP_CONFIG_FILE_PATH);
3232
assertType('string', PHP_CONFIG_FILE_SCAN_DIR);
3333
assertType('\'dll\'|\'so\'', PHP_SHLIB_SUFFIX);
3434
assertType('1', E_ERROR);
@@ -47,7 +47,7 @@
4747
assertType('16384', E_USER_DEPRECATED);
4848
assertType('32767', E_ALL);
4949
assertType('2048', E_STRICT);
50-
assertType('int<0, max>', __COMPILER_HALT_OFFSET__);
50+
assertType('int<1, max>', __COMPILER_HALT_OFFSET__);
5151
assertType('true', true);
5252
assertType('false', false);
5353
assertType('null', null);
@@ -62,11 +62,11 @@
6262
assertType('\':\'|\';\'', PATH_SEPARATOR);
6363

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

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

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

0 commit comments

Comments
 (0)