Skip to content

Commit

Permalink
Scope: use scope->getConstant instead
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Dec 20, 2024
1 parent 90e48fa commit f6c556f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,12 +610,7 @@ public function hasConstant(Name $name): bool
return $this->fileHasCompilerHaltStatementCalls();
}

if (!$name->isFullyQualified() && $this->getNamespace() !== null) {
if ($this->hasExpressionType(new ConstFetch(new FullyQualified([$this->getNamespace(), $name->toString()])))->yes()) {
return true;
}
}
if ($this->hasExpressionType(new ConstFetch(new FullyQualified($name->toString())))->yes()) {
if ($this->getGlobalConstantType($name) !== null) {
return true;
}

Expand Down Expand Up @@ -5686,6 +5681,25 @@ private function getConstantTypes(): array
return $constantTypes;
}

private function getGlobalConstantType(Name $name): ?Type
{
$fetches = [];
if (!$name->isFullyQualified() && $this->getNamespace() !== null) {
$fetches[] = new ConstFetch(new FullyQualified([$this->getNamespace(), $name->toString()]));
}

$fetches[] = new ConstFetch(new FullyQualified($name->toString()));
$fetches[] = new ConstFetch($name);

foreach ($fetches as $constFetch) {
if ($this->hasExpressionType($constFetch)->yes()) {
return $this->getType($constFetch);
}
}

return null;
}

/**
* @return array<string, ExpressionTypeHolder>
*/
Expand Down Expand Up @@ -5728,15 +5742,15 @@ public function getIterableValueType(Type $iteratee): Type

public function getPhpVersion(): PhpVersions
{
$versionExpr = new ConstFetch(new Name('PHP_VERSION_ID'));
if (!$this->hasExpressionType($versionExpr)->yes()) {
if (is_array($this->configPhpVersion)) {
return new PhpVersions(IntegerRangeType::fromInterval($this->configPhpVersion['min'], $this->configPhpVersion['max']));
}
return new PhpVersions(new ConstantIntegerType($this->phpVersion->getVersionId()));
$constType = $this->getGlobalConstantType(new Name('PHP_VERSION_ID'));
if ($constType !== null) {
return new PhpVersions($constType);
}

return new PhpVersions($this->getType($versionExpr));
if (is_array($this->configPhpVersion)) {
return new PhpVersions(IntegerRangeType::fromInterval($this->configPhpVersion['min'], $this->configPhpVersion['max']));
}
return new PhpVersions(new ConstantIntegerType($this->phpVersion->getVersionId()));
}

}
5 changes: 5 additions & 0 deletions src/Php/PhpVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function __construct(
{
}

public function getType(): Type
{
return $this->phpVersions;
}

public function supportsNoncapturingCatches(): TrinaryLogic
{
return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
Expand Down
43 changes: 43 additions & 0 deletions tests/PHPStan/Analyser/ScopePhpVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PhpParser\Node;
use PhpParser\Node\Expr\Exit_;
use PHPStan\Testing\TypeInferenceTestCase;
use PHPStan\Type\VerbosityLevel;

class ScopePhpVersionTest extends TypeInferenceTestCase
{

public function dataTestPhpVersion(): array
{
return [
[
'int<80000, 80499>',
__DIR__ . '/data/scope-constants-global.php',
],
[
'int<80000, 80499>',
__DIR__ . '/data/scope-constants-namespace.php',
],
];
}

/**
* @dataProvider dataTestPhpVersion
*/
public function testPhpVersion(string $expected, string $file): void
{
self::processFile($file, function (Node $node, Scope $scope) use ($expected): void {
if (!($node instanceof Exit_)) {
return;
}
$this->assertSame(
$expected,
$scope->getPhpVersion()->getType()->describe(VerbosityLevel::precise()),
);
});
}

}
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/data/scope-constants-global.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

// global namespace required for the test to work

if (PHP_VERSION_ID < 80000) {
return;
}

exit();
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/data/scope-constants-namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace NamespacedConstants;

if (PHP_VERSION_ID < 80000) {
return;
}

exit();

0 comments on commit f6c556f

Please sign in to comment.