@@ -294,41 +294,57 @@ public function enterDeclareStrictTypes(): self
294
294
);
295
295
}
296
296
297
- public function rememberConstructorScope (): self
297
+ /**
298
+ * @param array<string, ExpressionTypeHolder> $expressions
299
+ * @return array<string, ExpressionTypeHolder>
300
+ */
301
+ private function rememberConstructorExpressions (array $ currentExpressionTypes ): array
298
302
{
299
303
$ expressionTypes = [];
300
- foreach ($ this -> expressionTypes as $ exprString => $ expressionTypeHolder ) {
304
+ foreach ($ currentExpressionTypes as $ exprString => $ expressionTypeHolder ) {
301
305
$ expr = $ expressionTypeHolder ->getExpr ();
302
- if (!$ expr instanceof ConstFetch) {
303
- continue ;
304
- }
305
- $ expressionTypes [$ exprString ] = $ expressionTypeHolder ;
306
- }
306
+ if ($ expr instanceof PropertyFetch) {
307
+ if (
308
+ !$ expr ->name instanceof Node \Identifier
309
+ || !$ expr ->var instanceof Variable
310
+ || $ expr ->var ->name !== 'this '
311
+ || !$ this ->phpVersion ->supportsReadOnlyProperties ()
312
+ ) {
313
+ continue ;
314
+ }
307
315
308
- $ nativeExpressionTypes = [];
309
- foreach ($ this ->nativeExpressionTypes as $ exprString => $ expressionTypeHolder ) {
310
- $ expr = $ expressionTypeHolder ->getExpr ();
311
- if (!$ expr instanceof ConstFetch) {
316
+ $ propertyReflection = $ this ->propertyReflectionFinder ->findPropertyReflectionFromNode ($ expr , $ this );
317
+ if ($ propertyReflection === null ) {
318
+ continue ;
319
+ }
320
+
321
+ $ nativePropertyReflection = $ propertyReflection ->getNativeReflection ();
322
+ if ($ nativePropertyReflection === null || !$ nativePropertyReflection ->isReadOnly ()) {
323
+ continue ;
324
+ }
325
+ } elseif (!$ expr instanceof ConstFetch) {
312
326
continue ;
313
327
}
314
328
315
- $ nativeExpressionTypes [$ exprString ] = $ expressionTypeHolder ;
329
+ $ expressionTypes [$ exprString ] = $ expressionTypeHolder ;
316
330
}
317
331
318
- if (array_key_exists ('$this ' , $ this ->expressionTypes )) {
319
- $ expressionTypes ['$this ' ] = $ this ->expressionTypes ['$this ' ];
320
- }
321
- if (array_key_exists ('$this ' , $ this ->nativeExpressionTypes )) {
322
- $ nativeExpressionTypes ['$this ' ] = $ this ->nativeExpressionTypes ['$this ' ];
332
+ if (array_key_exists ('$this ' , $ currentExpressionTypes )) {
333
+ $ expressionTypes ['$this ' ] = $ currentExpressionTypes ['$this ' ];
323
334
}
324
335
336
+ return $ expressionTypes ;
337
+ }
338
+
339
+ public function rememberConstructorScope (): self
340
+ {
325
341
return $ this ->scopeFactory ->create (
326
342
$ this ->context ,
327
343
$ this ->isDeclareStrictTypes (),
328
344
$ this ->getFunction (),
329
345
$ this ->getNamespace (),
330
- $ expressionTypes ,
331
- $ nativeExpressionTypes ,
346
+ $ this -> rememberConstructorExpressions ( $ this -> expressionTypes ) ,
347
+ $ this -> rememberConstructorExpressions ( $ this -> nativeExpressionTypes ) ,
332
348
$ this ->conditionalExpressions ,
333
349
$ this ->inClosureBindScopeClasses ,
334
350
$ this ->anonymousFunctionReflection ,
@@ -3334,7 +3350,7 @@ public function enterFunction(
3334
3350
3335
3351
private function enterFunctionLike (
3336
3352
PhpFunctionFromParserNodeReflection $ functionReflection ,
3337
- bool $ preserveThis ,
3353
+ bool $ preserveConstructorScope ,
3338
3354
): self
3339
3355
{
3340
3356
$ parametersByName = [];
@@ -3346,6 +3362,12 @@ private function enterFunctionLike(
3346
3362
$ expressionTypes = [];
3347
3363
$ nativeExpressionTypes = [];
3348
3364
$ conditionalTypes = [];
3365
+
3366
+ if ($ preserveConstructorScope ) {
3367
+ $ expressionTypes = $ this ->rememberConstructorExpressions ($ this ->expressionTypes );
3368
+ $ nativeExpressionTypes = $ this ->rememberConstructorExpressions ($ this ->nativeExpressionTypes );
3369
+ }
3370
+
3349
3371
foreach ($ functionReflection ->getParameters () as $ parameter ) {
3350
3372
$ parameterType = $ parameter ->getType ();
3351
3373
@@ -3396,13 +3418,6 @@ private function enterFunctionLike(
3396
3418
$ nativeExpressionTypes [$ parameterOriginalValueExprString ] = ExpressionTypeHolder::createYes ($ parameterOriginalValueExpr , $ nativeParameterType );
3397
3419
}
3398
3420
3399
- if ($ preserveThis && array_key_exists ('$this ' , $ this ->expressionTypes )) {
3400
- $ expressionTypes ['$this ' ] = $ this ->expressionTypes ['$this ' ];
3401
- }
3402
- if ($ preserveThis && array_key_exists ('$this ' , $ this ->nativeExpressionTypes )) {
3403
- $ nativeExpressionTypes ['$this ' ] = $ this ->nativeExpressionTypes ['$this ' ];
3404
- }
3405
-
3406
3421
return $ this ->scopeFactory ->create (
3407
3422
$ this ->context ,
3408
3423
$ this ->isDeclareStrictTypes (),
0 commit comments