Skip to content

Commit

Permalink
Introduce Scope::getMaybeDefinedVariables()
Browse files Browse the repository at this point in the history
  • Loading branch information
ruudk authored Oct 2, 2024
1 parent 0b9ce98 commit 64ed7dc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,27 @@ public function getDefinedVariables(): array
return $variables;
}

/**
* @api
* @return array<int, string>
*/
public function getMaybeDefinedVariables(): array
{
$variables = [];
foreach ($this->expressionTypes as $exprString => $holder) {
if (!$holder->getExpr() instanceof Variable) {
continue;
}
if (!$holder->getCertainty()->maybe()) {
continue;
}

$variables[] = substr($exprString, 1);
}

return $variables;
}

private function isGlobalVariable(string $variableName): bool
{
return in_array($variableName, self::SUPERGLOBAL_VARIABLES, true);
Expand Down
5 changes: 5 additions & 0 deletions src/Analyser/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public function canAnyVariableExist(): bool;
*/
public function getDefinedVariables(): array;

/**
* @return array<int, string>
*/
public function getMaybeDefinedVariables(): array;

public function hasConstant(Name $name): bool;

public function getPropertyReflection(Type $typeWithProperty, string $propertyName): ?ExtendedPropertyReflection;
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Analyser/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;

/**
* @covers \PHPStan\Analyser\MutatingScope
*/
class ScopeTest extends PHPStanTestCase
{

Expand Down Expand Up @@ -248,4 +253,26 @@ public function testGetConstantType(): void
$this->assertSame('int<1, max>', $type->describe(VerbosityLevel::precise()));
}

public function testDefinedVariables(): void
{
/** @var ScopeFactory $scopeFactory */
$scopeFactory = self::getContainer()->getByType(ScopeFactory::class);
$scope = $scopeFactory->create(ScopeContext::create('file.php'))
->assignVariable('a', new ConstantStringType('a'), new StringType(), TrinaryLogic::createYes())
->assignVariable('b', new ConstantStringType('b'), new StringType(), TrinaryLogic::createMaybe());

$this->assertSame(['a'], $scope->getDefinedVariables());
}

public function testMaybeDefinedVariables(): void
{
/** @var ScopeFactory $scopeFactory */
$scopeFactory = self::getContainer()->getByType(ScopeFactory::class);
$scope = $scopeFactory->create(ScopeContext::create('file.php'))
->assignVariable('a', new ConstantStringType('a'), new StringType(), TrinaryLogic::createYes())
->assignVariable('b', new ConstantStringType('b'), new StringType(), TrinaryLogic::createMaybe());

$this->assertSame(['b'], $scope->getMaybeDefinedVariables());
}

}

0 comments on commit 64ed7dc

Please sign in to comment.