Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()) {
Comment on lines -5731 to -5732
Copy link
Contributor Author

@staabm staabm Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this previous code does not work in files without a namespace, e.g.

<?php // lint >= 7.4

if (PHP_VERSION_ID < 80000) {
	\PHPStan\dumpType(PHP_VERSION_ID);
}

the dumpType returns the correct value, but the scope uses a different expression name

$scope->debug();
array (
  '\\PHP_VERSION_ID (Yes)' => 'int<50207, 79999>',
  'native \\PHP_VERSION_ID (Yes)' => 'int<50207, 79999>',
)

vs. (note: file contains a namespace)

<?php // lint >= 7.4

namespace ANamespace;

if (PHP_VERSION_ID < 80000) {
	\PHPStan\dumpType(PHP_VERSION_ID);
}
$scope->debug();
array (
  'PHP_VERSION_ID (Yes)' => 'int<50207, 79999>',
  'native PHP_VERSION_ID (Yes)' => 'int<50207, 79999>',
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't \PHP_VERSION_ID const fetch enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, thats the point of this PR

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();
Loading