@@ -272,6 +272,7 @@ public function __construct(
272272 private readonly array $ universalObjectCratesClasses ,
273273 private readonly bool $ implicitThrows ,
274274 private readonly bool $ treatPhpDocTypesAsCertain ,
275+ private readonly bool $ narrowMethodScopeFromConstructor ,
275276 )
276277 {
277278 $ earlyTerminatingMethodNames = [];
@@ -793,7 +794,7 @@ private function processStmtNode(
793794 $ methodReflection ,
794795 ), $ methodScope );
795796
796- if ($ isConstructor ) {
797+ if ($ isConstructor && $ this -> narrowMethodScopeFromConstructor ) {
797798 $ scope = $ statementResult ->getScope ()->rememberConstructorScope ();
798799 }
799800 }
@@ -930,22 +931,24 @@ private function processStmtNode(
930931 $ classStatementsGatherer = new ClassStatementsGatherer ($ classReflection , $ nodeCallback );
931932 $ this ->processAttributeGroups ($ stmt , $ stmt ->attrGroups , $ classScope , $ classStatementsGatherer );
932933
933- // analyze static methods first; constructor next; instance methods and property hooks last so we can carry over the scope
934934 $ classLikeStatements = $ stmt ->stmts ;
935- usort ($ classLikeStatements , static function ($ a , $ b ) {
936- if ($ a instanceof Node \Stmt \Property) {
937- return 1 ;
938- }
939- if ($ b instanceof Node \Stmt \Property) {
940- return -1 ;
941- }
935+ if ($ this ->narrowMethodScopeFromConstructor ) {
936+ // analyze static methods first; constructor next; instance methods and property hooks last so we can carry over the scope
937+ usort ($ classLikeStatements , static function ($ a , $ b ) {
938+ if ($ a instanceof Node \Stmt \Property) {
939+ return 1 ;
940+ }
941+ if ($ b instanceof Node \Stmt \Property) {
942+ return -1 ;
943+ }
942944
943- if (!$ a instanceof Node \Stmt \ClassMethod || !$ b instanceof Node \Stmt \ClassMethod) {
944- return 0 ;
945- }
945+ if (!$ a instanceof Node \Stmt \ClassMethod || !$ b instanceof Node \Stmt \ClassMethod) {
946+ return 0 ;
947+ }
946948
947- return [!$ a ->isStatic (), $ a ->name ->toLowerString () !== '__construct ' ] <=> [!$ b ->isStatic (), $ b ->name ->toLowerString () !== '__construct ' ];
948- });
949+ return [!$ a ->isStatic (), $ a ->name ->toLowerString () !== '__construct ' ] <=> [!$ b ->isStatic (), $ b ->name ->toLowerString () !== '__construct ' ];
950+ });
951+ }
949952
950953 $ this ->processStmtNodes ($ stmt , $ classLikeStatements , $ classScope , $ classStatementsGatherer , $ context );
951954 $ nodeCallback (new ClassPropertiesNode ($ stmt , $ this ->readWritePropertiesExtensionProvider , $ classStatementsGatherer ->getProperties (), $ classStatementsGatherer ->getPropertyUsages (), $ classStatementsGatherer ->getMethodCalls (), $ classStatementsGatherer ->getReturnStatementsNodes (), $ classStatementsGatherer ->getPropertyAssigns (), $ classReflection ), $ classScope );
0 commit comments