Skip to content

Commit

Permalink
Override variable certainty in global scope with @var
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 20, 2020
1 parent 7ea7ce4 commit 44f9d08
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
use PHPStan\Reflection\Php\DummyParameter;
use PHPStan\Reflection\Php\PhpMethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\CallableType;
Expand Down Expand Up @@ -2457,6 +2458,10 @@ private function processStmtVarAnnotation(MutatingScope $scope, Node\Stmt $stmt,
continue;
}

if ($scope->getFunction() === null && !$scope->isInAnonymousFunction()) {
$certainty = TrinaryLogic::createYes();
}

$scope = $scope->assignVariable($name, $varTag->getType(), $certainty);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9970,6 +9970,11 @@ public function dataArrayTypehintWithoutNullInPhpDoc(): array
return $this->gatherAssertTypes(__DIR__ . '/data/array-typehint-without-null-in-phpdoc.php');
}

public function dataOverrideVariableCertaintyInRootScope(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/override-root-scope-variable.php');
}

/**
* @dataProvider dataBug2574
* @dataProvider dataBug2577
Expand Down Expand Up @@ -10027,6 +10032,7 @@ public function dataArrayTypehintWithoutNullInPhpDoc(): array
* @dataProvider dataMixedTypehint
* @dataProvider dataVariadics
* @dataProvider dataArrayTypehintWithoutNullInPhpDoc
* @dataProvider dataOverrideVariableCertaintyInRootScope
* @param string $assertType
* @param string $file
* @param mixed ...$args
Expand Down
42 changes: 42 additions & 0 deletions tests/PHPStan/Analyser/data/override-root-scope-variable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use PHPStan\TrinaryLogic;
use function PHPStan\Analyser\assertVariableCertainty;

assertVariableCertainty(TrinaryLogic::createMaybe(), $foo);

/** @var Foo $foo */

assertVariableCertainty(TrinaryLogic::createYes(), $foo);

function (): void {
assertVariableCertainty(TrinaryLogic::createNo(), $foo);

/** @var Foo $foo */

assertVariableCertainty(TrinaryLogic::createNo(), $foo);
};

function (): void {
if (rand(0, 1) === 0) {
$foo = doFoo();
}

assertVariableCertainty(TrinaryLogic::createMaybe(), $foo);

/** @var Foo $foo */

assertVariableCertainty(TrinaryLogic::createMaybe(), $foo);
};

assertVariableCertainty(TrinaryLogic::createMaybe(), $bar);
assert($bar instanceof Foo);
assertVariableCertainty(TrinaryLogic::createYes(), $bar);

function (): void {
assertVariableCertainty(TrinaryLogic::createNo(), $bar);

assert($bar instanceof Foo);

assertVariableCertainty(TrinaryLogic::createYes(), $bar);
};

0 comments on commit 44f9d08

Please sign in to comment.