-
Notifications
You must be signed in to change notification settings - Fork 525
Improve expression resolving of superglobals #3762
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -548,17 +548,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()); | ||
|
@@ -2893,18 +2896,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, | ||
|
@@ -3298,8 +3299,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, | ||
); | ||
} | ||
|
@@ -3312,6 +3313,8 @@ public function enterNamespace(string $namespaceName): self | |
$this->isDeclareStrictTypes(), | ||
null, | ||
$namespaceName, | ||
$this->getSuperglobalTypes(), | ||
$this->getNativeSuperglobalTypes(), | ||
); | ||
} | ||
|
||
|
@@ -3588,8 +3591,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(), | ||
|
@@ -5987,6 +5990,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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right. I stole this basically from |
||
{ | ||
$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> | ||
*/ | ||
|
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); |
Uh oh!
There was an error while loading. Please reload this page.