@@ -1445,24 +1445,38 @@ public static function isConstructorPromotion(File $phpcsFile, $stackPtr)
1445
1445
1446
1446
// If the previous token is a visibility keyword, this is constructor
1447
1447
// promotion. eg: `public $foobar`.
1448
- $ prev = $ phpcsFile ->findPrevious (Tokens::$ emptyTokens , ($ stackPtr - 1 ), $ functionIndex , true );
1449
- if (! is_int ($ prev )) {
1448
+ $ prevIndex = $ phpcsFile ->findPrevious (Tokens::$ emptyTokens , ($ stackPtr - 1 ), $ functionIndex , true );
1449
+ if (! is_int ($ prevIndex )) {
1450
1450
return false ;
1451
1451
}
1452
- $ prevToken = $ tokens [$ prev ];
1452
+ $ prevToken = $ tokens [$ prevIndex ];
1453
1453
if (in_array ($ prevToken ['code ' ], Tokens::$ scopeModifiers , true )) {
1454
1454
return true ;
1455
1455
}
1456
1456
1457
1457
// If the previous token is not a visibility keyword, but the one before it
1458
1458
// is, the previous token was probably a typehint and this is constructor
1459
1459
// promotion. eg: `public boolean $foobar`.
1460
- $ prev = $ phpcsFile ->findPrevious (Tokens::$ emptyTokens , ($ prev - 1 ), $ functionIndex , true );
1461
- if (! is_int ($ prev )) {
1460
+ $ prev2Index = $ phpcsFile ->findPrevious (Tokens::$ emptyTokens , ($ prevIndex - 1 ), $ functionIndex , true );
1461
+ if (! is_int ($ prev2Index )) {
1462
1462
return false ;
1463
1463
}
1464
- $ prevToken = $ tokens [$ prev ];
1465
- if (in_array ($ prevToken ['code ' ], Tokens::$ scopeModifiers , true )) {
1464
+ $ prev2Token = $ tokens [$ prev2Index ];
1465
+ if (in_array ($ prev2Token ['code ' ], Tokens::$ scopeModifiers , true )) {
1466
+ return true ;
1467
+ }
1468
+
1469
+ // If the previous token is not a visibility keyword, but the one two
1470
+ // before it is, and one of the tokens is `readonly`, the previous token
1471
+ // was probably a typehint and this is constructor promotion. eg: `public
1472
+ // readonly boolean $foobar`.
1473
+ $ prev3Index = $ phpcsFile ->findPrevious (Tokens::$ emptyTokens , ($ prev2Index - 1 ), $ functionIndex , true );
1474
+ if (! is_int ($ prev3Index )) {
1475
+ return false ;
1476
+ }
1477
+ $ prev3Token = $ tokens [$ prev3Index ];
1478
+ $ wasPreviousReadonly = $ prevToken ['content ' ] === 'readonly ' || $ prev2Token ['content ' ] === 'readonly ' ;
1479
+ if (in_array ($ prev3Token ['code ' ], Tokens::$ scopeModifiers , true ) && $ wasPreviousReadonly ) {
1466
1480
return true ;
1467
1481
}
1468
1482
0 commit comments