Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Scope::getMaybeDefinedVariables #3521

Merged
merged 3 commits into from
Oct 2, 2024
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
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>
Copy link
Contributor

Choose a reason for hiding this comment

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

why not list<string> ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copied from getDefinedVars

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great 🎉

*/
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());
}

}
Loading