Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make superglobals more specific #8473

Merged
merged 5 commits into from Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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