@@ -213,6 +213,23 @@ public function leaveNode(Node $node): Node|array|null
213213 {
214214 $ this ->trackDeclarationExit ($ node );
215215
216+ if ($ node instanceof Node \Stmt \Class_ && $ node ->name !== null ) {
217+ $ className = $ this ->resolveQualifiedName ($ node ->name );
218+ if ($ className !== null ) {
219+ $ constChecks = $ this ->collectUnprovenClassConstantChecks ($ node , $ className );
220+ if ($ constChecks !== []) {
221+ return [$ node , ...$ constChecks ];
222+ }
223+ }
224+ }
225+
226+ if ($ node instanceof Node \Stmt \Const_) {
227+ $ globalConstChecks = $ this ->collectUnprovenGlobalConstantChecks ($ node );
228+ if ($ globalConstChecks !== []) {
229+ return [$ node , ...$ globalConstChecks ];
230+ }
231+ }
232+
216233 if ($ node instanceof Node \Stmt \Function_ || $ node instanceof Node \Stmt \ClassMethod || $ node instanceof Node \Expr \Closure) {
217234 array_pop ($ this ->byRefStack );
218235 }
@@ -283,6 +300,122 @@ public function leaveNode(Node $node): Node|array|null
283300 return null ;
284301 }
285302
303+ /**
304+ * @return list<Node\Stmt>
305+ */
306+ private function collectUnprovenClassConstantChecks (Node \Stmt \Class_ $ node , string $ className ): array
307+ {
308+ $ injectedStmts = [];
309+
310+ foreach ($ node ->stmts as $ stmt ) {
311+ if (! ($ stmt instanceof Node \Stmt \ClassConst)) {
312+ continue ;
313+ }
314+
315+ $ doc = $ stmt ->getDocComment ();
316+ if ($ doc === null || ! str_contains ($ doc ->getText (), '@var ' )) {
317+ continue ;
318+ }
319+
320+ if (Config ::isRespectIgnoreTagsEnabled () && str_contains ($ doc ->getText (), '@typephp-ignore ' )) {
321+ continue ;
322+ }
323+
324+ $ varTag = DocblockExtractor ::extractVarTagFromDoc ($ doc ->getText ());
325+ if ($ varTag === null ) {
326+ continue ;
327+ }
328+
329+ $ typeString = $ varTag [0 ];
330+
331+ try {
332+ [$ typeParser , $ lexer ] = DocblockExtractor ::getTypeParserComponents ();
333+ $ tokens = new TokenIterator ($ lexer ->tokenize (DocblockNormalizer ::normalize ($ typeString )));
334+ $ typeNode = $ typeParser ->parse ($ tokens );
335+
336+ foreach ($ stmt ->consts as $ const ) {
337+ if (Config ::isEliminateStaticallyProvenChecksEnabled () && StaticTypeEvaluator ::satisfies ($ const ->value , $ typeNode , $ this ->currentFile )) {
338+ continue ;
339+ }
340+
341+ $ constName = $ const ->name ->toString ();
342+ $ fullTarget = $ className . ':: ' . $ constName ;
343+ $ classConstFetch = new Node \Expr \ClassConstFetch (new Node \Name \FullyQualified ($ className ), $ constName );
344+
345+ $ checkCall = NodeBuilder ::createConstantCheckCall (
346+ $ fullTarget ,
347+ $ classConstFetch ,
348+ $ typeString ,
349+ $ const ->getStartLine ()
350+ );
351+
352+ $ checkStmt = new Node \Stmt \Expression (NodeBuilder ::createTernaryThrowExpr ($ checkCall , $ const ->getStartLine ()));
353+ $ checkStmt ->setAttribute ('typephp_injected ' , true );
354+ $ injectedStmts [] = $ checkStmt ;
355+ }
356+ } catch (\Throwable ) {
357+ }
358+ }
359+
360+ return $ injectedStmts ;
361+ }
362+
363+ /**
364+ * @return list<Node\Stmt>
365+ */
366+ private function collectUnprovenGlobalConstantChecks (Node \Stmt \Const_ $ node ): array
367+ {
368+ $ doc = $ node ->getDocComment ();
369+ if ($ doc === null || ! str_contains ($ doc ->getText (), '@var ' )) {
370+ return [];
371+ }
372+
373+ if (Config ::isRespectIgnoreTagsEnabled () && str_contains ($ doc ->getText (), '@typephp-ignore ' )) {
374+ return [];
375+ }
376+
377+ $ varTag = DocblockExtractor ::extractVarTagFromDoc ($ doc ->getText ());
378+ if ($ varTag === null ) {
379+ return [];
380+ }
381+
382+ $ typeString = $ varTag [0 ];
383+ $ injectedStmts = [];
384+
385+ try {
386+ [$ typeParser , $ lexer ] = DocblockExtractor ::getTypeParserComponents ();
387+ $ tokens = new TokenIterator ($ lexer ->tokenize (DocblockNormalizer ::normalize ($ typeString )));
388+ $ typeNode = $ typeParser ->parse ($ tokens );
389+
390+ foreach ($ node ->consts as $ const ) {
391+ if (Config ::isEliminateStaticallyProvenChecksEnabled () && StaticTypeEvaluator ::satisfies ($ const ->value , $ typeNode , $ this ->currentFile )) {
392+ continue ;
393+ }
394+
395+ $ constName = $ const ->name ->toString ();
396+ $ qualifiedConstName = $ this ->currentNamespace !== ''
397+ ? $ this ->currentNamespace . '\\' . $ constName
398+ : $ constName ;
399+
400+ $ constFetch = new Node \Expr \ConstFetch (new Node \Name \FullyQualified ($ qualifiedConstName ));
401+
402+ $ checkCall = NodeBuilder ::createConstantCheckCall (
403+ $ qualifiedConstName ,
404+ $ constFetch ,
405+ $ typeString ,
406+ $ const ->getStartLine ()
407+ );
408+
409+ $ checkStmt = new Node \Stmt \Expression (NodeBuilder ::createTernaryThrowExpr ($ checkCall , $ const ->getStartLine ()));
410+ $ checkStmt ->setAttribute ('typephp_injected ' , true );
411+ $ injectedStmts [] = $ checkStmt ;
412+ }
413+ } catch (\Throwable ) {
414+ }
415+
416+ return $ injectedStmts ;
417+ }
418+
286419 private function createForeachBoundaryCheck (Node \Stmt \Foreach_ $ node ): ?Node \Stmt \Expression
287420 {
288421 $ targetVarExpr = null ;
@@ -324,7 +457,7 @@ private function createForeachBoundaryCheck(Node\Stmt\Foreach_ $node): ?Node\Stm
324457
325458 private static function isLoopBodyFreeOfOpaqueMutations (Node \Stmt \Foreach_ $ node ): bool
326459 {
327- $ visitor = new class () extends NodeVisitorAbstract {
460+ $ visitor = new class () extends NodeVisitorAbstract {
328461 public bool $ hasOpaqueCalls = false ;
329462
330463 public function enterNode (Node $ n ): ?int
0 commit comments