Skip to content

Commit

Permalink
Merge pull request #8473 from kkmuffme/detailed-superglobal-types
Browse files Browse the repository at this point in the history
make superglobals more specific
  • Loading branch information
orklah committed Sep 19, 2022
2 parents 8185260 + e2e6265 commit 3b7e508
Show file tree
Hide file tree
Showing 31 changed files with 308 additions and 73 deletions.
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedArgument.md
Expand Up @@ -6,5 +6,5 @@ Emitted when Psalm cannot determine the type of an argument
<?php

function takesInt(int $i) : void {}
takesInt($_GET['foo']);
takesInt($GLOBALS['foo']);
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedArrayAccess.md
Expand Up @@ -5,5 +5,5 @@ Emitted when trying to access an array offset on a value whose type Psalm cannot
```php
<?php

echo $_GET['foo'][0];
echo $GLOBALS['foo'][0];
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedArrayAssignment.md
Expand Up @@ -5,5 +5,5 @@ Emitted when trying to assign a value to an array offset on a value whose type P
```php
<?php

$_GET['foo'][0] = "5";
$GLOBALS['foo'][0] = "5";
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedArrayOffset.md
Expand Up @@ -5,5 +5,5 @@ Emitted when attempting to access an array offset where Psalm cannot determine t
```php
<?php

echo [1, 2, 3][$_GET['foo']];
echo [1, 2, 3][$GLOBALS['foo']];
```
8 changes: 4 additions & 4 deletions docs/running_psalm/issues/MixedAssignment.md
Expand Up @@ -6,7 +6,7 @@ cannot infer a type more specific than `mixed`.
```php
<?php

$a = $_GET['foo'];
$a = $GLOBALS['foo'];
```

## How to fix
Expand All @@ -16,7 +16,7 @@ The above example can be fixed in a few ways – by adding an `assert` call:
```php
<?php

$a = $_GET['foo'];
$a = $GLOBALS['foo'];
assert(is_string($a));
```

Expand All @@ -25,7 +25,7 @@ or by adding an explicit cast:
```php
<?php

$a = (string) $_GET['foo'];
$a = (string) $GLOBALS['foo'];
```

or by adding a docblock
Expand All @@ -34,5 +34,5 @@ or by adding a docblock
<?php

/** @var string */
$a = $_GET['foo'];
$a = $GLOBALS['foo'];
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedClone.md
Expand Up @@ -5,5 +5,5 @@ Emitted when trying to clone a value whose type is not known
```php
<?php

$a = clone $_GET["a"];
$a = clone $GLOBALS["a"];
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedFunctionCall.md
Expand Up @@ -6,6 +6,6 @@ Emitted when calling a function on a value whose type Psalm cannot infer.
<?php

/** @var mixed */
$a = $_GET['foo'];
$a = $GLOBALS['foo'];
$a();
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedInferredReturnType.md
Expand Up @@ -6,6 +6,6 @@ Emitted when Psalm cannot determine a function's return type
<?php

function foo() : int {
return $_GET['foo'];
return $GLOBALS['foo'];
}
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedOperand.md
Expand Up @@ -5,7 +5,7 @@ Emitted when Psalm cannot infer a type for an operand in any calculated expressi
```php
<?php

echo $_GET['foo'] + "hello";
echo $GLOBALS['foo'] + "hello";
```

## Why it’s bad
Expand Down
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedReturnStatement.md
Expand Up @@ -6,6 +6,6 @@ Emitted when Psalm cannot determine the type of a given return statement
<?php

function foo() : int {
return $_GET['foo']; // emitted here
return $GLOBALS['foo']; // emitted here
}
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedStringOffsetAssignment.md
Expand Up @@ -5,5 +5,5 @@ Emitted when assigning a value on a string using a value for which Psalm cannot
```php
<?php

"hello"[0] = $_GET['foo'];
"hello"[0] = $GLOBALS['foo'];
```
2 changes: 1 addition & 1 deletion src/Psalm/Codebase.php
Expand Up @@ -1088,7 +1088,7 @@ public function getSymbolInformation(string $file_path, string $symbol): ?array
}

if (strpos($symbol, '$') === 0) {
$type = VariableFetchAnalyzer::getGlobalType($symbol);
$type = VariableFetchAnalyzer::getGlobalType($symbol, $this->analysis_php_version_id);
if (!$type->isMixed()) {
return ['type' => '<?php ' . $type];
}
Expand Down

0 comments on commit 3b7e508

Please sign in to comment.