Skip to content

Commit 34dabdd

Browse files
authored
Merge branch refs/heads/1.9.x into 1.10.x
2 parents b0fbe29 + 02e1a14 commit 34dabdd

9 files changed

+62
-132
lines changed

conf/config.neon

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,6 @@ services:
618618

619619
-
620620
class: PHPStan\Analyser\ScopeFactory
621-
arguments:
622-
explicitMixedForGlobalVariables: %featureToggles.explicitMixedForGlobalVariables%
623621

624622
-
625623
class: PHPStan\Analyser\NodeScopeResolver

src/Analyser/DirectInternalScopeFactory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,20 @@ public function __construct(
3434
private bool $treatPhpDocTypesAsCertain,
3535
private PhpVersion $phpVersion,
3636
private bool $explicitMixedInUnknownGenericNew,
37+
private bool $explicitMixedForGlobalVariables,
3738
private ConstantResolver $constantResolver,
3839
)
3940
{
4041
}
4142

43+
/**
44+
* @param array<string, ExpressionTypeHolder> $expressionTypes
45+
* @param array<string, ExpressionTypeHolder> $nativeExpressionTypes
46+
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
47+
* @param array<(FunctionReflection|MethodReflection)> $inFunctionCallsStack
48+
* @param array<string, true> $currentlyAssignedExpressions
49+
* @param array<string, true> $currentlyAllowedUndefinedExpressions
50+
*/
4251
public function create(
4352
ScopeContext $context,
4453
bool $declareStrictTypes = false,
@@ -93,6 +102,7 @@ public function create(
93102
$parentScope,
94103
$nativeTypesPromoted,
95104
$this->explicitMixedInUnknownGenericNew,
105+
$this->explicitMixedForGlobalVariables,
96106
);
97107
}
98108

src/Analyser/InternalScopeFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface InternalScopeFactory
1515
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
1616
* @param array<string, true> $currentlyAssignedExpressions
1717
* @param array<string, true> $currentlyAllowedUndefinedExpressions
18-
* @param array<FunctionReflection|MethodReflection> $inFunctionCallsStack
18+
* @param array<MethodReflection|FunctionReflection> $inFunctionCallsStack
1919
*/
2020
public function create(
2121
ScopeContext $context,

src/Analyser/LazyInternalScopeFactory.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class LazyInternalScopeFactory implements InternalScopeFactory
2222

2323
private bool $explicitMixedInUnknownGenericNew;
2424

25+
private bool $explicitMixedForGlobalVariables;
26+
2527
/**
2628
* @param class-string $scopeClass
2729
*/
@@ -32,8 +34,18 @@ public function __construct(
3234
{
3335
$this->treatPhpDocTypesAsCertain = $container->getParameter('treatPhpDocTypesAsCertain');
3436
$this->explicitMixedInUnknownGenericNew = $this->container->getParameter('featureToggles')['explicitMixedInUnknownGenericNew'];
37+
$this->explicitMixedForGlobalVariables = $this->container->getParameter('featureToggles')['explicitMixedForGlobalVariables'];
3538
}
3639

40+
/**
41+
* @param array<string, ExpressionTypeHolder> $expressionTypes
42+
* @param array<string, ExpressionTypeHolder> $nativeExpressionTypes
43+
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
44+
* @param array<string, true> $currentlyAssignedExpressions
45+
* @param array<string, true> $currentlyAllowedUndefinedExpressions
46+
* @param array<(FunctionReflection|MethodReflection)> $inFunctionCallsStack
47+
*
48+
*/
3749
public function create(
3850
ScopeContext $context,
3951
bool $declareStrictTypes = false,
@@ -88,6 +100,7 @@ public function create(
88100
$parentScope,
89101
$nativeTypesPromoted,
90102
$this->explicitMixedInUnknownGenericNew,
103+
$this->explicitMixedForGlobalVariables,
91104
);
92105
}
93106

src/Analyser/MutatingScope.php

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ public function __construct(
188188
private ?Scope $parentScope = null,
189189
private bool $nativeTypesPromoted = false,
190190
private bool $explicitMixedInUnknownGenericNew = false,
191+
private bool $explicitMixedForGlobalVariables = false,
191192
)
192193
{
193194
if ($namespace === '') {
@@ -465,6 +466,10 @@ public function afterOpenSslCall(string $openSslFunctionName): self
465466
/** @api */
466467
public function hasVariableType(string $variableName): TrinaryLogic
467468
{
469+
if ($this->isGlobalVariable($variableName)) {
470+
return TrinaryLogic::createYes();
471+
}
472+
468473
$varExprString = '$' . $variableName;
469474
if (!isset($this->expressionTypes[$varExprString])) {
470475
if ($this->canAnyVariableExist()) {
@@ -495,6 +500,10 @@ public function getVariableType(string $variableName): Type
495500
}
496501
}
497502

503+
if ($this->isGlobalVariable($variableName)) {
504+
return new ArrayType(new StringType(), new MixedType($this->explicitMixedForGlobalVariables));
505+
}
506+
498507
if ($this->hasVariableType($variableName)->no()) {
499508
throw new UndefinedVariableException($this, $variableName);
500509
}
@@ -528,6 +537,21 @@ public function getDefinedVariables(): array
528537
return $variables;
529538
}
530539

540+
private function isGlobalVariable(string $variableName): bool
541+
{
542+
return in_array($variableName, [
543+
'GLOBALS',
544+
'_SERVER',
545+
'_GET',
546+
'_POST',
547+
'_FILES',
548+
'_COOKIE',
549+
'_SESSION',
550+
'_REQUEST',
551+
'_ENV',
552+
], true);
553+
}
554+
531555
/** @api */
532556
public function hasConstant(Name $name): bool
533557
{
@@ -2109,6 +2133,7 @@ public function doNotTreatPhpDocTypesAsCertain(): Scope
21092133
$this->parentScope,
21102134
false,
21112135
$this->explicitMixedInUnknownGenericNew,
2136+
$this->explicitMixedForGlobalVariables,
21122137
);
21132138
}
21142139

@@ -2405,8 +2430,12 @@ public function enterClass(ClassReflection $classReflection): self
24052430
$this->isDeclareStrictTypes(),
24062431
null,
24072432
$this->getNamespace(),
2408-
array_merge($this->getSuperglobalTypes(), $this->getConstantTypes(), ['$this' => $thisHolder]),
2409-
array_merge($this->getNativeSuperglobalTypes(), $this->getNativeConstantTypes(), ['$this' => $thisHolder]),
2433+
array_merge($this->getConstantTypes(), [
2434+
'$this' => $thisHolder,
2435+
]),
2436+
array_merge($this->getNativeConstantTypes(), [
2437+
'$this' => $thisHolder,
2438+
]),
24102439
[],
24112440
null,
24122441
null,
@@ -2642,8 +2671,8 @@ private function enterFunctionLike(
26422671
$this->isDeclareStrictTypes(),
26432672
$functionReflection,
26442673
$this->getNamespace(),
2645-
array_merge($this->getSuperglobalTypes(), $this->getConstantTypes(), $expressionTypes),
2646-
array_merge($this->getNativeSuperglobalTypes(), $this->getNativeConstantTypes(), $nativeExpressionTypes),
2674+
array_merge($this->getConstantTypes(), $expressionTypes),
2675+
array_merge($this->getNativeConstantTypes(), $nativeExpressionTypes),
26472676
);
26482677
}
26492678

@@ -2655,8 +2684,6 @@ public function enterNamespace(string $namespaceName): self
26552684
$this->isDeclareStrictTypes(),
26562685
null,
26572686
$namespaceName,
2658-
$this->getSuperglobalTypes(),
2659-
$this->getNativeSuperglobalTypes(),
26602687
);
26612688
}
26622689

@@ -2880,8 +2907,8 @@ private function enterAnonymousFunctionWithoutReflection(
28802907
$this->isDeclareStrictTypes(),
28812908
$this->getFunction(),
28822909
$this->getNamespace(),
2883-
array_merge($this->getSuperglobalTypes(), $this->getConstantTypes(), $expressionTypes),
2884-
array_merge($this->getNativeSuperglobalTypes(), $this->getNativeConstantTypes(), $nativeTypes),
2910+
array_merge($this->getConstantTypes(), $expressionTypes),
2911+
array_merge($this->getNativeConstantTypes(), $nativeTypes),
28852912
[],
28862913
$this->inClosureBindScopeClass,
28872914
new TrivialParametersAcceptor(),
@@ -4963,34 +4990,4 @@ private function getNativeConstantTypes(): array
49634990
return $constantTypes;
49644991
}
49654992

4966-
/** @return array<string, ExpressionTypeHolder> */
4967-
private function getSuperglobalTypes(): array
4968-
{
4969-
$superglobalTypes = [];
4970-
$exprStrings = ['$GLOBALS', '$_SERVER', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_REQUEST', '$_ENV'];
4971-
foreach ($this->expressionTypes as $exprString => $typeHolder) {
4972-
if (!in_array($exprString, $exprStrings, true)) {
4973-
continue;
4974-
}
4975-
4976-
$superglobalTypes[$exprString] = $typeHolder;
4977-
}
4978-
return $superglobalTypes;
4979-
}
4980-
4981-
/** @return array<string, ExpressionTypeHolder> */
4982-
private function getNativeSuperglobalTypes(): array
4983-
{
4984-
$superglobalTypes = [];
4985-
$exprStrings = ['$GLOBALS', '$_SERVER', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_REQUEST', '$_ENV'];
4986-
foreach ($this->nativeExpressionTypes as $exprString => $typeHolder) {
4987-
if (!in_array($exprString, $exprStrings, true)) {
4988-
continue;
4989-
}
4990-
4991-
$superglobalTypes[$exprString] = $typeHolder;
4992-
}
4993-
return $superglobalTypes;
4994-
}
4995-
49964993
}

src/Analyser/ScopeFactory.php

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,17 @@
22

33
namespace PHPStan\Analyser;
44

5-
use PhpParser\Node\Expr\Variable;
6-
use PHPStan\Type\ArrayType;
7-
use PHPStan\Type\MixedType;
8-
use PHPStan\Type\StringType;
9-
105
/** @api */
116
class ScopeFactory
127
{
138

14-
public function __construct(
15-
private InternalScopeFactory $internalScopeFactory,
16-
private bool $explicitMixedForGlobalVariables,
17-
)
9+
public function __construct(private InternalScopeFactory $internalScopeFactory)
1810
{
1911
}
2012

2113
public function create(ScopeContext $context): MutatingScope
2214
{
23-
$superglobalType = new ArrayType(new StringType(), new MixedType($this->explicitMixedForGlobalVariables));
24-
$expressionTypes = [
25-
'$GLOBALS' => ExpressionTypeHolder::createYes(new Variable('GLOBALS'), $superglobalType),
26-
'$_SERVER' => ExpressionTypeHolder::createYes(new Variable('_SERVER'), $superglobalType),
27-
'$_GET' => ExpressionTypeHolder::createYes(new Variable('_GET'), $superglobalType),
28-
'$_POST' => ExpressionTypeHolder::createYes(new Variable('_POST'), $superglobalType),
29-
'$_FILES' => ExpressionTypeHolder::createYes(new Variable('_FILES'), $superglobalType),
30-
'$_COOKIE' => ExpressionTypeHolder::createYes(new Variable('_COOKIE'), $superglobalType),
31-
'$_SESSION' => ExpressionTypeHolder::createYes(new Variable('_SESSION'), $superglobalType),
32-
'$_REQUEST' => ExpressionTypeHolder::createYes(new Variable('_REQUEST'), $superglobalType),
33-
'$_ENV' => ExpressionTypeHolder::createYes(new Variable('_ENV'), $superglobalType),
34-
];
35-
36-
return $this->internalScopeFactory->create(
37-
$context,
38-
false,
39-
null,
40-
null,
41-
$expressionTypes,
42-
$expressionTypes,
43-
);
15+
return $this->internalScopeFactory->create($context);
4416
}
4517

4618
}

src/Testing/PHPStanTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ public function createScopeFactory(ReflectionProvider $reflectionProvider, TypeS
179179
$this->shouldTreatPhpDocTypesAsCertain(),
180180
$container->getByType(PhpVersion::class),
181181
$container->getParameter('featureToggles')['explicitMixedInUnknownGenericNew'],
182+
$container->getParameter('featureToggles')['explicitMixedForGlobalVariables'],
182183
$constantResolver,
183184
),
184-
$container->getParameter('featureToggles')['explicitMixedForGlobalVariables'],
185185
);
186186
}
187187

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,6 @@ public function dataFileAsserts(): iterable
463463
yield from $this->gatherAssertTypes(__DIR__ . '/data/closure-types.php');
464464
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5219.php');
465465
yield from $this->gatherAssertTypes(__DIR__ . '/data/strval.php');
466-
yield from $this->gatherAssertTypes(__DIR__ . '/data/superglobals.php');
467466
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-next.php');
468467

469468
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-string.php');

tests/PHPStan/Analyser/data/superglobals.php

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)