Skip to content

Commit 427ac6b

Browse files
committed
toFiberScope, toMutatingScope - take advantage of inheritance and polymorphism
1 parent 316c3c5 commit 427ac6b

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

src/Analyser/Fiber/FiberScope.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,33 @@
1111
final class FiberScope extends MutatingScope
1212
{
1313

14+
public function toFiberScope(): self
15+
{
16+
return $this;
17+
}
18+
19+
public function toMutatingScope(): MutatingScope
20+
{
21+
return $this->scopeFactory->toMutatingFactory()->create(
22+
$this->context,
23+
$this->isDeclareStrictTypes(),
24+
$this->getFunction(),
25+
$this->getNamespace(),
26+
$this->expressionTypes,
27+
$this->nativeExpressionTypes,
28+
$this->conditionalExpressions,
29+
$this->inClosureBindScopeClasses,
30+
$this->getAnonymousFunctionReflection(),
31+
$this->isInFirstLevelStatement(),
32+
$this->currentlyAssignedExpressions,
33+
$this->currentlyAllowedUndefinedExpressions,
34+
$this->inFunctionCallsStack,
35+
$this->afterExtractCall,
36+
$this->getParentScope(),
37+
$this->nativeTypesPromoted,
38+
);
39+
}
40+
1441
/** @api */
1542
public function getType(Expr $node): Type
1643
{

src/Analyser/MutatingScope.php

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
use function usort;
168168
use const PHP_INT_MAX;
169169
use const PHP_INT_MIN;
170+
use const PHP_VERSION_ID;
170171

171172
class MutatingScope implements Scope, NodeCallbackInvoker
172173
{
@@ -205,7 +206,7 @@ class MutatingScope implements Scope, NodeCallbackInvoker
205206
* @param list<array{MethodReflection|FunctionReflection|null, ParameterReflection|null}> $inFunctionCallsStack
206207
*/
207208
public function __construct(
208-
private InternalScopeFactory $scopeFactory,
209+
protected InternalScopeFactory $scopeFactory,
209210
private ReflectionProvider $reflectionProvider,
210211
private InitializerExprTypeResolver $initializerExprTypeResolver,
211212
private DynamicReturnTypeExtensionRegistry $dynamicReturnTypeExtensionRegistry,
@@ -217,26 +218,26 @@ public function __construct(
217218
private NodeScopeResolver $nodeScopeResolver,
218219
private RicherScopeGetTypeHelper $richerScopeGetTypeHelper,
219220
private ConstantResolver $constantResolver,
220-
private ScopeContext $context,
221+
protected ScopeContext $context,
221222
private PhpVersion $phpVersion,
222223
private AttributeReflectionFactory $attributeReflectionFactory,
223224
private int|array|null $configPhpVersion,
224225
private $nodeCallback = null,
225226
private bool $declareStrictTypes = false,
226227
private PhpFunctionFromParserNodeReflection|null $function = null,
227228
?string $namespace = null,
228-
private array $expressionTypes = [],
229-
private array $nativeExpressionTypes = [],
230-
private array $conditionalExpressions = [],
231-
private array $inClosureBindScopeClasses = [],
229+
protected array $expressionTypes = [],
230+
protected array $nativeExpressionTypes = [],
231+
protected array $conditionalExpressions = [],
232+
protected array $inClosureBindScopeClasses = [],
232233
private ?ClosureType $anonymousFunctionReflection = null,
233234
private bool $inFirstLevelStatement = true,
234-
private array $currentlyAssignedExpressions = [],
235-
private array $currentlyAllowedUndefinedExpressions = [],
236-
private array $inFunctionCallsStack = [],
237-
private bool $afterExtractCall = false,
235+
protected array $currentlyAssignedExpressions = [],
236+
protected array $currentlyAllowedUndefinedExpressions = [],
237+
protected array $inFunctionCallsStack = [],
238+
protected bool $afterExtractCall = false,
238239
private ?Scope $parentScope = null,
239-
private bool $nativeTypesPromoted = false,
240+
protected bool $nativeTypesPromoted = false,
240241
)
241242
{
242243
if ($namespace === '') {
@@ -248,8 +249,8 @@ public function __construct(
248249

249250
public function toFiberScope(): self
250251
{
251-
if (static::class === FiberScope::class) {
252-
return $this;
252+
if (PHP_VERSION_ID < 80100) {
253+
throw new ShouldNotHappenException('Cannot create FiberScope below PHP 8.1');
253254
}
254255

255256
return $this->scopeFactory->toFiberFactory()->create(
@@ -274,28 +275,7 @@ public function toFiberScope(): self
274275

275276
public function toMutatingScope(): self
276277
{
277-
if (static::class === self::class) {
278-
return $this;
279-
}
280-
281-
return $this->scopeFactory->toMutatingFactory()->create(
282-
$this->context,
283-
$this->isDeclareStrictTypes(),
284-
$this->getFunction(),
285-
$this->getNamespace(),
286-
$this->expressionTypes,
287-
$this->nativeExpressionTypes,
288-
$this->conditionalExpressions,
289-
$this->inClosureBindScopeClasses,
290-
$this->anonymousFunctionReflection,
291-
$this->isInFirstLevelStatement(),
292-
$this->currentlyAssignedExpressions,
293-
$this->currentlyAllowedUndefinedExpressions,
294-
$this->inFunctionCallsStack,
295-
$this->afterExtractCall,
296-
$this->parentScope,
297-
$this->nativeTypesPromoted,
298-
);
278+
return $this;
299279
}
300280

301281
/** @api */

0 commit comments

Comments
 (0)