@@ -124,7 +124,7 @@ public static function reset(): void
124124 private string $ currentNamespace = '' ;
125125
126126 /**
127- * @var list<array{name: ?string, isAnonymous: bool, hasInheritance: bool, hasRealInheritance: bool, hasPropertyWithDoc: bool, isReadonly: bool, isFinal: bool, hasTemplates: bool, isTrait?: bool, aliases?: array<string, TypeNode>, templates?: array<string, TemplateTagValueNode>}>
127+ * @var list<array{name: ?string, isAnonymous: bool, hasInheritance: bool, hasRealInheritance: bool, hasPropertyWithDoc: bool, isReadonly: bool, isFinal: bool, hasTemplates: bool, isTrait?: bool, aliases?: array<string, TypeNode>, templates?: array<string, TemplateTagValueNode>, unannotatedProps?: array<string, true> }>
128128 */
129129 private array $ classStack = [];
130130
@@ -309,8 +309,20 @@ public function enterNode(Node $node): Node|array|null
309309 return $ this ->handleExpression ($ node );
310310 }
311311
312- if ($ node instanceof Node \Expr \ArrowFunction) {
313- $ node ->expr ->setAttribute ('typephp_is_standalone ' , true );
312+ if ($ node instanceof Node \Stmt \For_) {
313+ $ doc = $ node ->getDocComment ();
314+ if ($ doc !== null && str_contains ($ doc ->getText (), '@var ' )) {
315+ $ this ->scopeManager ->extractVarDocblock ($ doc ->getText ());
316+ }
317+
318+ foreach ($ node ->init as $ initExpr ) {
319+ $ initExpr ->setAttribute ('typephp_is_standalone ' , true );
320+ }
321+ foreach ($ node ->loop as $ loopExpr ) {
322+ $ loopExpr ->setAttribute ('typephp_is_standalone ' , true );
323+ }
324+
325+ return null ;
314326 }
315327
316328 if ($ node instanceof Node \Stmt \Foreach_) {
@@ -404,10 +416,6 @@ public function leaveNode(Node $node): Node|array|null
404416 }
405417
406418 if ($ node instanceof Node \Expr \PreInc || $ node instanceof Node \Expr \PostInc || $ node instanceof Node \Expr \PreDec || $ node instanceof Node \Expr \PostDec) {
407- if (($ node instanceof Node \Expr \PostInc || $ node instanceof Node \Expr \PostDec) && $ node ->getAttribute ('typephp_is_standalone ' ) !== true ) {
408- return null ;
409- }
410-
411419 $ replacement = $ this ->handleIncDec ($ node );
412420 if ($ replacement !== null ) {
413421 return $ replacement ;
@@ -1154,13 +1162,15 @@ private function enterClassLike(Node\Stmt\Class_|Node\Stmt\Interface_|Node\Stmt\
11541162 $ hasInheritance = true ;
11551163 }
11561164
1157- if ($ typeName !== null && ($ node instanceof Node \Stmt \Class_ || $ node instanceof Node \Stmt \Trait_)) {
1165+ $ declaredPropsWithoutDoc = [];
1166+
1167+ if ($ node instanceof Node \Stmt \Class_ || $ node instanceof Node \Stmt \Trait_) {
11581168 foreach ($ node ->stmts as $ stmt ) {
11591169 if ($ stmt instanceof Node \Stmt \Property) {
11601170 $ pDoc = $ stmt ->getDocComment ();
11611171 if ($ pDoc !== null && str_contains ($ pDoc ->getText (), '@var ' )) {
11621172 $ varTag = DocblockExtractor ::extractVarTagFromDoc ($ pDoc ->getText ());
1163- if ($ varTag !== null ) {
1173+ if ($ varTag !== null && $ typeName !== null ) {
11641174 foreach ($ stmt ->props as $ p ) {
11651175 $ propName = $ p ->name ->toString ();
11661176 $ this ->localClassPropertyTypes [$ typeName ][$ propName ] = $ varTag [0 ];
@@ -1169,6 +1179,13 @@ private function enterClassLike(Node\Stmt\Class_|Node\Stmt\Interface_|Node\Stmt\
11691179 }
11701180 }
11711181 }
1182+ } elseif (! $ stmt ->isStatic ()) {
1183+ foreach ($ stmt ->props as $ p ) {
1184+ $ propName = $ p ->name ->toString ();
1185+ if ($ typeName === null || ! StubManager ::hasPropertyStub ($ typeName , $ propName )) {
1186+ $ declaredPropsWithoutDoc [$ propName ] = true ;
1187+ }
1188+ }
11721189 }
11731190 } elseif ($ stmt instanceof Node \Stmt \ClassMethod && strtolower ($ stmt ->name ->toString ()) === '__construct ' ) {
11741191 $ methodDoc = $ stmt ->getDocComment ();
@@ -1192,18 +1209,21 @@ private function enterClassLike(Node\Stmt\Class_|Node\Stmt\Interface_|Node\Stmt\
11921209 if ($ pType === null && $ methodParamTags !== null && isset ($ methodParamTags [$ paramName ])) {
11931210 $ pType = $ methodParamTags [$ paramName ]->type ;
11941211 }
1195- if ($ pType !== null ) {
1212+ if ($ pType !== null && $ typeName !== null ) {
11961213 $ this ->localClassPropertyTypes [$ typeName ][$ paramName ] = (string ) $ pType ;
11971214 if ($ shortName !== null ) {
11981215 $ this ->localClassPropertyTypes [$ shortName ][$ paramName ] = (string ) $ pType ;
11991216 }
1217+ } elseif ($ pType === null && ($ typeName === null || ! StubManager ::hasPropertyStub ($ typeName , $ paramName ))) {
1218+ $ declaredPropsWithoutDoc [$ paramName ] = true ;
12001219 }
12011220 }
12021221 }
12031222 }
12041223 }
12051224 }
12061225
1226+ $ hasMagicPropertyDoc = $ doc !== null && str_contains ($ doc ->getText (), '@property ' );
12071227 $ classAliases = $ this ->extractClassLikeAliases ($ node , $ doc , $ typeName );
12081228
12091229 $ this ->classStack [] = [
@@ -1218,6 +1238,7 @@ private function enterClassLike(Node\Stmt\Class_|Node\Stmt\Interface_|Node\Stmt\
12181238 'isTrait ' => $ isTrait ,
12191239 'aliases ' => $ classAliases ,
12201240 'templates ' => $ templates ,
1241+ 'unannotatedProps ' => (! $ hasRealInheritance && ! $ isTrait && ! $ hasMagicPropertyDoc ) ? $ declaredPropsWithoutDoc : [],
12211242 ];
12221243 }
12231244
@@ -2366,28 +2387,91 @@ private static function rhsNeedsParensInBinaryOp(Node\Expr $expr): bool
23662387 private function handleIncDec (Node \Expr \PreInc |Node \Expr \PostInc |Node \Expr \PreDec |Node \Expr \PostDec $ node ): ?Node \Expr
23672388 {
23682389 $ isInc = $ node instanceof Node \Expr \PreInc || $ node instanceof Node \Expr \PostInc;
2390+ $ isPost = $ node instanceof Node \Expr \PostInc || $ node instanceof Node \Expr \PostDec;
2391+ $ isStandalone = $ node ->getAttribute ('typephp_is_standalone ' ) === true ;
23692392 $ binaryClass = $ isInc ? Node \Expr \BinaryOp \Plus::class : Node \Expr \BinaryOp \Minus::class;
23702393
2371- if ($ this ->targetHasSideEffects ($ node ->var )) {
2372- $ preAssignments = [];
2373- $ cleanTarget = $ this ->hoistImpureTarget ($ node ->var , $ preAssignments );
2374- $ binaryExpr = new $ binaryClass (clone $ cleanTarget , new Node \Scalar \LNumber (1 ));
2394+ /** @var list<Node\Expr\Assign|Node\Expr\AssignRef> $preAssignments */
2395+ $ preAssignments = [];
2396+ $ hasSideEffects = $ this ->targetHasSideEffects ($ node ->var );
2397+ $ cleanTarget = $ hasSideEffects
2398+ ? $ this ->hoistImpureTarget ($ node ->var , $ preAssignments )
2399+ : $ node ->var ;
2400+
2401+ if ($ isPost && ! $ isStandalone ) {
2402+ if ($ this ->isProvablyUnannotatedThisProperty ($ cleanTarget )) {
2403+ return null ;
2404+ }
2405+
2406+ $ oldVar = new Node \Expr \Variable ('__typephpOld ' . $ this ->hoistTempCounter );
2407+ $ binaryExpr = new $ binaryClass (clone $ oldVar , new Node \Scalar \LNumber (1 ));
23752408 $ wrapped = $ this ->wrapTargetMutation (clone $ cleanTarget , $ binaryExpr , $ node ->var ->getStartLine ());
23762409
23772410 if ($ wrapped === null ) {
23782411 return null ;
23792412 }
23802413
2414+ $ this ->hoistTempCounter ++;
23812415 $ this ->markWriteContext ($ cleanTarget );
2416+ $ preAssignments [] = new Node \Expr \Assign (clone $ oldVar , clone $ cleanTarget );
2417+ $ preAssignments [] = new Node \Expr \Assign ($ cleanTarget , $ wrapped );
23822418
2383- return $ this ->wrapWithPreEvaluations ($ preAssignments , new Node \Expr \Assign ($ cleanTarget , $ wrapped ));
2419+ $ expr = $ this ->wrapWithPreEvaluations ($ preAssignments , $ oldVar );
2420+ $ expr ->setAttribute ('typephp_needs_parens ' , true );
2421+
2422+ return $ expr ;
23842423 }
23852424
2386- $ binaryExpr = new $ binaryClass ($ node ->var , new Node \Scalar \LNumber (1 ));
2425+ $ binaryExpr = new $ binaryClass (clone $ cleanTarget , new Node \Scalar \LNumber (1 ));
2426+ $ wrapped = $ this ->wrapTargetMutation (clone $ cleanTarget , $ binaryExpr , $ node ->var ->getStartLine ());
23872427
2388- $ wrapped = $ this ->wrapTargetMutation ($ node ->var , $ binaryExpr , $ node ->var ->getStartLine ());
2428+ if ($ wrapped === null ) {
2429+ return null ;
2430+ }
2431+
2432+ if ($ hasSideEffects ) {
2433+ $ this ->markWriteContext ($ cleanTarget );
2434+ }
2435+
2436+ $ assignExpr = new Node \Expr \Assign ($ cleanTarget , $ wrapped );
2437+ $ result = $ preAssignments !== []
2438+ ? $ this ->wrapWithPreEvaluations ($ preAssignments , $ assignExpr )
2439+ : $ assignExpr ;
2440+
2441+ if (! $ isStandalone ) {
2442+ $ result ->setAttribute ('typephp_needs_parens ' , true );
2443+ }
2444+
2445+ return $ result ;
2446+ }
2447+
2448+ private function isProvablyUnannotatedThisProperty (Node \Expr $ target ): bool
2449+ {
2450+ if (! Config ::isEliminateStaticallyProvenChecksEnabled ()) {
2451+ return false ;
2452+ }
2453+
2454+ if (
2455+ ! ($ target instanceof Node \Expr \PropertyFetch)
2456+ || ! ($ target ->var instanceof Node \Expr \Variable)
2457+ || $ target ->var ->name !== 'this '
2458+ || ! ($ target ->name instanceof Node \Identifier)
2459+ ) {
2460+ return false ;
2461+ }
2462+
2463+ if ($ this ->classStack === []) {
2464+ return false ;
2465+ }
2466+
2467+ $ classInfo = end ($ this ->classStack );
2468+ if (($ classInfo ['isTrait ' ] ?? false ) || ($ classInfo ['hasRealInheritance ' ] ?? true )) {
2469+ return false ;
2470+ }
2471+
2472+ $ propName = $ target ->name ->toString ();
23892473
2390- return $ wrapped !== null ? new Node \ Expr \ Assign ( $ node -> var , $ wrapped ) : null ;
2474+ return isset ( $ classInfo [ ' unannotatedProps ' ][ $ propName ]) ;
23912475 }
23922476
23932477 private function targetHasSideEffects (Node $ node ): bool
0 commit comments