@@ -77,6 +77,9 @@ public static function inject(Node\Stmt\Function_|Node\Stmt\ClassMethod $node, ?
7777
7878 $ hasParamOut = $ byRefParams !== [] && ($ hasParamOutDoc || $ hasInheritance );
7979
80+ $ hasSelfOutDoc = str_contains ($ docText , 'self-out ' ) || str_contains ($ docText , 'this-out ' );
81+ $ hasSelfOut = $ isClassMethod && ! $ node ->isStatic () && ($ hasSelfOutDoc || $ hasInheritance );
82+
8083 $ hasReturnDoc = str_contains ($ docText , '@return ' )
8184 || str_contains ($ docText , '@phpstan-return ' )
8285 || str_contains ($ docText , '@psalm-return ' );
@@ -86,7 +89,7 @@ public static function inject(Node\Stmt\Function_|Node\Stmt\ClassMethod $node, ?
8689 && ! ($ isNativeVoid && ! $ hasReturnDoc )
8790 && self ::hasReturnContracts ($ docText , $ isClassMethod , $ isPrivate );
8891
89- if (! $ hasParam && ! $ hasReturn && ! $ hasParamOut ) {
92+ if (! $ hasParam && ! $ hasReturn && ! $ hasParamOut && ! $ hasSelfOut ) {
9093 return ;
9194 }
9295
@@ -100,7 +103,7 @@ public static function inject(Node\Stmt\Function_|Node\Stmt\ClassMethod $node, ?
100103 $ injectedStmts = self ::buildParamInjections ($ node ->params , $ docText , $ thisArg );
101104 }
102105
103- if ($ hasReturn || $ hasParamOut ) {
106+ if ($ hasReturn || $ hasParamOut || $ hasSelfOut ) {
104107 $ node ->stmts = self ::isGenerator ($ node )
105108 ? self ::wrapGeneratorReturns ($ node ->stmts , $ thisArg )
106109 : self ::wrapNonGeneratorReturns (
@@ -109,13 +112,35 @@ public static function inject(Node\Stmt\Function_|Node\Stmt\ClassMethod $node, ?
109112 $ isNativeVoid ,
110113 $ needsReturnVars ,
111114 $ hasReturn ,
112- $ hasParamOut ? $ byRefParams : []
115+ $ hasParamOut ? $ byRefParams : [],
116+ $ hasSelfOut
113117 );
114118 }
115119
116120 $ node ->stmts = [...$ injectedStmts , ...$ node ->stmts ];
117121 }
118122
123+ public static function buildSelfOutCheckStmt (Node \Expr $ thisArg , bool $ needsReturnVars = false ): Node \Stmt \Expression
124+ {
125+ $ varsArg = $ needsReturnVars
126+ ? new Node \Expr \Variable ('_typephpArgs ' )
127+ : new Node \Expr \Array_ ();
128+
129+ $ checkCall = new Node \Expr \FuncCall (
130+ new Node \Name \FullyQualified ('TypePHP\Internal\RuntimeTypeChecker::checkSelfOut ' ),
131+ [
132+ new Node \Arg (new Node \Scalar \MagicConst \Method ()),
133+ new Node \Arg ($ thisArg ),
134+ new Node \Arg ($ varsArg ),
135+ ]
136+ );
137+
138+ $ stmt = new Node \Stmt \Expression ($ checkCall );
139+ $ stmt ->setAttribute ('typephp_injected ' , true );
140+
141+ return $ stmt ;
142+ }
143+
119144 private static function resolveDocComment (Node \Stmt \Function_ |Node \Stmt \ClassMethod $ node ): ?Doc
120145 {
121146 $ doc = $ node ->getDocComment ();
@@ -699,10 +724,11 @@ private static function wrapNonGeneratorReturns(
699724 bool $ isNativeVoid ,
700725 bool $ needsReturnVars = false ,
701726 bool $ hasReturn = true ,
702- array $ byRefParams = []
727+ array $ byRefParams = [],
728+ bool $ hasSelfOut = false
703729 ): array {
704730 $ traverser = new NodeTraverser ();
705- $ traverser ->addVisitor (new class ($ thisArg , $ isNativeVoid , $ needsReturnVars , $ hasReturn , $ byRefParams ) extends NodeVisitorAbstract {
731+ $ traverser ->addVisitor (new class ($ thisArg , $ isNativeVoid , $ needsReturnVars , $ hasReturn , $ byRefParams, $ hasSelfOut ) extends NodeVisitorAbstract {
706732 /**
707733 * @param array<string> $byRefParams
708734 */
@@ -711,7 +737,8 @@ public function __construct(
711737 private bool $ isNativeVoid ,
712738 private bool $ needsReturnVars ,
713739 private bool $ hasReturn ,
714- private array $ byRefParams
740+ private array $ byRefParams ,
741+ private bool $ hasSelfOut
715742 ) {
716743 }
717744
@@ -726,12 +753,21 @@ public function enterNode(Node $n): int|array|null
726753 return null ;
727754 }
728755
756+ $ exitStmts = [];
757+ if ($ this ->hasSelfOut ) {
758+ $ exitStmts [] = FunctionContractInjector ::buildSelfOutCheckStmt ($ this ->thisArg , $ this ->needsReturnVars );
759+ }
760+
729761 $ paramOutStmts = $ this ->byRefParams !== []
730762 ? FunctionContractInjector ::buildParamOutCheckStmts ($ this ->byRefParams , $ this ->thisArg )
731763 : [];
732764
765+ if ($ paramOutStmts !== []) {
766+ $ exitStmts = [...$ exitStmts , ...$ paramOutStmts ];
767+ }
768+
733769 if (! $ this ->hasReturn ) {
734- return $ paramOutStmts !== [] ? [...$ paramOutStmts , $ n ] : null ;
770+ return $ exitStmts !== [] ? [...$ exitStmts , $ n ] : null ;
735771 }
736772
737773 $ exprToWrap = $ n ->expr ?? new Node \Expr \ConstFetch (new Node \Name ('null ' ));
@@ -740,7 +776,7 @@ public function enterNode(Node $n): int|array|null
740776 $ checkCall = FunctionContractInjector ::buildReturnCheckCall ($ exprToWrap , $ this ->thisArg , $ this ->needsReturnVars );
741777 $ voidGuardStmts = FunctionContractInjector ::buildVoidReturnGuard ($ checkCall );
742778
743- return [...$ paramOutStmts , ...$ voidGuardStmts ];
779+ return [...$ exitStmts , ...$ voidGuardStmts ];
744780 }
745781
746782 // Call-site cache bypass for return checks
@@ -764,7 +800,7 @@ public function enterNode(Node $n): int|array|null
764800 $ ternaryExpr
765801 );
766802
767- return $ paramOutStmts !== [] ? [...$ paramOutStmts , $ n ] : null ;
803+ return $ exitStmts !== [] ? [...$ exitStmts , $ n ] : null ;
768804 }
769805
770806 return null ;
@@ -776,21 +812,28 @@ public function enterNode(Node $n): int|array|null
776812
777813 $ lastStmt = end ($ newStmts );
778814 if (! $ lastStmt instanceof Node \Stmt \Return_ && ! ($ lastStmt instanceof Node \Stmt \Expression && $ lastStmt ->expr instanceof Node \Expr \Throw_)) {
815+ $ exitStmts = [];
816+ if ($ hasSelfOut ) {
817+ $ exitStmts [] = self ::buildSelfOutCheckStmt ($ thisArg , $ needsReturnVars );
818+ }
819+
779820 $ paramOutStmts = $ byRefParams !== []
780821 ? self ::buildParamOutCheckStmts ($ byRefParams , $ thisArg )
781822 : [];
782823
824+ if ($ paramOutStmts !== []) {
825+ $ exitStmts = [...$ exitStmts , ...$ paramOutStmts ];
826+ }
827+
783828 if (! $ hasReturn ) {
784- if ($ paramOutStmts !== []) {
785- $ retStmt = new Node \Stmt \Return_ (null );
786- $ retStmt ->setAttribute ('typephp_injected ' , true );
787- $ newStmts = [...$ newStmts , ...$ paramOutStmts , $ retStmt ];
829+ if ($ exitStmts !== []) {
830+ $ newStmts = [...$ newStmts , ...$ exitStmts ];
788831 }
789832 } else {
790833 $ checkCall = self ::buildReturnCheckCall (new Node \Expr \ConstFetch (new Node \Name ('null ' )), $ thisArg , $ needsReturnVars );
791834
792835 if ($ isNativeVoid ) {
793- $ newStmts = [...$ newStmts , ...$ paramOutStmts , ...self ::buildVoidReturnGuard ($ checkCall )];
836+ $ newStmts = [...$ newStmts , ...$ exitStmts , ...self ::buildVoidReturnGuard ($ checkCall )];
794837 } else {
795838 $ cacheKeyExpr = new Node \Scalar \MagicConst \Method ();
796839 $ cacheCheck = new Node \Expr \Isset_ ([
@@ -812,7 +855,7 @@ public function enterNode(Node $n): int|array|null
812855
813856 $ retStmt = new Node \Stmt \Return_ ($ fallbackExpr );
814857 $ retStmt ->setAttribute ('typephp_injected ' , true );
815- $ newStmts = [...$ newStmts , ...$ paramOutStmts , $ retStmt ];
858+ $ newStmts = [...$ newStmts , ...$ exitStmts , $ retStmt ];
816859 }
817860 }
818861 }
0 commit comments