Skip to content

Commit b539c74

Browse files
authored
Merge branch refs/heads/1.12.x into 2.0.x
2 parents f38addd + 64ed7dc commit b539c74

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/Analyser/MutatingScope.php

+21
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,27 @@ public function getDefinedVariables(): array
571571
return $variables;
572572
}
573573

574+
/**
575+
* @api
576+
* @return array<int, string>
577+
*/
578+
public function getMaybeDefinedVariables(): array
579+
{
580+
$variables = [];
581+
foreach ($this->expressionTypes as $exprString => $holder) {
582+
if (!$holder->getExpr() instanceof Variable) {
583+
continue;
584+
}
585+
if (!$holder->getCertainty()->maybe()) {
586+
continue;
587+
}
588+
589+
$variables[] = substr($exprString, 1);
590+
}
591+
592+
return $variables;
593+
}
594+
574595
private function isGlobalVariable(string $variableName): bool
575596
{
576597
return in_array($variableName, self::SUPERGLOBAL_VARIABLES, true);

src/Analyser/Scope.php

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public function canAnyVariableExist(): bool;
6767
*/
6868
public function getDefinedVariables(): array;
6969

70+
/**
71+
* @return array<int, string>
72+
*/
73+
public function getMaybeDefinedVariables(): array;
74+
7075
public function hasConstant(Name $name): bool;
7176

7277
public function getPropertyReflection(Type $typeWithProperty, string $propertyName): ?ExtendedPropertyReflection;

tests/PHPStan/Analyser/ScopeTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
use PHPStan\Type\Constant\ConstantStringType;
1313
use PHPStan\Type\IntegerRangeType;
1414
use PHPStan\Type\ObjectType;
15+
use PHPStan\Type\StringType;
1516
use PHPStan\Type\Type;
1617
use PHPStan\Type\UnionType;
1718
use PHPStan\Type\VerbosityLevel;
1819

20+
/**
21+
* @covers \PHPStan\Analyser\MutatingScope
22+
*/
1923
class ScopeTest extends PHPStanTestCase
2024
{
2125

@@ -249,4 +253,26 @@ public function testGetConstantType(): void
249253
$this->assertSame('int<1, max>', $type->describe(VerbosityLevel::precise()));
250254
}
251255

256+
public function testDefinedVariables(): void
257+
{
258+
/** @var ScopeFactory $scopeFactory */
259+
$scopeFactory = self::getContainer()->getByType(ScopeFactory::class);
260+
$scope = $scopeFactory->create(ScopeContext::create('file.php'))
261+
->assignVariable('a', new ConstantStringType('a'), new StringType(), TrinaryLogic::createYes())
262+
->assignVariable('b', new ConstantStringType('b'), new StringType(), TrinaryLogic::createMaybe());
263+
264+
$this->assertSame(['a'], $scope->getDefinedVariables());
265+
}
266+
267+
public function testMaybeDefinedVariables(): void
268+
{
269+
/** @var ScopeFactory $scopeFactory */
270+
$scopeFactory = self::getContainer()->getByType(ScopeFactory::class);
271+
$scope = $scopeFactory->create(ScopeContext::create('file.php'))
272+
->assignVariable('a', new ConstantStringType('a'), new StringType(), TrinaryLogic::createYes())
273+
->assignVariable('b', new ConstantStringType('b'), new StringType(), TrinaryLogic::createMaybe());
274+
275+
$this->assertSame(['b'], $scope->getMaybeDefinedVariables());
276+
}
277+
252278
}

0 commit comments

Comments
 (0)