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

Improve expression resolving of superglobals #3762

Open
wants to merge 2 commits into
base: 2.1.x
Choose a base branch
from
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
63 changes: 48 additions & 15 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,17 +541,20 @@ public function getVariableType(string $variableName): Type
}
}

if ($this->isGlobalVariable($variableName)) {
return new ArrayType(new BenevolentUnionType([new IntegerType(), new StringType()]), new MixedType(true));
}
$varExprString = '$' . $variableName;

if ($this->hasVariableType($variableName)->no()) {
throw new UndefinedVariableException($this, $variableName);
}

$varExprString = '$' . $variableName;
if (!array_key_exists($varExprString, $this->expressionTypes)) {
return new MixedType();
if (!$this->isGlobalVariable($variableName)) {
return new MixedType();
}

$superglobalType = new ArrayType(new BenevolentUnionType([new IntegerType(), new StringType()]), new MixedType(true));
$this->expressionTypes[$varExprString] = ExpressionTypeHolder::createYes(new Variable($variableName), $superglobalType);
$this->nativeExpressionTypes[$varExprString] = ExpressionTypeHolder::createYes(new Variable($variableName), $superglobalType);
}

return TypeUtils::resolveLateResolvableTypes($this->expressionTypes[$varExprString]->getType());
Expand Down Expand Up @@ -2883,18 +2886,16 @@ public function isInFunctionExists(string $functionName): bool
public function enterClass(ClassReflection $classReflection): self
{
$thisHolder = ExpressionTypeHolder::createYes(new Variable('this'), new ThisType($classReflection));
$constantTypes = $this->getConstantTypes();
$constantTypes['$this'] = $thisHolder;
$nativeConstantTypes = $this->getNativeConstantTypes();
$nativeConstantTypes['$this'] = $thisHolder;
$expressionTypes = array_merge($this->getSuperglobalTypes(), $this->getConstantTypes(), ['$this' => $thisHolder]);
$nativeExpressionTypes = array_merge($this->getNativeSuperglobalTypes(), $this->getNativeConstantTypes(), ['$this' => $thisHolder]);

return $this->scopeFactory->create(
$this->context->enterClass($classReflection),
$this->isDeclareStrictTypes(),
null,
$this->getNamespace(),
$constantTypes,
$nativeConstantTypes,
$expressionTypes,
$nativeExpressionTypes,
[],
[],
null,
Expand Down Expand Up @@ -3258,8 +3259,8 @@ private function enterFunctionLike(
$this->isDeclareStrictTypes(),
$functionReflection,
$this->getNamespace(),
array_merge($this->getConstantTypes(), $expressionTypes),
array_merge($this->getNativeConstantTypes(), $nativeExpressionTypes),
array_merge($this->getSuperglobalTypes(), $this->getConstantTypes(), $expressionTypes),
array_merge($this->getNativeSuperglobalTypes(), $this->getNativeConstantTypes(), $nativeExpressionTypes),
$conditionalTypes,
);
}
Expand All @@ -3272,6 +3273,8 @@ public function enterNamespace(string $namespaceName): self
$this->isDeclareStrictTypes(),
null,
$namespaceName,
$this->getSuperglobalTypes(),
$this->getNativeSuperglobalTypes(),
);
herndlm marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -3548,8 +3551,8 @@ private function enterAnonymousFunctionWithoutReflection(
$this->isDeclareStrictTypes(),
$this->getFunction(),
$this->getNamespace(),
array_merge($this->getConstantTypes(), $expressionTypes),
array_merge($this->getNativeConstantTypes(), $nativeTypes),
array_merge($this->getSuperglobalTypes(), $this->getConstantTypes(), $expressionTypes),
array_merge($this->getNativeSuperglobalTypes(), $this->getNativeConstantTypes(), $nativeTypes),
[],
$this->inClosureBindScopeClasses,
new TrivialParametersAcceptor(),
Expand Down Expand Up @@ -5807,6 +5810,36 @@ public function getConstantReflection(Type $typeWithConstant, string $constantNa
return $typeWithConstant->getConstant($constantName);
}

/** @return array<string, ExpressionTypeHolder> */
private function getSuperglobalTypes(): array
{
$superglobalTypes = [];
$exprStrings = ['$GLOBALS', '$_SERVER', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_REQUEST', '$_ENV'];
foreach ($this->expressionTypes as $exprString => $typeHolder) {
if (!in_array($exprString, $exprStrings, true)) {
continue;
}

$superglobalTypes[$exprString] = $typeHolder;
}
return $superglobalTypes;
}

/** @return array<string, ExpressionTypeHolder> */
private function getNativeSuperglobalTypes(): array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could de-duplicate this method by passing in the expressionTypes via parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right. I stole this basically from getNativeConstantTypes() vs getConstantTypes() which could be adapted in a similar way then. maybe in a follow-up refactor :)

{
$superglobalTypes = [];
$exprStrings = ['$GLOBALS', '$_SERVER', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_REQUEST', '$_ENV'];
foreach ($this->nativeExpressionTypes as $exprString => $typeHolder) {
if (!in_array($exprString, $exprStrings, true)) {
continue;
}

$superglobalTypes[$exprString] = $typeHolder;
}
return $superglobalTypes;
}

/**
* @return array<string, ExpressionTypeHolder>
*/
Expand Down
59 changes: 59 additions & 0 deletions tests/PHPStan/Analyser/nsrt/superglobals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types=1);

namespace Superglobals;

use function PHPStan\Testing\assertNativeType;
use function PHPStan\Testing\assertType;

class Superglobals
{

public function originalTypes(): void
{
assertType('array<mixed>', $GLOBALS);
assertType('array<mixed>', $_SERVER);
assertType('array<mixed>', $_GET);
assertType('array<mixed>', $_POST);
assertType('array<mixed>', $_FILES);
assertType('array<mixed>', $_COOKIE);
assertType('array<mixed>', $_SESSION);
assertType('array<mixed>', $_REQUEST);
assertType('array<mixed>', $_ENV);
}

public function canBeOverwritten(): void
{
$GLOBALS = [];
assertType('array{}', $GLOBALS);
assertNativeType('array{}', $GLOBALS);
}

public function canBePartlyOverwritten(): void
{
$GLOBALS['foo'] = 'foo';
assertType("non-empty-array&hasOffsetValue('foo', 'foo')", $GLOBALS);
assertNativeType("non-empty-array&hasOffsetValue('foo', 'foo')", $GLOBALS);
}

public function canBeNarrowed(): void
{
if (isset($GLOBALS['foo'])) {
assertType("non-empty-array&hasOffsetValue('foo', mixed~null)", $GLOBALS);
assertNativeType("non-empty-array<mixed>&hasOffset('foo')", $GLOBALS); // https://github.com/phpstan/phpstan/issues/8395
} else {
assertType('array<mixed>', $GLOBALS);
assertNativeType('array<mixed>', $GLOBALS);
}
assertType('array', $GLOBALS);
assertNativeType('array<mixed>', $GLOBALS);
}

}

function functionScope() {
assertType('array<mixed>', $GLOBALS);
assertNativeType('array<mixed>', $GLOBALS);
}

assertType('array<mixed>', $GLOBALS);
assertNativeType('array<mixed>', $GLOBALS);
Loading