Skip to content
Open
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
8 changes: 2 additions & 6 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,14 +659,10 @@ public function getVariableType(string $variableName): Type

if ($hasVariableType->maybe()) {
if ($variableName === 'argc') {
return IntegerRangeType::fromInterval(1, null);
return StaticTypeFactory::argc();
}
if ($variableName === 'argv') {
return new IntersectionType([
new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), new StringType()),
new NonEmptyArrayType(),
new AccessoryArrayListType(),
]);
return StaticTypeFactory::argv();
}
if ($this->canAnyVariableExist()) {
return new MixedType();
Expand Down
16 changes: 15 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
use PHPStan\Type\ParserNodeTypeToPHPStanType;
use PHPStan\Type\ResourceType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand Down Expand Up @@ -2181,7 +2182,8 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch
continue;
}

$scope = $scope->assignVariable($var->name, new MixedType(), new MixedType(), TrinaryLogic::createYes());
$varType = $this->getGlobalVariableType($var->name);
$scope = $scope->assignVariable($var->name, $varType, $varType, TrinaryLogic::createYes());
$vars[] = $var->name;
}
$scope = $this->processVarAnnotation($scope, $vars, $stmt);
Expand Down Expand Up @@ -4853,4 +4855,16 @@ private function inferForLoopExpressions(For_ $stmt, Expr $lastCondExpr, Mutatin
return $bodyScope;
}

private function getGlobalVariableType(string $variableName): Type
{
if ($variableName === 'argc') {
return StaticTypeFactory::argc();
}
if ($variableName === 'argv') {
return StaticTypeFactory::argv();
}

return new MixedType();
}

}
16 changes: 16 additions & 0 deletions src/Type/StaticTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Type;

use ArrayAccess;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
Expand Down Expand Up @@ -42,6 +44,20 @@ public static function truthy(): Type
return $truthy;
}

public static function argv(): Type
{
return new IntersectionType([
new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), new StringType()),
new NonEmptyArrayType(),
new AccessoryArrayListType(),
]);
}

public static function argc(): Type
{
return IntegerRangeType::fromInterval(1, null);
}

public static function generalOffsetAccessibleType(): Type
{
static $generalOffsetAccessible;
Expand Down
35 changes: 33 additions & 2 deletions tests/PHPStan/Analyser/nsrt/cli-globals.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ function g($argc, $argv) {

function h() {
global $argc, $argv;
assertType('mixed', $argc); // should be int<1, max>
assertType('mixed', $argv); // should be non-empty-array<int, string>
assertType('int<1, max>', $argc);
assertType('non-empty-list<string>', $argv);
}

function i() {
Expand All @@ -31,3 +31,34 @@ function i() {
assertType("'hallo'", $argc);
assertType("'welt'", $argv);
}

function j() {
global $argc, $argv;
assertType('int<1, max>', $argc);
assertType('non-empty-list<string>', $argv);

$argc = 'overridden';
assertType("'overridden'", $argc);
}

class Foo {
public function bar(): void {
global $argc, $argv;
assertType('int<1, max>', $argc);
assertType('non-empty-list<string>', $argv);
}

public static function baz(): void {
global $argc, $argv;
assertType('int<1, max>', $argc);
assertType('non-empty-list<string>', $argv);
}
}

function withClosure(): void {
$fn = function () {
global $argc, $argv;
assertType('int<1, max>', $argc);
assertType('non-empty-list<string>', $argv);
};
}
Loading