Skip to content

Commit

Permalink
more precise $argc, $argv global var type
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Sep 10, 2022
1 parent fb08fb5 commit 0a9465e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\ClosureType;
Expand Down Expand Up @@ -500,6 +501,18 @@ public function hasVariableType(string $variableName): TrinaryLogic
/** @api */
public function getVariableType(string $variableName): Type
{
if ($this->hasVariableType($variableName)->maybe()) {
if ($variableName === 'argc') {
return IntegerRangeType::fromInterval(1, null);
}
if ($variableName === 'argv') {
return TypeCombinator::intersect(
new ArrayType(new IntegerType(), new StringType()),
new NonEmptyArrayType(),
);
}
}

if ($this->isGlobalVariable($variableName)) {
return new ArrayType(new StringType(), new MixedType($this->explicitMixedForGlobalVariables));
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7244.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7501.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/bug-7954.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/cli-globals.php');
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Analyser/data/cli-globals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace CliGlobals;

\PHPStan\Testing\assertType('int<1, max>', $argc);
\PHPStan\Testing\assertType('non-empty-array<int, string>', $argv);

function f() {
\PHPStan\Testing\assertType('*ERROR*', $argc);
\PHPStan\Testing\assertType('*ERROR*', $argv);
}

function g($argc, $argv) {
\PHPStan\Testing\assertType('mixed', $argc);
\PHPStan\Testing\assertType('mixed', $argv);
}

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

0 comments on commit 0a9465e

Please sign in to comment.