Skip to content

Commit f6c556f

Browse files
authored
Scope: use scope->getConstant instead
1 parent 90e48fa commit f6c556f

File tree

5 files changed

+93
-13
lines changed

5 files changed

+93
-13
lines changed

src/Analyser/MutatingScope.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -610,12 +610,7 @@ public function hasConstant(Name $name): bool
610610
return $this->fileHasCompilerHaltStatementCalls();
611611
}
612612

613-
if (!$name->isFullyQualified() && $this->getNamespace() !== null) {
614-
if ($this->hasExpressionType(new ConstFetch(new FullyQualified([$this->getNamespace(), $name->toString()])))->yes()) {
615-
return true;
616-
}
617-
}
618-
if ($this->hasExpressionType(new ConstFetch(new FullyQualified($name->toString())))->yes()) {
613+
if ($this->getGlobalConstantType($name) !== null) {
619614
return true;
620615
}
621616

@@ -5686,6 +5681,25 @@ private function getConstantTypes(): array
56865681
return $constantTypes;
56875682
}
56885683

5684+
private function getGlobalConstantType(Name $name): ?Type
5685+
{
5686+
$fetches = [];
5687+
if (!$name->isFullyQualified() && $this->getNamespace() !== null) {
5688+
$fetches[] = new ConstFetch(new FullyQualified([$this->getNamespace(), $name->toString()]));
5689+
}
5690+
5691+
$fetches[] = new ConstFetch(new FullyQualified($name->toString()));
5692+
$fetches[] = new ConstFetch($name);
5693+
5694+
foreach ($fetches as $constFetch) {
5695+
if ($this->hasExpressionType($constFetch)->yes()) {
5696+
return $this->getType($constFetch);
5697+
}
5698+
}
5699+
5700+
return null;
5701+
}
5702+
56895703
/**
56905704
* @return array<string, ExpressionTypeHolder>
56915705
*/
@@ -5728,15 +5742,15 @@ public function getIterableValueType(Type $iteratee): Type
57285742

57295743
public function getPhpVersion(): PhpVersions
57305744
{
5731-
$versionExpr = new ConstFetch(new Name('PHP_VERSION_ID'));
5732-
if (!$this->hasExpressionType($versionExpr)->yes()) {
5733-
if (is_array($this->configPhpVersion)) {
5734-
return new PhpVersions(IntegerRangeType::fromInterval($this->configPhpVersion['min'], $this->configPhpVersion['max']));
5735-
}
5736-
return new PhpVersions(new ConstantIntegerType($this->phpVersion->getVersionId()));
5745+
$constType = $this->getGlobalConstantType(new Name('PHP_VERSION_ID'));
5746+
if ($constType !== null) {
5747+
return new PhpVersions($constType);
57375748
}
57385749

5739-
return new PhpVersions($this->getType($versionExpr));
5750+
if (is_array($this->configPhpVersion)) {
5751+
return new PhpVersions(IntegerRangeType::fromInterval($this->configPhpVersion['min'], $this->configPhpVersion['max']));
5752+
}
5753+
return new PhpVersions(new ConstantIntegerType($this->phpVersion->getVersionId()));
57405754
}
57415755

57425756
}

src/Php/PhpVersions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public function __construct(
1818
{
1919
}
2020

21+
public function getType(): Type
22+
{
23+
return $this->phpVersions;
24+
}
25+
2126
public function supportsNoncapturingCatches(): TrinaryLogic
2227
{
2328
return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\Exit_;
7+
use PHPStan\Testing\TypeInferenceTestCase;
8+
use PHPStan\Type\VerbosityLevel;
9+
10+
class ScopePhpVersionTest extends TypeInferenceTestCase
11+
{
12+
13+
public function dataTestPhpVersion(): array
14+
{
15+
return [
16+
[
17+
'int<80000, 80499>',
18+
__DIR__ . '/data/scope-constants-global.php',
19+
],
20+
[
21+
'int<80000, 80499>',
22+
__DIR__ . '/data/scope-constants-namespace.php',
23+
],
24+
];
25+
}
26+
27+
/**
28+
* @dataProvider dataTestPhpVersion
29+
*/
30+
public function testPhpVersion(string $expected, string $file): void
31+
{
32+
self::processFile($file, function (Node $node, Scope $scope) use ($expected): void {
33+
if (!($node instanceof Exit_)) {
34+
return;
35+
}
36+
$this->assertSame(
37+
$expected,
38+
$scope->getPhpVersion()->getType()->describe(VerbosityLevel::precise()),
39+
);
40+
});
41+
}
42+
43+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
// global namespace required for the test to work
4+
5+
if (PHP_VERSION_ID < 80000) {
6+
return;
7+
}
8+
9+
exit();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace NamespacedConstants;
4+
5+
if (PHP_VERSION_ID < 80000) {
6+
return;
7+
}
8+
9+
exit();

0 commit comments

Comments
 (0)